From 87fb094a35d3f5049e7f7d0a1501ed42b333aab8 Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Wed, 1 Nov 2023 16:24:10 +0800 Subject: [PATCH 1/4] Use redis cluster client --- redisstorage/config.go | 2 +- redisstorage/redisstorage.go | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/redisstorage/config.go b/redisstorage/config.go index 4b25338d..28bfb7aa 100644 --- a/redisstorage/config.go +++ b/redisstorage/config.go @@ -3,7 +3,7 @@ package redisstorage // Config stores the redis connection configs type Config struct { // Host:Port address - Addr string `mapstructure:"Addr"` + Addr []string `mapstructure:"Addr"` // Username for ACL Username string `mapstructure:"Username"` diff --git a/redisstorage/redisstorage.go b/redisstorage/redisstorage.go index 9c99d752..e7834b95 100644 --- a/redisstorage/redisstorage.go +++ b/redisstorage/redisstorage.go @@ -20,19 +20,18 @@ const ( // redisStorageImpl implements RedisStorage interface type redisStorageImpl struct { - client *redis.Client + client *redis.ClusterClient mockPrice bool } func NewRedisStorage(cfg Config) (RedisStorage, error) { - if cfg.Addr == "" { + if len(cfg.Addr) == 0 { return nil, errors.New("redis address is empty") } - client := redis.NewClient(&redis.Options{ - Addr: cfg.Addr, + client := redis.NewClusterClient(&redis.ClusterOptions{ + Addrs: cfg.Addr, Username: cfg.Username, Password: cfg.Password, - DB: cfg.DB, }) res, err := client.Ping(context.Background()).Result() if err != nil { From de4abcffb4b82ac29f7b38bc1a12e51bca2a5104 Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Wed, 1 Nov 2023 16:25:22 +0800 Subject: [PATCH 2/4] Update --- redisstorage/config.go | 2 +- redisstorage/redisstorage.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/redisstorage/config.go b/redisstorage/config.go index 28bfb7aa..a5c85825 100644 --- a/redisstorage/config.go +++ b/redisstorage/config.go @@ -3,7 +3,7 @@ package redisstorage // Config stores the redis connection configs type Config struct { // Host:Port address - Addr []string `mapstructure:"Addr"` + Addrs []string `mapstructure:"Addr"` // Username for ACL Username string `mapstructure:"Username"` diff --git a/redisstorage/redisstorage.go b/redisstorage/redisstorage.go index e7834b95..38886c21 100644 --- a/redisstorage/redisstorage.go +++ b/redisstorage/redisstorage.go @@ -25,11 +25,11 @@ type redisStorageImpl struct { } func NewRedisStorage(cfg Config) (RedisStorage, error) { - if len(cfg.Addr) == 0 { + if len(cfg.Addrs) == 0 { return nil, errors.New("redis address is empty") } client := redis.NewClusterClient(&redis.ClusterOptions{ - Addrs: cfg.Addr, + Addrs: cfg.Addrs, Username: cfg.Username, Password: cfg.Password, }) From 08721dffe3bcd3bc3e0470e77496dfdae5ff3f11 Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Wed, 1 Nov 2023 16:41:54 +0800 Subject: [PATCH 3/4] Add toggle --- config/config.debug.toml | 3 ++- config/config.local.toml | 3 ++- redisstorage/config.go | 3 +++ redisstorage/interfaces.go | 7 +++++++ redisstorage/redisstorage.go | 22 ++++++++++++++++------ 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/config/config.debug.toml b/config/config.debug.toml index 3c79ef21..8a1449a0 100644 --- a/config/config.debug.toml +++ b/config/config.debug.toml @@ -51,7 +51,8 @@ BridgeVersion = "v1" MaxConns = 20 TableSuffix = "" [BridgeServer.Redis] - Addr = "localhost:6379" + IsClusterMode = false + Addrs = ["localhost:6379"] Username = "" Password = "" DB = 0 diff --git a/config/config.local.toml b/config/config.local.toml index 4b374f30..9ac0d051 100644 --- a/config/config.local.toml +++ b/config/config.local.toml @@ -52,7 +52,8 @@ SentinelConfigFilePath = "/app/sentinel_config.json" MaxConns = 20 TableSuffix = "" [BridgeServer.Redis] - Addr = "xgon-bridge-redis:6379" + IsClusterMode = false + Addrs = ["xgon-bridge-redis:6379"] Username = "" Password = "" DB = 0 diff --git a/redisstorage/config.go b/redisstorage/config.go index a5c85825..3f13fc27 100644 --- a/redisstorage/config.go +++ b/redisstorage/config.go @@ -2,6 +2,9 @@ package redisstorage // Config stores the redis connection configs type Config struct { + // If this is true, will use ClusterClient + IsClusterMode bool `mapstructure:"IsClusterMode"` + // Host:Port address Addrs []string `mapstructure:"Addr"` diff --git a/redisstorage/interfaces.go b/redisstorage/interfaces.go index b9d67290..58eb2f51 100644 --- a/redisstorage/interfaces.go +++ b/redisstorage/interfaces.go @@ -4,9 +4,16 @@ import ( "context" "github.com/0xPolygonHermez/zkevm-bridge-service/bridgectrl/pb" + "github.com/redis/go-redis/v9" ) type RedisStorage interface { SetCoinPrice(ctx context.Context, prices []*pb.SymbolPrice) error GetCoinPrice(ctx context.Context, symbols []*pb.SymbolInfo) ([]*pb.SymbolPrice, error) } + +type RedisClient interface { + Ping(ctx context.Context) *redis.StatusCmd + HSet(ctx context.Context, key string, values ...interface{}) *redis.IntCmd + HMGet(ctx context.Context, key string, fields ...string) *redis.SliceCmd +} diff --git a/redisstorage/redisstorage.go b/redisstorage/redisstorage.go index 38886c21..6fb41e2a 100644 --- a/redisstorage/redisstorage.go +++ b/redisstorage/redisstorage.go @@ -20,7 +20,7 @@ const ( // redisStorageImpl implements RedisStorage interface type redisStorageImpl struct { - client *redis.ClusterClient + client RedisClient mockPrice bool } @@ -28,11 +28,21 @@ func NewRedisStorage(cfg Config) (RedisStorage, error) { if len(cfg.Addrs) == 0 { return nil, errors.New("redis address is empty") } - client := redis.NewClusterClient(&redis.ClusterOptions{ - Addrs: cfg.Addrs, - Username: cfg.Username, - Password: cfg.Password, - }) + var client RedisClient + if cfg.IsClusterMode { + client = redis.NewClusterClient(&redis.ClusterOptions{ + Addrs: cfg.Addrs, + Username: cfg.Username, + Password: cfg.Password, + }) + } else { + client = redis.NewClient(&redis.Options{ + Addr: cfg.Addrs[0], + Username: cfg.Username, + Password: cfg.Password, + DB: cfg.DB, + }) + } res, err := client.Ping(context.Background()).Result() if err != nil { return nil, errors.Wrap(err, "cannot connect to redis server") From 941ac943e0795d77baaa3bd830f0d3df1b8964d8 Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Wed, 1 Nov 2023 16:51:07 +0800 Subject: [PATCH 4/4] Update tag name --- redisstorage/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redisstorage/config.go b/redisstorage/config.go index 3f13fc27..f99e72fa 100644 --- a/redisstorage/config.go +++ b/redisstorage/config.go @@ -6,7 +6,7 @@ type Config struct { IsClusterMode bool `mapstructure:"IsClusterMode"` // Host:Port address - Addrs []string `mapstructure:"Addr"` + Addrs []string `mapstructure:"Addrs"` // Username for ACL Username string `mapstructure:"Username"`