Skip to content

Commit

Permalink
chore: backoff options
Browse files Browse the repository at this point in the history
  • Loading branch information
fracasula committed Dec 10, 2024
1 parent b355760 commit 889d1c7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cmd/sampleapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func setupWorkspaceConfigsPoller[K comparable](
poller.WithPollingInterval[K](1*time.Second),
poller.WithPollingBackoffInitialInterval[K](1*time.Second),
poller.WithPollingBackoffMaxInterval[K](1*time.Minute),
poller.WithPollingMaxElapsedTime[K](5*time.Minute),
poller.WithPollingMaxRetries[K](15),
poller.WithPollingBackoffMultiplier[K](1.5),
poller.WithOnResponse[K](func(updated bool, err error) {
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions poller/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func WithPollingBackoffMaxInterval[K comparable](d time.Duration) Option[K] {
return func(p *WorkspaceConfigsPoller[K]) { p.backoff.maxInterval = d }
}

func WithPollingMaxElapsedTime[K comparable](d time.Duration) Option[K] {
return func(p *WorkspaceConfigsPoller[K]) { p.backoff.maxElapsedTime = d }
}

func WithPollingMaxRetries[K comparable](n uint64) Option[K] {
return func(p *WorkspaceConfigsPoller[K]) { p.backoff.maxRetries = n }
}

func WithPollingBackoffMultiplier[K comparable](m float64) Option[K] {
return func(p *WorkspaceConfigsPoller[K]) { p.backoff.multiplier = m }
}
Expand Down
9 changes: 7 additions & 2 deletions poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type WorkspaceConfigsPoller[K comparable] struct {
backoff struct {
initialInterval time.Duration
maxInterval time.Duration
maxElapsedTime time.Duration
maxRetries uint64
multiplier float64
}
log logger.Logger
Expand All @@ -47,6 +49,8 @@ func NewWorkspaceConfigsPoller[K comparable](
}
p.backoff.initialInterval = 1 * time.Second
p.backoff.maxInterval = 1 * time.Minute
p.backoff.maxElapsedTime = 5 * time.Minute
p.backoff.maxRetries = 15
p.backoff.multiplier = 1.5

for _, opt := range opts {
Expand Down Expand Up @@ -148,9 +152,10 @@ func (p *WorkspaceConfigsPoller[K]) poll(ctx context.Context) (bool, error) {
}

func (p *WorkspaceConfigsPoller[K]) nextBackOff() func() time.Duration {
return backoff.NewExponentialBackOff(
return backoff.WithMaxRetries(backoff.NewExponentialBackOff(
backoff.WithInitialInterval(p.backoff.initialInterval),
backoff.WithMaxInterval(p.backoff.maxInterval),
backoff.WithMaxElapsedTime(p.backoff.maxElapsedTime),
backoff.WithMultiplier(p.backoff.multiplier),
).NextBackOff
), p.backoff.maxRetries).NextBackOff
}

0 comments on commit 889d1c7

Please sign in to comment.