fix(settings): cache unset scheduling thresholds

This commit is contained in:
wucm667
2026-08-11 04:34:49 +08:00
parent 0b3fe95afd
commit 3e1674a060
3 changed files with 35 additions and 2 deletions
@@ -24,8 +24,10 @@ import (
// ─── Mocks ───
type mockSettingRepo struct {
mu sync.Mutex
data map[string]string
mu sync.Mutex
data map[string]string
getValueErr error
getValueCalls int
}
func newMockSettingRepo() *mockSettingRepo {
@@ -45,6 +47,10 @@ func (m *mockSettingRepo) Get(_ context.Context, key string) (*Setting, error) {
func (m *mockSettingRepo) GetValue(_ context.Context, key string) (string, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.getValueCalls++
if m.getValueErr != nil {
return "", m.getValueErr
}
v, ok := m.data[key]
if !ok {
return "", nil
@@ -1114,6 +1114,13 @@ func (s *SettingService) GetAccountSchedulingThresholds(ctx context.Context) map
raw, err := s.settingRepo.GetValue(dbCtx, SettingKeyAccountSchedulingThresholds)
if err != nil {
if errors.Is(err, ErrSettingNotFound) {
accountSchedulingThresholdsCache.Store(&cachedAccountSchedulingThresholds{
thresholds: cloneAccountSchedulingThresholds(thresholds),
expiresAt: time.Now().Add(accountSchedulingThresholdsCacheTTL).UnixNano(),
})
return cloneAccountSchedulingThresholds(thresholds), nil
}
slog.Warn("failed to get account scheduling thresholds, falling back to defaults", "error", err)
accountSchedulingThresholdsCache.Store(&cachedAccountSchedulingThresholds{
thresholds: cloneAccountSchedulingThresholds(thresholds),
@@ -5,6 +5,7 @@ package service
import (
"context"
"testing"
"time"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/stretchr/testify/require"
@@ -108,6 +109,25 @@ func TestGetAccountSchedulingThresholds_ReadsStoredValue(t *testing.T) {
require.NotContains(t, got, "kiro")
}
func TestGetAccountSchedulingThresholds_MissingSettingUsesDefaultsAndNormalCacheTTL(t *testing.T) {
svc := newSettingServiceForPlatformThresholdTest(nil)
repo := svc.settingRepo.(*mockSettingRepo)
repo.getValueErr = ErrSettingNotFound
got := svc.GetAccountSchedulingThresholds(context.Background())
require.Equal(t, defaultAccountSchedulingThresholds(), got)
require.Equal(t, 1, repo.getValueCalls)
repo.data[SettingKeyAccountSchedulingThresholds] = `{"openai":91}`
got = svc.GetAccountSchedulingThresholds(context.Background())
require.Equal(t, 100, got[PlatformOpenAI], "missing-setting defaults should remain cached for the normal TTL")
require.Equal(t, 1, repo.getValueCalls)
cached, ok := accountSchedulingThresholdsCache.Load().(*cachedAccountSchedulingThresholds)
require.True(t, ok)
require.Greater(t, cached.expiresAt, time.Now().Add(accountSchedulingThresholdsCacheTTL-time.Second).UnixNano())
}
func TestUpdateSettings_OmittedAccountSchedulingThresholdsDoesNotCacheDefaults(t *testing.T) {
svc := newSettingServiceForPlatformThresholdTest(map[string]string{
SettingKeyAccountSchedulingThresholds: `{"openai":85,"grok":88,"kiro":87}`,