feat(platform): support redis sentinel for failover
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
dbae2870bc
commit
2bd3d0a18e
5 files changed
+110
-13
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user