Skip to content

Commit

Permalink
Hai/use redis cluster (#26)
Browse files Browse the repository at this point in the history
* Use redis cluster client

* Update

* Add toggle

* Update tag name
  • Loading branch information
trunghai95 authored Nov 6, 2023
1 parent a788459 commit 4d01cc4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion config/config.debug.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ BridgeVersion = "v1"
MaxConns = 20
TableSuffix = ""
[BridgeServer.Redis]
Addr = "localhost:6379"
IsClusterMode = false
Addrs = ["localhost:6379"]
Username = ""
Password = ""
DB = 0
Expand Down
3 changes: 2 additions & 1 deletion config/config.local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion redisstorage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
7 changes: 7 additions & 0 deletions redisstorage/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 17 additions & 8 deletions redisstorage/redisstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4d01cc4

Please sign in to comment.