Skip to content

Commit

Permalink
Configurable max backoff interval in leader-for-life (#106)
Browse files Browse the repository at this point in the history
* Renamed maxBackoffInterval constant
* Added MaxBackoffInterval field into Config
* Check if duration interval to become a leader is meaningful

Fixes #102
  • Loading branch information
caueasantos committed Aug 30, 2022
1 parent ab57f27 commit 58f9b32
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions leader/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ var readNamespace = utils.GetOperatorNamespace

var log = logf.Log.WithName("leader")

// maxBackoffInterval defines the maximum amount of time to wait between
// defaultMaxBackoffInterval defines the default maximum amount of time to wait between
// attempts to become the leader.
const maxBackoffInterval = time.Second * 16
const defaultMaxBackoffInterval = time.Second * 16

// Option is a function that can modify Become's Config
type Option func(*Config) error

// Config defines the configuration for Become
type Config struct {
Client crclient.Client
Client crclient.Client
MaxBackoffInterval time.Duration
}

func (c *Config) setDefaults() error {
Expand All @@ -67,6 +68,12 @@ func (c *Config) setDefaults() error {
}
c.Client = client
}

// Humans tend to understand a duration value as positive numbers. The constant
// defaultMaxBackoffInterval will overwrite the content to avoid an unintended behaviour.
if c.MaxBackoffInterval <= 0 {
c.MaxBackoffInterval = defaultMaxBackoffInterval
}
return nil
}

Expand Down Expand Up @@ -195,7 +202,7 @@ func Become(ctx context.Context, lockName string, opts ...Option) error {

select {
case <-time.After(wait.Jitter(backoff, .2)):
if backoff < maxBackoffInterval {
if backoff < config.MaxBackoffInterval {
backoff *= 2
}
continue
Expand Down

0 comments on commit 58f9b32

Please sign in to comment.