feat(server): hot-reload config store and reloadable infra clients

- Replace single Load() with a watching Store that re-reads config files
  (and config.local.yaml) and fans out a ReloadEvent with a per-field diff
  so consumers can decide whether the change is hot-applicable or requires
  a process restart.
- Wrap llm, retrieval, vector store, and object storage clients in
  Reloadable* shells so the bootstrap can swap their underlying impls when
  config changes without re-instantiating handlers.
- Make jwt.Manager and ops TokenIssuer mutable under a lock so secrets and
  TTLs can be rotated live; thread default plan code through a setter on
  the ops AdminUserService.
- Wire ConfigStore through bootstrap and every cmd/main.go, scheduler /
  worker / tenant-api / ops-api start the watcher; services and handlers
  take a config.Provider so they always read current values for things
  like generation.stream_enabled, scheduler dispatch, retrieval, etc.
- Switch shared/config decoding off viper to a Kratos-derived runtime
  package so env placeholders (\${VAR:default}) resolve consistently and
  the same source machinery powers both the loader and the watcher.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-01 16:01:23 +08:00
parent ce2d8a2907
commit 618399f86d
61 changed files with 3186 additions and 496 deletions
+26 -4
View File
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/jackc/pgx/v5"
@@ -24,6 +25,7 @@ import (
type BrandService struct {
pool *pgxpool.Pool
monitoringPool *pgxpool.Pool
limitsMu sync.RWMutex
limits sharedconfig.BrandLibraryConfig
auditLogs *auditlog.AsyncWriter
cache sharedcache.Cache
@@ -39,6 +41,24 @@ func (s *BrandService) WithCache(c sharedcache.Cache) *BrandService {
return s
}
func (s *BrandService) UpdateConfig(limits sharedconfig.BrandLibraryConfig) {
if s == nil {
return
}
s.limitsMu.Lock()
s.limits = limits
s.limitsMu.Unlock()
}
func (s *BrandService) currentLimits() sharedconfig.BrandLibraryConfig {
if s == nil {
return sharedconfig.BrandLibraryConfig{}
}
s.limitsMu.RLock()
defer s.limitsMu.RUnlock()
return s.limits
}
// --- Brand CRUD ---
type BrandRequest struct {
@@ -809,11 +829,12 @@ func (s *BrandService) loadBrandLibrarySummary(ctx context.Context, tenantID int
return nil, err
}
maxBrands := s.limits.BrandLimitForPlan(plan.PlanCode)
limits := s.currentLimits()
maxBrands := limits.BrandLimitForPlan(plan.PlanCode)
if plan.MaxBrands > 0 {
maxBrands = plan.MaxBrands
}
maxKeywords := s.limits.MaxKeywords
maxKeywords := limits.MaxKeywords
return &BrandLibrarySummaryResponse{
PlanCode: plan.PlanCode,
@@ -824,15 +845,16 @@ func (s *BrandService) loadBrandLibrarySummary(ctx context.Context, tenantID int
MaxKeywords: maxKeywords,
UsedKeywords: usage.KeywordCount,
RemainingKeywords: maxInt(maxKeywords-usage.KeywordCount, 0),
MaxQuestionsPerKeyword: s.limits.MaxQuestionsPerKeyword,
MaxQuestionsPerKeyword: limits.MaxQuestionsPerKeyword,
}, nil
}
func (s *BrandService) loadBrandLibraryPlan(ctx context.Context, tenantID int64) (*brandLibraryPlan, error) {
limits := s.currentLimits()
plan := &brandLibraryPlan{
PlanCode: "free",
PlanName: "",
MaxBrands: s.limits.BrandLimitForPlan("free"),
MaxBrands: limits.BrandLimitForPlan("free"),
}
var quotaPolicyJSON []byte