mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-14 12:02:27 +08:00
fix(settings): cache unset scheduling thresholds
This commit is contained in:
@@ -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}`,
|
||||
|
||||
Reference in New Issue
Block a user