Skip to content

Commit

Permalink
feat: Add ability to read from slaves in redis cluster (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrergeru authored May 14, 2024
1 parent 1ee299f commit 1ac8c36
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
29 changes: 20 additions & 9 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,31 @@ type RedisConfig struct {
WriteTimeout string `yaml:"write_timeout"`
// MaxRetries count of retries.
MaxRetries int `yaml:"max_retries"`
// Enables read-only commands on slave nodes.
ReadOnly bool `yaml:"read_only"`
// Allows routing read-only commands to the **closest** master or slave node.
// It automatically enables ReadOnly.
RouteByLatency bool `yaml:"route_by_latency"`
// Allows routing read-only commands to the **random** master or slave node.
// It automatically enables ReadOnly.
RouteRandomly bool `yaml:"route_randomly"`
}

// GetSettings returns redis config parsed from moira config files.
func (config *RedisConfig) GetSettings() redis.DatabaseConfig {
return redis.DatabaseConfig{
MasterName: config.MasterName,
Addrs: strings.Split(config.Addrs, ","),
Username: config.Username,
Password: config.Password,
MaxRetries: config.MaxRetries,
MetricsTTL: to.Duration(config.MetricsTTL),
DialTimeout: to.Duration(config.DialTimeout),
ReadTimeout: to.Duration(config.ReadTimeout),
WriteTimeout: to.Duration(config.WriteTimeout),
MasterName: config.MasterName,
Addrs: strings.Split(config.Addrs, ","),
Username: config.Username,
Password: config.Password,
MaxRetries: config.MaxRetries,
MetricsTTL: to.Duration(config.MetricsTTL),
DialTimeout: to.Duration(config.DialTimeout),
ReadTimeout: to.Duration(config.ReadTimeout),
WriteTimeout: to.Duration(config.WriteTimeout),
ReadOnly: config.ReadOnly,
RouteByLatency: config.RouteByLatency,
RouteRandomly: config.RouteRandomly,
}
}

Expand Down
3 changes: 3 additions & 0 deletions database/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type DatabaseConfig struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
MaxRetries int
ReadOnly bool
RouteByLatency bool
RouteRandomly bool
}

type NotificationHistoryConfig struct {
Expand Down
3 changes: 3 additions & 0 deletions database/redis/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func NewDatabase(logger moira.Logger, config DatabaseConfig, nh NotificationHist
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
MaxRetries: config.MaxRetries,
ReadOnly: config.ReadOnly,
RouteByLatency: config.RouteByLatency,
RouteRandomly: config.RouteRandomly,
})

ctx := context.Background()
Expand Down

0 comments on commit 1ac8c36

Please sign in to comment.