Skip to content

Commit

Permalink
feat: Add support for sentinel writer (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy authored May 23, 2024
1 parent 2c9b94f commit 7c75078
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/redis-shake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func main() {
if opts.Cluster {
theWriter = writer.NewRedisClusterWriter(ctx, opts)
log.Infof("create RedisClusterWriter: %v", opts.Address)
} else if opts.Sentinel {
theWriter = writer.NewRedisSentinelWriter(ctx, opts)
log.Infof("create RedisSentinelWriter: %v", opts.Address)
} else {
theWriter = writer.NewRedisStandaloneWriter(ctx, opts)
log.Infof("create RedisStandaloneWriter: %v", opts.Address)
Expand Down
4 changes: 4 additions & 0 deletions internal/client/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type Redis struct {
protoWriter *proto.Writer
}

func NewSentinelClient(ctx context.Context, address string, Tls bool) *Redis {
return NewRedisClient(ctx, address, "", "", Tls)
}

func NewRedisClient(ctx context.Context, address string, username string, password string, Tls bool) *Redis {
r := new(Redis)
var conn net.Conn
Expand Down
30 changes: 30 additions & 0 deletions internal/writer/redis_sentinel_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package writer

import (
"RedisShake/internal/client"
"RedisShake/internal/log"
"context"
"fmt"
)

func NewRedisSentinelWriter(ctx context.Context, opts *RedisWriterOptions) Writer {
sentinel := client.NewSentinelClient(ctx, opts.Address, opts.Tls)
sentinel.Send("SENTINEL", "GET-MASTER-ADDR-BY-NAME", opts.Master)
addr, err := sentinel.Receive()
if err != nil {
log.Panicf(err.Error())
}
hostport := addr.([]interface{})
address := fmt.Sprintf("%s:%s", hostport[0].(string), hostport[1].(string))
sentinel.Close()

redisOpt := &RedisWriterOptions{
Address: address,
Username: opts.Username,
Password: opts.Password,
Tls: opts.Tls,
OffReply: opts.OffReply,
}
log.Infof("connecting to master node at %s", redisOpt.Address)
return NewRedisStandaloneWriter(ctx, redisOpt)
}
2 changes: 2 additions & 0 deletions internal/writer/redis_standalone_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

type RedisWriterOptions struct {
Cluster bool `mapstructure:"cluster" default:"false"`
Sentinel bool `mapstructure:"sentinel" default:"false"`
Master string `mapstructure:"master" default:""`
Address string `mapstructure:"address" default:""`
Username string `mapstructure:"username" default:""`
Password string `mapstructure:"password" default:""`
Expand Down
2 changes: 2 additions & 0 deletions shake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ prefer_replica = true # set to true if you want to sync from replica node

[redis_writer]
cluster = false # set to true if target is a redis cluster
sentinel = false # set to true if target is a redis sentinel
master = "" # set to master name if target is a redis sentinel
address = "127.0.0.1:6380" # when cluster is true, set address to one of the cluster node
username = "" # keep empty if not using ACL
password = "" # keep empty if no authentication is required
Expand Down

0 comments on commit 7c75078

Please sign in to comment.