Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hai/use redis cluster #26

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading