Skip to content
Merged
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
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