From 21a4165489395fe6ff22cd4f9024bcff57fcdd4f Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Thu, 3 Apr 2025 15:51:52 -0400 Subject: [PATCH] implement redis key ttl --- contrib/schema.json | 4 ++++ pkg/ambient/config/config_recorder.go | 1 + pkg/weather/recorder/recorders/redis/redis.go | 21 +++++++++++++++++-- pkg/weather/recorder/recorders/redis/set.go | 1 + 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/contrib/schema.json b/contrib/schema.json index 2dbcf0c..a51ba8c 100644 --- a/contrib/schema.json +++ b/contrib/schema.json @@ -121,6 +121,10 @@ "default": "127.0.0.1", "type": "string" }, + "redisKeyTTL": { + "default": "24h", + "type": "string" + }, "redisPassword": { "type": "string" }, diff --git a/pkg/ambient/config/config_recorder.go b/pkg/ambient/config/config_recorder.go index f647da4..dd8f493 100644 --- a/pkg/ambient/config/config_recorder.go +++ b/pkg/ambient/config/config_recorder.go @@ -22,4 +22,5 @@ type RedisConfig struct { RedisDB int `yaml:"redisDB" env:"REDIS_DB" default:"0" json:"redisDB,omitempty"` RedisTLS bool `yaml:"redisTLS" env:"REDIS_TLS" default:"false" json:"redisTLS,omitempty"` RedisTLSInsecure bool `yaml:"redisTLSInsecure" env:"REDIS_TLS_INSECURE" default:"false" json:"redisTLSInsecure,omitempty"` + RedisKeyTTL string `yaml:"redisKeyTTL" env:"REDIS_KEY_TTL" default:"24h" json:"redisKeyTTL,omitempty"` } diff --git a/pkg/weather/recorder/recorders/redis/redis.go b/pkg/weather/recorder/recorders/redis/redis.go index 17397b3..cd46978 100644 --- a/pkg/weather/recorder/recorders/redis/redis.go +++ b/pkg/weather/recorder/recorders/redis/redis.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "fmt" "sync" + "time" "gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel" "go.opentelemetry.io/otel/attribute" @@ -20,6 +21,7 @@ import ( ) const ( + DEF_TTL = "24h" DEF_RETAIN = 120 UPDATES_KEY = "weatherUpdates" NAME = "redis recorder" @@ -27,6 +29,7 @@ const ( type RedisRecorder struct { baseCtx context.Context + keyTTL time.Duration tracer trace.Tracer redis *redis.Client config *config.AmbientLocalExporterConfig @@ -42,8 +45,10 @@ func (r *RedisRecorder) Ping(ctx context.Context) error { } func (r *RedisRecorder) Init(ctx context.Context, opts *recorders.RecorderOpts) { + r.log = zerolog.Ctx(r.baseCtx) + if opts.AppConfig.RecorderConfig.RedisConfig == nil { - panic("refusing to init redis recorder with no redisConfig") + r.log.Panic().Msg("refusing to init redis recorder with no redisConfig") } if opts.RetainLast < 1 { @@ -54,7 +59,18 @@ func (r *RedisRecorder) Init(ctx context.Context, opts *recorders.RecorderOpts) r.keep = opts.RetainLast r.RWMutex = &sync.RWMutex{} r.baseCtx = opts.BaseCtx - r.log = zerolog.Ctx(r.baseCtx) + + keyTTL := opts.AppConfig.RecorderConfig.RedisConfig.RedisKeyTTL + if keyTTL == "" { + keyTTL = DEF_TTL + } + + var err error + r.keyTTL, err = time.ParseDuration(keyTTL) + if err != nil { + r.log.Panic().Str("redisKeyTTL", keyTTL).Err(err). + Msg("invalid redis key ttl provided") + } r.tracer = otel.GetTracer(r.baseCtx, "redisRecorder") ctx, span := r.tracer.Start(ctx, "redisRecorder.init", trace.WithAttributes( @@ -62,6 +78,7 @@ func (r *RedisRecorder) Init(ctx context.Context, opts *recorders.RecorderOpts) attribute.Int("retainLast", opts.RetainLast), attribute.Int("redisPort", opts.AppConfig.RecorderConfig.RedisConfig.RedisPort), attribute.Bool("tls", opts.AppConfig.RecorderConfig.RedisConfig.RedisTLS), + attribute.String("keyTTL", r.keyTTL.String()), )) defer span.End() diff --git a/pkg/weather/recorder/recorders/redis/set.go b/pkg/weather/recorder/recorders/redis/set.go index d94bbb7..a420b51 100644 --- a/pkg/weather/recorder/recorders/redis/set.go +++ b/pkg/weather/recorder/recorders/redis/set.go @@ -48,6 +48,7 @@ func (r *RedisRecorder) set(ctx context.Context, data []byte) error { pipe.LPush(ctx, r.Key(), data) pipe.LTrim(ctx, r.Key(), 0, int64(r.keep)-1) count = pipe.LLen(ctx, r.Key()) + pipe.Expire(ctx, r.Key(), r.keyTTL) return nil }) if err != nil {