mirror of
https://github.com/nais/wonderwall.git
synced 2026-05-06 08:27:10 +00:00
refactor(retry): use functional opts, proxy to external lib
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
urllib "net/url"
|
||||
"time"
|
||||
|
||||
"github.com/sethvargo/go-retry"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/nais/wonderwall/pkg/config"
|
||||
@@ -23,7 +22,7 @@ import (
|
||||
"github.com/nais/wonderwall/pkg/openid"
|
||||
openidclient "github.com/nais/wonderwall/pkg/openid/client"
|
||||
openidconfig "github.com/nais/wonderwall/pkg/openid/config"
|
||||
retrypkg "github.com/nais/wonderwall/pkg/retry"
|
||||
"github.com/nais/wonderwall/pkg/retry"
|
||||
"github.com/nais/wonderwall/pkg/router"
|
||||
"github.com/nais/wonderwall/pkg/session"
|
||||
"github.com/nais/wonderwall/pkg/url"
|
||||
@@ -205,7 +204,7 @@ func (s *Standalone) LoginCallback(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var tokens *openid.Tokens
|
||||
err = retry.Do(r.Context(), retrypkg.DefaultBackoff, func(ctx context.Context) error {
|
||||
err = retry.Do(r.Context(), func(ctx context.Context) error {
|
||||
tokens, err = loginCallback.RedeemTokens(ctx)
|
||||
return retry.RetryableError(err)
|
||||
})
|
||||
|
||||
@@ -1,48 +1,58 @@
|
||||
package retry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/sethvargo/go-retry"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultBaseDuration = 50 * time.Millisecond
|
||||
DefaultMaxDuration = 1 * time.Second
|
||||
var (
|
||||
DefaultBackoff = Fibonacci()
|
||||
RetryableError = retry.RetryableError
|
||||
)
|
||||
|
||||
var DefaultBackoff = Fibonacci().Backoff()
|
||||
|
||||
type FibonacciBackoff struct {
|
||||
base time.Duration
|
||||
max time.Duration
|
||||
backoff retry.Backoff
|
||||
Base time.Duration
|
||||
Max time.Duration
|
||||
}
|
||||
|
||||
func (in *FibonacciBackoff) BaseDuration(base time.Duration) {
|
||||
in.base = base
|
||||
in.backoff = fibonacci(in.base, in.max)
|
||||
}
|
||||
|
||||
func (in *FibonacciBackoff) MaxDuration(max time.Duration) {
|
||||
in.max = max
|
||||
in.backoff = fibonacci(in.base, in.max)
|
||||
}
|
||||
|
||||
func (in *FibonacciBackoff) Backoff() retry.Backoff {
|
||||
return in.backoff
|
||||
}
|
||||
|
||||
func Fibonacci() *FibonacciBackoff {
|
||||
return &FibonacciBackoff{
|
||||
base: DefaultBaseDuration,
|
||||
max: DefaultMaxDuration,
|
||||
backoff: fibonacci(DefaultBaseDuration, DefaultMaxDuration),
|
||||
func WithBaseDuration(base time.Duration) func(*FibonacciBackoff) {
|
||||
return func(f *FibonacciBackoff) {
|
||||
f.Base = base
|
||||
}
|
||||
}
|
||||
|
||||
func fibonacci(base, max time.Duration) retry.Backoff {
|
||||
b := retry.NewFibonacci(base)
|
||||
b = retry.WithMaxDuration(max, b)
|
||||
func WithMaxDuration(max time.Duration) func(*FibonacciBackoff) {
|
||||
return func(f *FibonacciBackoff) {
|
||||
f.Max = max
|
||||
}
|
||||
}
|
||||
|
||||
func Fibonacci(opts ...func(f *FibonacciBackoff)) retry.Backoff {
|
||||
const DefaultBaseDuration = 50 * time.Millisecond
|
||||
const DefaultMaxDuration = 1 * time.Second
|
||||
|
||||
fb := &FibonacciBackoff{
|
||||
Base: DefaultBaseDuration,
|
||||
Max: DefaultMaxDuration,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(fb)
|
||||
}
|
||||
|
||||
b := retry.NewFibonacci(fb.Base)
|
||||
b = retry.WithMaxDuration(fb.Max, b)
|
||||
return b
|
||||
}
|
||||
|
||||
// Do retries the given function using the DefaultBackoff strategy.
|
||||
func Do(ctx context.Context, f retry.RetryFunc) error {
|
||||
return DoWithBackoff(ctx, DefaultBackoff, f)
|
||||
}
|
||||
|
||||
// DoWithBackoff retries the given function using the given backoff strategy.
|
||||
func DoWithBackoff(ctx context.Context, b retry.Backoff, f retry.RetryFunc) error {
|
||||
return retry.Do(ctx, b, f)
|
||||
}
|
||||
|
||||
@@ -7,15 +7,13 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/sethvargo/go-retry"
|
||||
|
||||
"github.com/nais/wonderwall/pkg/config"
|
||||
"github.com/nais/wonderwall/pkg/crypto"
|
||||
mw "github.com/nais/wonderwall/pkg/middleware"
|
||||
"github.com/nais/wonderwall/pkg/openid"
|
||||
openidclient "github.com/nais/wonderwall/pkg/openid/client"
|
||||
openidconfig "github.com/nais/wonderwall/pkg/openid/config"
|
||||
retrypkg "github.com/nais/wonderwall/pkg/retry"
|
||||
"github.com/nais/wonderwall/pkg/retry"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -86,7 +84,7 @@ func (in *manager) Create(r *http.Request, tokens *openid.Tokens, sessionLifetim
|
||||
return retry.RetryableError(err)
|
||||
}
|
||||
|
||||
if err := retry.Do(r.Context(), retrypkg.DefaultBackoff, retryable); err != nil {
|
||||
if err := retry.Do(r.Context(), retryable); err != nil {
|
||||
return nil, fmt.Errorf("writing to store: %w", err)
|
||||
}
|
||||
|
||||
@@ -196,7 +194,7 @@ func (in *manager) Refresh(r *http.Request, sess *Session) (*Session, error) {
|
||||
|
||||
return err
|
||||
}
|
||||
if err := retry.Do(ctx, retrypkg.DefaultBackoff, refresh); err != nil {
|
||||
if err := retry.Do(ctx, refresh); err != nil {
|
||||
if errors.Is(err, openidclient.ErrOpenIDClient) {
|
||||
return nil, fmt.Errorf("%w: authorization might be invalid: %+v", ErrInvalidExternal, err)
|
||||
}
|
||||
@@ -234,7 +232,7 @@ func (in *manager) deleteForKey(ctx context.Context, key string) error {
|
||||
return retry.RetryableError(err)
|
||||
}
|
||||
|
||||
if err := retry.Do(ctx, retrypkg.DefaultBackoff, retryable); err != nil {
|
||||
if err := retry.Do(ctx, retryable); err != nil {
|
||||
return fmt.Errorf("deleting from store: %w", err)
|
||||
}
|
||||
|
||||
@@ -262,7 +260,7 @@ func (in *manager) update(ctx context.Context, sess *Session) error {
|
||||
return retry.RetryableError(err)
|
||||
}
|
||||
|
||||
if err := retry.Do(ctx, retrypkg.DefaultBackoff, update); err != nil {
|
||||
if err := retry.Do(ctx, update); err != nil {
|
||||
return fmt.Errorf("updating in store: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,9 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/sethvargo/go-retry"
|
||||
|
||||
"github.com/nais/wonderwall/pkg/config"
|
||||
"github.com/nais/wonderwall/pkg/crypto"
|
||||
retrypkg "github.com/nais/wonderwall/pkg/retry"
|
||||
"github.com/nais/wonderwall/pkg/retry"
|
||||
)
|
||||
|
||||
var _ Reader = &reader{}
|
||||
@@ -60,7 +58,7 @@ func (in *reader) getForTicket(ctx context.Context, ticket *Ticket) (*Session, e
|
||||
return retry.RetryableError(err)
|
||||
}
|
||||
|
||||
if err := retry.Do(ctx, retrypkg.DefaultBackoff, retryable); err != nil {
|
||||
if err := retry.Do(ctx, retryable); err != nil {
|
||||
return nil, fmt.Errorf("reading from store: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user