Skip to content

Commit

Permalink
feat: Add retry backoffs to database config (#1068)
Browse files Browse the repository at this point in the history
* Add hardcoded timeouts to database config

* Pass params though config

* Fix pass values to config

* Fix config
  • Loading branch information
Tetrergeru authored Nov 21, 2024
1 parent c7e4998 commit dbaeade
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
6 changes: 6 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type RedisConfig struct {
WriteTimeout string `yaml:"write_timeout"`
// MaxRetries count of retries.
MaxRetries int `yaml:"max_retries"`
// Minimum backoff between retries. Used to calculate exponential backoff. Default value is 0
MinRetryBackoff string `yaml:"min_retry_backoff"`
// Maximum backoff between retries. Used to calculate exponential backoff. Default value is 0
MaxRetryBackoff string `yaml:"max_retry_backoff"`
// Enables read-only commands on slave nodes.
ReadOnly bool `yaml:"read_only"`
// Allows routing read-only commands to the **closest** master or slave node.
Expand All @@ -68,6 +72,8 @@ func (config *RedisConfig) GetSettings() redis.DatabaseConfig {
SentinelUsername: config.SentinelUsername,
SentinelPassword: config.SentinelPassword,
MaxRetries: config.MaxRetries,
MinRetryBackoff: to.Duration(config.MinRetryBackoff),
MaxRetryBackoff: to.Duration(config.MaxRetryBackoff),
MetricsTTL: to.Duration(config.MetricsTTL),
DialTimeout: to.Duration(config.DialTimeout),
ReadTimeout: to.Duration(config.ReadTimeout),
Expand Down
29 changes: 18 additions & 11 deletions database/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import "time"

// DatabaseConfig - Redis database connection config.
type DatabaseConfig struct {
Addrs []string

Username string
Password string

MasterName string
Addrs []string
Username string
Password string
SentinelPassword string
SentinelUsername string
MetricsTTL time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
MaxRetries int
ReadOnly bool
RouteByLatency bool
RouteRandomly bool

MetricsTTL time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration

MaxRetries int
MinRetryBackoff time.Duration
MaxRetryBackoff time.Duration

ReadOnly bool
RouteByLatency bool
RouteRandomly bool
}

type NotificationHistoryConfig struct {
Expand Down
27 changes: 17 additions & 10 deletions database/redis/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,26 @@ type DbConnector struct {

func NewDatabase(logger moira.Logger, config DatabaseConfig, nh NotificationHistoryConfig, n NotificationConfig, source DBSource) *DbConnector {
client := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: config.Addrs,

MasterName: config.MasterName,
Addrs: config.Addrs,
Username: config.Username,
Password: config.Password,
SentinelPassword: config.SentinelPassword,
SentinelUsername: config.SentinelUsername,
DialTimeout: config.DialTimeout,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
MaxRetries: config.MaxRetries,
ReadOnly: config.ReadOnly,
RouteByLatency: config.RouteByLatency,
RouteRandomly: config.RouteRandomly,

Username: config.Username,
Password: config.Password,

DialTimeout: config.DialTimeout,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,

MaxRetries: config.MaxRetries,
MinRetryBackoff: config.MinRetryBackoff,
MaxRetryBackoff: config.MaxRetryBackoff,

ReadOnly: config.ReadOnly,
RouteByLatency: config.RouteByLatency,
RouteRandomly: config.RouteRandomly,
})

ctx := context.Background()
Expand Down
2 changes: 2 additions & 0 deletions local/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
redis:
addrs: "redis:6379"
metrics_ttl: 3h
min_retry_backoff: 1s
max_retry_backoff: 10s
telemetry:
graphite:
enabled: true
Expand Down

0 comments on commit dbaeade

Please sign in to comment.