-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from xataio/add-backoff-internal-lib
Add backoff lib
- Loading branch information
Showing
6 changed files
with
119 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package backoff | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
) | ||
|
||
type Backoff interface { | ||
RetryNotify(Operation, Notify) error | ||
} | ||
|
||
type ( | ||
Operation func() error | ||
Notify func(error, time.Duration) | ||
) | ||
|
||
type Config struct { | ||
InitialInterval time.Duration | ||
MaxInterval time.Duration | ||
MaxRetries uint | ||
} | ||
|
||
// ExponentialBackoff is a wrapper around the cenkalti exponential backoff | ||
type ExponentialBackoff struct { | ||
backoff.BackOff | ||
} | ||
|
||
var ErrPermanent = errors.New("permanent error, do not retry") | ||
|
||
type Provider func(ctx context.Context) Backoff | ||
|
||
func NewExponentialBackoff(ctx context.Context, cfg *Config) *ExponentialBackoff { | ||
exp := backoff.NewExponentialBackOff() | ||
exp.InitialInterval = cfg.InitialInterval | ||
exp.MaxElapsedTime = cfg.MaxInterval | ||
|
||
var bo backoff.BackOff = exp | ||
if cfg.MaxRetries > 0 { | ||
bo = backoff.WithMaxRetries(bo, uint64(cfg.MaxRetries)) | ||
} | ||
bo = backoff.WithContext(bo, ctx) | ||
|
||
return &ExponentialBackoff{ | ||
BackOff: bo, | ||
} | ||
} | ||
|
||
func (ebo *ExponentialBackoff) RetryNotify(op Operation, notify Notify) error { | ||
boOp := func() error { | ||
err := op() | ||
if errors.Is(err, ErrPermanent) { | ||
return backoff.Permanent(err) | ||
} | ||
return err | ||
} | ||
return backoff.RetryNotify(boOp, ebo, backoff.Notify(notify)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package mocks | ||
|
||
import "github.com/xataio/pgstream/internal/backoff" | ||
|
||
type Backoff struct { | ||
RetryNotifyFn func(backoff.Operation, backoff.Notify) error | ||
} | ||
|
||
func (m *Backoff) RetryNotify(op backoff.Operation, not backoff.Notify) error { | ||
return m.RetryNotifyFn(op, not) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters