fix(*): 平台稳定性 - 修复会话隔离、运行时缓存与部署镜像更新链路
This commit is contained in:
1 parent
88abdd1b75
commit
e77c26d2b0
32 files changed
+788
-116
No files matched your search
@@ -28,10 +28,11 @@ func (nopHost) ChannelConcurrencyLimit(string) (int, error) {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
repo *repository.Repository
|
||||
host Host
|
||||
Coordinator *Coordinator
|
||||
concurrencyCache *BoundedReadCache[string, RuntimeTaskPolicy]
|
||||
repo *repository.Repository
|
||||
host Host
|
||||
Coordinator *Coordinator
|
||||
concurrencyCache *BoundedReadCache[string, RuntimeTaskPolicy]
|
||||
runtimePolicyCache *BoundedReadCache[string, RuntimePolicySetting]
|
||||
}
|
||||
|
||||
func New(repo *repository.Repository, coordinator *Coordinator, host Host) *Service {
|
||||
@@ -39,10 +40,11 @@ func New(repo *repository.Repository, coordinator *Coordinator, host Host) *Serv
|
||||
host = nopHost{}
|
||||
}
|
||||
return &Service{
|
||||
repo: repo,
|
||||
host: host,
|
||||
Coordinator: coordinator,
|
||||
concurrencyCache: NewBoundedReadCache[string, RuntimeTaskPolicy](1, 1024, 1, 2*time.Second),
|
||||
repo: repo,
|
||||
host: host,
|
||||
Coordinator: coordinator,
|
||||
concurrencyCache: NewBoundedReadCache[string, RuntimeTaskPolicy](1, 1024, 1, 2*time.Second),
|
||||
runtimePolicyCache: NewBoundedReadCache[string, RuntimePolicySetting](1, 8192, 1, 2*time.Second),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,6 @@ import (
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const runtimePolicySettingKey = "runtime_policy"
|
||||
@@ -216,8 +214,21 @@ func selfUseRuntimePolicy() RuntimePolicySetting {
|
||||
func SelfUseRuntimePolicy() RuntimePolicySetting { return selfUseRuntimePolicy() }
|
||||
|
||||
func (s *Service) RuntimePolicy() (RuntimePolicySetting, error) {
|
||||
_, value, err := s.readRuntimePolicy()
|
||||
return value, err
|
||||
if s.runtimePolicyCache == nil {
|
||||
_, value, err := s.readRuntimePolicy()
|
||||
return value, err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
return s.runtimePolicyCache.Get(ctx, runtimePolicySettingKey, func(ctx context.Context) (RuntimePolicySetting, int, error) {
|
||||
reader := &Service{
|
||||
repo: s.repo.WithContext(ctx),
|
||||
host: s.host,
|
||||
Coordinator: s.Coordinator,
|
||||
}
|
||||
_, value, err := reader.readRuntimePolicy()
|
||||
return value, 2048, err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) RuntimeConcurrencySetting() (RuntimeTaskPolicy, error) {
|
||||
@@ -225,7 +236,7 @@ func (s *Service) RuntimeConcurrencySetting() (RuntimeTaskPolicy, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
return s.concurrencyCache.Get(ctx, runtimePolicySettingKey, func(ctx context.Context) (RuntimeTaskPolicy, int, error) {
|
||||
reader := &Service{repo: s.repo.WithContext(ctx), host: s.host, Coordinator: s.Coordinator, concurrencyCache: s.concurrencyCache}
|
||||
reader := &Service{repo: s.repo.WithContext(ctx), host: s.host, Coordinator: s.Coordinator, concurrencyCache: s.concurrencyCache, runtimePolicyCache: s.runtimePolicyCache}
|
||||
policy, err := reader.RuntimePolicy()
|
||||
return policy.Task, 256, err
|
||||
})
|
||||
@@ -284,6 +295,9 @@ func (s *Service) UpdateRuntimePolicySetting(actor *model.User, value RuntimePol
|
||||
}
|
||||
|
||||
s.concurrencyCache.Clear()
|
||||
if s.runtimePolicyCache != nil {
|
||||
s.runtimePolicyCache.Clear()
|
||||
}
|
||||
if err := s.appendAdminAudit(actor, "runtime_policy.update", "system_setting", runtimePolicySettingKey, "更新资源与请求策略", map[string]any{"before": before, "after": value}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -303,6 +317,9 @@ func (s *Service) ResetRuntimePolicySetting(actor *model.User) (*PublicRuntimePo
|
||||
}
|
||||
|
||||
s.concurrencyCache.Clear()
|
||||
if s.runtimePolicyCache != nil {
|
||||
s.runtimePolicyCache.Clear()
|
||||
}
|
||||
after := DefaultRuntimePolicy()
|
||||
if err := s.appendAdminAudit(actor, "runtime_policy.reset", "system_setting", runtimePolicySettingKey, "重置资源与请求策略", map[string]any{"before": before, "after": after}); err != nil {
|
||||
return nil, err
|
||||
@@ -311,14 +328,14 @@ func (s *Service) ResetRuntimePolicySetting(actor *model.User) (*PublicRuntimePo
|
||||
}
|
||||
|
||||
func (s *Service) readRuntimePolicy() (*model.SystemSetting, RuntimePolicySetting, error) {
|
||||
setting, err := s.repo.SystemSetting(runtimePolicySettingKey)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
value := DefaultRuntimePolicy()
|
||||
return nil, value, validateRuntimePolicy(value)
|
||||
}
|
||||
setting, err := s.repo.SystemSettingOptional(runtimePolicySettingKey)
|
||||
if err != nil {
|
||||
return nil, RuntimePolicySetting{}, err
|
||||
}
|
||||
if setting == nil {
|
||||
value := DefaultRuntimePolicy()
|
||||
return nil, value, validateRuntimePolicy(value)
|
||||
}
|
||||
value := DefaultRuntimePolicy()
|
||||
if strings.TrimSpace(setting.ValueJSON) == "" || json.Unmarshal([]byte(setting.ValueJSON), &value) != nil {
|
||||
return nil, RuntimePolicySetting{}, errors.New("资源与请求策略配置格式无效")
|
||||
|
||||
Reference in new issue
Block a user