Skip to content

Commit 56df368

Browse files
committed
feat: add configurable retention period for Redis usage queue
- Introduced `redis-usage-queue-retention-seconds` config parameter with a default of 60 seconds and a max of 3600 seconds. - Updated logic in `redisqueue` to honor configurable retention periods for enqueued usage data. - Modified config validation and initialization to support and enforce retention limits. - Enhanced change tracking in `config_diff` to detect updates to this parameter.
1 parent 85124f0 commit 56df368

6 files changed

Lines changed: 51 additions & 4 deletions

File tree

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ func main() {
418418
}
419419
}
420420
redisqueue.SetUsageStatisticsEnabled(cfg.UsageStatisticsEnabled)
421+
redisqueue.SetRetentionSeconds(cfg.RedisUsageQueueRetentionSeconds)
421422
coreauth.SetQuotaCooldownDisabled(cfg.DisableCooling)
422423

423424
if err = logging.ConfigureLogOutput(cfg); err != nil {

config.example.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ error-logs-max-files: 10
6666
# When false, disable in-memory usage statistics aggregation
6767
usage-statistics-enabled: false
6868

69+
# How long (in seconds) Redis usage queue items are retained in memory for the RESP interface (LPOP/RPOP).
70+
# Default: 60. Max: 3600.
71+
redis-usage-queue-retention-seconds: 60
72+
6973
# Proxy URL. Supports socks5/http/https protocols. Example: socks5://user:pass@192.168.1.1:1080/
7074
# Per-entry proxy-url also supports "direct" or "none" to bypass both the global proxy-url and environment proxies explicitly.
7175
proxy-url: ""

internal/api/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,10 @@ func (s *Server) UpdateClients(cfg *config.Config) {
10001000
redisqueue.SetUsageStatisticsEnabled(cfg.UsageStatisticsEnabled)
10011001
}
10021002

1003+
if oldCfg == nil || oldCfg.RedisUsageQueueRetentionSeconds != cfg.RedisUsageQueueRetentionSeconds {
1004+
redisqueue.SetRetentionSeconds(cfg.RedisUsageQueueRetentionSeconds)
1005+
}
1006+
10031007
if s.requestLogger != nil && (oldCfg == nil || oldCfg.ErrorLogsMaxFiles != cfg.ErrorLogsMaxFiles) {
10041008
if setter, ok := s.requestLogger.(interface{ SetErrorLogsMaxFiles(int) }); ok {
10051009
setter.SetErrorLogsMaxFiles(cfg.ErrorLogsMaxFiles)

internal/config/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ type Config struct {
6565
// UsageStatisticsEnabled toggles in-memory usage aggregation; when false, usage data is discarded.
6666
UsageStatisticsEnabled bool `yaml:"usage-statistics-enabled" json:"usage-statistics-enabled"`
6767

68+
// RedisUsageQueueRetentionSeconds controls how long (in seconds) usage queue items
69+
// are retained in memory for the Redis RESP interface (LPOP/RPOP).
70+
// Default: 60. Max: 3600.
71+
RedisUsageQueueRetentionSeconds int `yaml:"redis-usage-queue-retention-seconds" json:"redis-usage-queue-retention-seconds"`
72+
6873
// DisableCooling disables quota cooldown scheduling when true.
6974
DisableCooling bool `yaml:"disable-cooling" json:"disable-cooling"`
7075

@@ -609,6 +614,7 @@ func LoadConfigOptional(configFile string, optional bool) (*Config, error) {
609614
cfg.LogsMaxTotalSizeMB = 0
610615
cfg.ErrorLogsMaxFiles = 10
611616
cfg.UsageStatisticsEnabled = false
617+
cfg.RedisUsageQueueRetentionSeconds = 60
612618
cfg.DisableCooling = false
613619
cfg.DisableImageGeneration = DisableImageGenerationOff
614620
cfg.Pprof.Enable = false
@@ -671,6 +677,13 @@ func LoadConfigOptional(configFile string, optional bool) (*Config, error) {
671677
cfg.ErrorLogsMaxFiles = 10
672678
}
673679

680+
if cfg.RedisUsageQueueRetentionSeconds <= 0 {
681+
cfg.RedisUsageQueueRetentionSeconds = 60
682+
} else if cfg.RedisUsageQueueRetentionSeconds > 3600 {
683+
log.WithField("value", cfg.RedisUsageQueueRetentionSeconds).Warn("redis-usage-queue-retention-seconds too large; clamping to 3600")
684+
cfg.RedisUsageQueueRetentionSeconds = 3600
685+
}
686+
674687
if cfg.MaxRetryCredentials < 0 {
675688
cfg.MaxRetryCredentials = 0
676689
}

internal/redisqueue/queue.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
"time"
77
)
88

9-
const retentionWindow = time.Minute
9+
const (
10+
defaultRetentionSeconds int64 = 60
11+
maxRetentionSeconds int64 = 3600
12+
)
1013

1114
type queueItem struct {
1215
enqueuedAt time.Time
@@ -20,10 +23,15 @@ type queue struct {
2023
}
2124

2225
var (
23-
enabled atomic.Bool
24-
global queue
26+
enabled atomic.Bool
27+
retentionSeconds atomic.Int64
28+
global queue
2529
)
2630

31+
func init() {
32+
retentionSeconds.Store(defaultRetentionSeconds)
33+
}
34+
2735
func SetEnabled(value bool) {
2836
enabled.Store(value)
2937
if !value {
@@ -35,6 +43,16 @@ func Enabled() bool {
3543
return enabled.Load()
3644
}
3745

46+
func SetRetentionSeconds(value int) {
47+
normalized := int64(value)
48+
if normalized <= 0 {
49+
normalized = defaultRetentionSeconds
50+
} else if normalized > maxRetentionSeconds {
51+
normalized = maxRetentionSeconds
52+
}
53+
retentionSeconds.Store(normalized)
54+
}
55+
3856
func Enqueue(payload []byte) {
3957
if !Enabled() {
4058
return
@@ -110,7 +128,11 @@ func (q *queue) pruneLocked(now time.Time) {
110128
return
111129
}
112130

113-
cutoff := now.Add(-retentionWindow)
131+
windowSeconds := retentionSeconds.Load()
132+
if windowSeconds <= 0 {
133+
windowSeconds = defaultRetentionSeconds
134+
}
135+
cutoff := now.Add(-time.Duration(windowSeconds) * time.Second)
114136
for q.head < len(q.items) && q.items[q.head].enqueuedAt.Before(cutoff) {
115137
q.head++
116138
}

internal/watcher/diff/config_diff.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string {
3939
if oldCfg.UsageStatisticsEnabled != newCfg.UsageStatisticsEnabled {
4040
changes = append(changes, fmt.Sprintf("usage-statistics-enabled: %t -> %t", oldCfg.UsageStatisticsEnabled, newCfg.UsageStatisticsEnabled))
4141
}
42+
if oldCfg.RedisUsageQueueRetentionSeconds != newCfg.RedisUsageQueueRetentionSeconds {
43+
changes = append(changes, fmt.Sprintf("redis-usage-queue-retention-seconds: %d -> %d", oldCfg.RedisUsageQueueRetentionSeconds, newCfg.RedisUsageQueueRetentionSeconds))
44+
}
4245
if oldCfg.DisableCooling != newCfg.DisableCooling {
4346
changes = append(changes, fmt.Sprintf("disable-cooling: %t -> %t", oldCfg.DisableCooling, newCfg.DisableCooling))
4447
}

0 commit comments

Comments
 (0)