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.
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const retentionWindow = time.Minute
|
||||
const (
|
||||
defaultRetentionSeconds int64 = 60
|
||||
maxRetentionSeconds int64 = 3600
|
||||
)
|
||||
|
||||
type queueItem struct {
|
||||
enqueuedAt time.Time
|
||||
@@ -20,10 +23,15 @@ type queue struct {
|
||||
}
|
||||
|
||||
var (
|
||||
enabled atomic.Bool
|
||||
global queue
|
||||
enabled atomic.Bool
|
||||
retentionSeconds atomic.Int64
|
||||
global queue
|
||||
)
|
||||
|
||||
func init() {
|
||||
retentionSeconds.Store(defaultRetentionSeconds)
|
||||
}
|
||||
|
||||
func SetEnabled(value bool) {
|
||||
enabled.Store(value)
|
||||
if !value {
|
||||
@@ -35,6 +43,16 @@ func Enabled() bool {
|
||||
return enabled.Load()
|
||||
}
|
||||
|
||||
func SetRetentionSeconds(value int) {
|
||||
normalized := int64(value)
|
||||
if normalized <= 0 {
|
||||
normalized = defaultRetentionSeconds
|
||||
} else if normalized > maxRetentionSeconds {
|
||||
normalized = maxRetentionSeconds
|
||||
}
|
||||
retentionSeconds.Store(normalized)
|
||||
}
|
||||
|
||||
func Enqueue(payload []byte) {
|
||||
if !Enabled() {
|
||||
return
|
||||
@@ -110,7 +128,11 @@ func (q *queue) pruneLocked(now time.Time) {
|
||||
return
|
||||
}
|
||||
|
||||
cutoff := now.Add(-retentionWindow)
|
||||
windowSeconds := retentionSeconds.Load()
|
||||
if windowSeconds <= 0 {
|
||||
windowSeconds = defaultRetentionSeconds
|
||||
}
|
||||
cutoff := now.Add(-time.Duration(windowSeconds) * time.Second)
|
||||
for q.head < len(q.items) && q.items[q.head].enqueuedAt.Before(cutoff) {
|
||||
q.head++
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user