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 4b25338d..f99e72fa 100644 --- a/redisstorage/config.go +++ b/redisstorage/config.go @@ -2,8 +2,11 @@ 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 - Addr string `mapstructure:"Addr"` + Addrs []string `mapstructure:"Addrs"` // Username for ACL Username string `mapstructure:"Username"` 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 9c99d752..6fb41e2a 100644 --- a/redisstorage/redisstorage.go +++ b/redisstorage/redisstorage.go @@ -20,20 +20,29 @@ const ( // redisStorageImpl implements RedisStorage interface type redisStorageImpl struct { - client *redis.Client + client RedisClient mockPrice bool } func NewRedisStorage(cfg Config) (RedisStorage, error) { - if cfg.Addr == "" { + if len(cfg.Addrs) == 0 { return nil, errors.New("redis address is empty") } - client := redis.NewClient(&redis.Options{ - Addr: cfg.Addr, - Username: cfg.Username, - Password: cfg.Password, - DB: cfg.DB, - }) + 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")