From d8c87e3e45ba802b779e5372d17475673fd2f576 Mon Sep 17 00:00:00 2001 From: yuriy0803 Date: Wed, 31 Aug 2022 22:43:47 +0200 Subject: [PATCH] support redis sentinel mode https://codeflex.co/configuring-redis-cluster-on-linux/ --- api.json | 9 ++++++++- proxy/proxy.go | 6 ++++++ storage/redis.go | 38 +++++++++++++++++++++++++------------- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/api.json b/api.json index 860a645..22e57a3 100644 --- a/api.json +++ b/api.json @@ -93,7 +93,14 @@ "endpoint": "127.0.0.1:6379", "poolSize": 10, "database": 0, - "password": "" + "password": "", + "sentinelEnabled": false, + "masterName": "mymaster", + "sentinelAddrs": [ + "127.0.0.1:26379", + "127.0.0.1:26389", + "127.0.0.1:26399" + ] }, "exchange": { diff --git a/proxy/proxy.go b/proxy/proxy.go index 14ab2d5..a31662d 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -169,6 +169,12 @@ func (s *ProxyServer) rpc() *rpc.RPCClient { } func (s *ProxyServer) checkUpstreams() { + idx := atomic.LoadInt32(&s.upstream) + current := s.upstreams[idx] + if current.Check() { + return + } + candidate := int32(0) backup := false diff --git a/storage/redis.go b/storage/redis.go index b542871..9351a59 100644 --- a/storage/redis.go +++ b/storage/redis.go @@ -16,10 +16,13 @@ import ( ) type Config struct { - Endpoint string `json:"endpoint"` - Password string `json:"password"` - Database int64 `json:"database"` - PoolSize int `json:"poolSize"` + SentinelEnabled bool `json:"sentinelEnabled"` + Endpoint string `json:"endpoint"` + Password string `json:"password"` + Database int64 `json:"database"` + PoolSize int `json:"poolSize"` + MasterName string `json:"masterName"` + SentinelAddrs []string `json:"sentinelAddrs"` } type RedisClient struct { @@ -156,16 +159,25 @@ type Worker struct { } func NewRedisClient(cfg *Config, prefix string, pplns int64, CoinName string) *RedisClient { - options := redis.Options{ - Addr: cfg.Endpoint, - Password: cfg.Password, - DB: cfg.Database, - PoolSize: cfg.PoolSize, - } - if cfg.Endpoint[0:1] == "/" { - options.Network = "unix" + var client *redis.Client + if cfg.SentinelEnabled && len(cfg.MasterName) != 0 && len(cfg.SentinelAddrs) != 0 { + // sentinel mode + client = redis.NewFailoverClient(&redis.FailoverOptions{ + MasterName: cfg.MasterName, + SentinelAddrs: cfg.SentinelAddrs, + Password: cfg.Password, + DB: cfg.Database, + PoolSize: cfg.PoolSize, + }) + } else { + // single instance + client = redis.NewClient(&redis.Options{ + Addr: cfg.Endpoint, + Password: cfg.Password, + DB: cfg.Database, + PoolSize: cfg.PoolSize, + }) } - client := redis.NewClient(&options) return &RedisClient{client: client, prefix: prefix, pplns: pplns, CoinName: CoinName} }