From 2bd3d0a18e9a377598622dc5be3dc4666e1f556e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sat, 21 Nov 2026 23:37:48 +0100 Subject: [PATCH] feat(platform): support redis sentinel for failover Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- internal/config/config.go | 29 ++++++++++++++++------ internal/config/config_test.go | 25 +++++++++++++++++++ internal/platform/redis.go | 43 +++++++++++++++++++++++++++++---- internal/platform/redis_test.go | 24 ++++++++++++++++++ internal/platform/run.go | 2 +- 5 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 internal/config/config_test.go create mode 100644 internal/platform/redis_test.go diff --git a/internal/config/config.go b/internal/config/config.go index ce786c9..685c0a4 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,14 +16,26 @@ const ( ) type Common struct { - Edition Edition `env:"UPTIME_EDITION" envDefault:"selfhosted"` - LogLevel slog.Level `env:"UPTIME_LOG_LEVEL" envDefault:"info"` - LogFormat string `env:"UPTIME_LOG_FORMAT" envDefault:"json"` - DatabaseURL string `env:"UPTIME_DATABASE_URL"` - RedisURL string `env:"UPTIME_REDIS_URL"` + Edition Edition `env:"UPTIME_EDITION" envDefault:"selfhosted"` + LogLevel slog.Level `env:"UPTIME_LOG_LEVEL" envDefault:"info"` + LogFormat string `env:"UPTIME_LOG_FORMAT" envDefault:"json"` + DatabaseURL string `env:"UPTIME_DATABASE_URL"` + RedisURL string `env:"UPTIME_REDIS_URL"` + RedisSentinel Sentinel ShutdownTimeout time.Duration `env:"UPTIME_SHUTDOWN_TIMEOUT" envDefault:"20s"` } +// Sentinel switches the redis client to failover mode. UPTIME_REDIS_URL then +// only contributes credentials, db and TLS for the master; its host is ignored. +type Sentinel struct { + Master string `env:"UPTIME_REDIS_SENTINEL_MASTER"` + Addrs []string `env:"UPTIME_REDIS_SENTINEL_ADDRS" envSeparator:","` + Username string `env:"UPTIME_REDIS_SENTINEL_USERNAME"` + Password string `env:"UPTIME_REDIS_SENTINEL_PASSWORD"` +} + +func (s Sentinel) Enabled() bool { return s.Master != "" } + type API struct { Common Listen string `env:"UPTIME_API_LISTEN" envDefault:":8080"` @@ -57,8 +69,11 @@ func (c Common) Validate() error { if c.DatabaseURL == "" { return fmt.Errorf("config: UPTIME_DATABASE_URL is required") } - if c.RedisURL == "" { - return fmt.Errorf("config: UPTIME_REDIS_URL is required") + switch { + case c.RedisSentinel.Enabled() && len(c.RedisSentinel.Addrs) == 0: + return fmt.Errorf("config: UPTIME_REDIS_SENTINEL_ADDRS is required with UPTIME_REDIS_SENTINEL_MASTER") + case !c.RedisSentinel.Enabled() && c.RedisURL == "": + return fmt.Errorf("config: UPTIME_REDIS_URL or UPTIME_REDIS_SENTINEL_MASTER is required") } return nil } diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..aa66644 --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,25 @@ +package config + +import "testing" + +func TestValidateRedisModes(t *testing.T) { + base := Common{Edition: EditionSelfHosted, DatabaseURL: "postgres://x"} + + cases := []struct { + name string + mod func(*Common) + ok bool + }{ + {"standalone", func(c *Common) { c.RedisURL = "redis://r" }, true}, + {"nothing", func(c *Common) {}, false}, + {"sentinel", func(c *Common) { c.RedisSentinel = Sentinel{Master: "m", Addrs: []string{"s:26379"}} }, true}, + {"sentinel without addrs", func(c *Common) { c.RedisSentinel = Sentinel{Master: "m"} }, false}, + } + for _, tc := range cases { + c := base + tc.mod(&c) + if err := c.Validate(); (err == nil) != tc.ok { + t.Errorf("%s: err=%v", tc.name, err) + } + } +} diff --git a/internal/platform/redis.go b/internal/platform/redis.go index 52db8df..bbd82ae 100644 --- a/internal/platform/redis.go +++ b/internal/platform/redis.go @@ -5,17 +5,50 @@ import ( "fmt" "github.com/redis/go-redis/v9" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/config" ) -func OpenRedis(ctx context.Context, url string) (*redis.Client, error) { - opts, err := redis.ParseURL(url) - if err != nil { - return nil, fmt.Errorf("redis: %w", err) +func OpenRedis(ctx context.Context, c config.Common) (*redis.Client, error) { + var rdb *redis.Client + if c.RedisSentinel.Enabled() { + opts, err := failoverOptions(c.RedisURL, c.RedisSentinel) + if err != nil { + return nil, fmt.Errorf("redis: %w", err) + } + rdb = redis.NewFailoverClient(opts) + } else { + opts, err := redis.ParseURL(c.RedisURL) + if err != nil { + return nil, fmt.Errorf("redis: %w", err) + } + rdb = redis.NewClient(opts) } - rdb := redis.NewClient(opts) if err := rdb.Ping(ctx).Err(); err != nil { _ = rdb.Close() return nil, fmt.Errorf("redis: %w", err) } return rdb, nil } + +func failoverOptions(url string, s config.Sentinel) (*redis.FailoverOptions, error) { + fo := &redis.FailoverOptions{ + MasterName: s.Master, + SentinelAddrs: s.Addrs, + SentinelUsername: s.Username, + SentinelPassword: s.Password, + } + if url == "" { + return fo, nil + } + base, err := redis.ParseURL(url) + if err != nil { + return nil, err + } + fo.Username = base.Username + fo.Password = base.Password + fo.DB = base.DB + fo.TLSConfig = base.TLSConfig + fo.Protocol = base.Protocol + return fo, nil +} diff --git a/internal/platform/redis_test.go b/internal/platform/redis_test.go new file mode 100644 index 0000000..20bf880 --- /dev/null +++ b/internal/platform/redis_test.go @@ -0,0 +1,24 @@ +package platform + +import ( + "testing" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/config" +) + +func TestFailoverOptionsTakesMasterAuthFromURL(t *testing.T) { + s := config.Sentinel{Master: "mymaster", Addrs: []string{"s1:26379", "s2:26379"}, Password: "sentinelpw"} + fo, err := failoverOptions("rediss://app:masterpw@ignored:6379/3", s) + if err != nil { + t.Fatal(err) + } + if fo.Password != "masterpw" || fo.Username != "app" || fo.DB != 3 { + t.Fatalf("master auth not applied: %+v", fo) + } + if fo.SentinelPassword != "sentinelpw" || len(fo.SentinelAddrs) != 2 { + t.Fatalf("sentinel settings lost: %+v", fo) + } + if fo.TLSConfig == nil { + t.Fatal("rediss scheme must enable TLS") + } +} diff --git a/internal/platform/run.go b/internal/platform/run.go index 43eb860..72d5c21 100644 --- a/internal/platform/run.go +++ b/internal/platform/run.go @@ -57,7 +57,7 @@ func Connect(ctx context.Context, c config.Common, log *slog.Logger) (*Deps, err if d.DB, err = OpenPostgres(ctx, c.DatabaseURL); err != nil { return nil, err } - if d.Redis, err = OpenRedis(ctx, c.RedisURL); err != nil { + if d.Redis, err = OpenRedis(ctx, c); err != nil { d.Close() return nil, err }