Skip to content

Commit

Permalink
Merge pull request #29 from seatgeek/upgrade-backoff-to-v5
Browse files Browse the repository at this point in the history
Update module github.com/cenkalti/backoff/v4 to v5
  • Loading branch information
colinodell authored Dec 31, 2024
2 parents 45cfb53 + 980491a commit a5b6367
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 49 deletions.
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module github.com/seatgeek/mailroom

go 1.22.3
go 1.23

toolchain go1.23.4

require (
github.com/cenkalti/backoff/v4 v4.3.0
github.com/cenkalti/backoff/v5 v5.0.0
github.com/go-playground/webhooks/v6 v6.4.0
github.com/google/uuid v1.6.0
github.com/lmittmann/tint v1.0.6
Expand All @@ -18,6 +20,7 @@ require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOEl
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v5 v5.0.0 h1:4ziwFuaVJicDO1ah1Nz1aXXV1caM28PFgf1V5TTFXew=
github.com/cenkalti/backoff/v5 v5.0.0/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
github.com/containerd/containerd v1.7.18 h1:jqjZTQNfXGoEaZdW1WwPU0RqSn1Bm2Ay/KJPUuO8nao=
github.com/containerd/containerd v1.7.18/go.mod h1:IYEk9/IO6wAPUz2bCMVUbsfXjzw5UNP5fLz4PsUygQ4=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand Down
4 changes: 3 additions & 1 deletion internal/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func main() {
// 5*time.Second,
// ),
// 3,
// backoff.WithMaxElapsedTime(30*time.Second),
// func() notifier.BackOff {
// return backoff.NewExponentialBackOff()
// },
// ),
),
mailroom.WithUserStore(
Expand Down
2 changes: 1 addition & 1 deletion pkg/notifier/conventions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package notifier
import (
"context"

"github.com/cenkalti/backoff/v4"
"github.com/cenkalti/backoff/v5"
"github.com/seatgeek/mailroom/pkg/common"
)

Expand Down
41 changes: 21 additions & 20 deletions pkg/notifier/decorators.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"log/slog"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/cenkalti/backoff/v5"
"github.com/seatgeek/mailroom/pkg/common"
)

Expand Down Expand Up @@ -41,37 +41,38 @@ func (w *withTimeout) Validate(ctx context.Context) error {
return nil
}

// WithRetry decorates the given Transport with retry logic using exponential backoff
func WithRetry(transport Transport, maxRetries uint64, opts ...backoff.ExponentialBackOffOpts) Transport {
type BackOff = backoff.BackOff
type BackOffFactory = func() BackOff

// WithRetry decorates the given Transport with retry logic using the provided backoff
func WithRetry(transport Transport, maxTries uint, backoffFactory BackOffFactory) Transport {
return &withRetry{
Transport: transport,
maxRetries: maxRetries,
opts: opts,
Transport: transport,
maxTries: maxTries,
backoff: backoffFactory,
}
}

type withRetry struct {
Transport
maxRetries uint64
opts []backoff.ExponentialBackOffOpts
maxTries uint
backoff func() BackOff
}

func (w *withRetry) Push(ctx context.Context, notification common.Notification) error {
return backoff.RetryNotify(
func() error {
return w.Transport.Push(ctx, notification)
_, err := backoff.Retry(
ctx,
func() (bool, error) {
return true, w.Transport.Push(ctx, notification)
},
backoff.WithMaxRetries(
backoff.WithContext(
backoff.NewExponentialBackOff(w.opts...),
ctx,
),
w.maxRetries,
),
func(err error, duration time.Duration) {
backoff.WithMaxTries(w.maxTries),
backoff.WithBackOff(w.backoff()),
backoff.WithNotify(func(err error, duration time.Duration) {
slog.Error("failed to push notification", "id", notification.Context().ID, "error", err, "next_retry", duration.String())
},
}),
)

return err
}

func (w *withRetry) Validate(ctx context.Context) error {
Expand Down
46 changes: 23 additions & 23 deletions pkg/notifier/decorators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/cenkalti/backoff/v5"
"github.com/seatgeek/mailroom/pkg/common"
"github.com/seatgeek/mailroom/pkg/event"
"github.com/seatgeek/mailroom/pkg/identifier"
Expand Down Expand Up @@ -56,51 +56,53 @@ func TestWithRetry(t *testing.T) {
Type: "test",
}).Build()

err1 := errors.New("err 1")
err2 := errors.New("err 2")

tests := []struct {
name string
maxRetries uint64
maxTries uint
givenErrs []error
wantAttempts int
wantErr error
}{
{
name: "no errors",
maxRetries: 2,
name: "no errors",
maxTries: 2,
givenErrs: []error{
nil,
},
wantAttempts: 1,
wantErr: nil,
},
{
name: "one error",
maxRetries: 2,
name: "one error",
maxTries: 2,
givenErrs: []error{
errors.New("test"),
err1,
nil,
},
wantAttempts: 2,
wantErr: nil,
},
{
name: "one permanent error",
maxRetries: 2,
name: "one permanent error",
maxTries: 2,
givenErrs: []error{
notifier.Permanent(errors.New("test")),
notifier.Permanent(err1),
},
wantAttempts: 1,
wantErr: errors.New("test"),
wantErr: err1,
},
{
name: "max attempts",
maxRetries: 2,
name: "max attempts",
maxTries: 2,
givenErrs: []error{
errors.New("err 1"),
errors.New("err 2"),
errors.New("err 3"),
err1,
err2,
},
wantAttempts: 3,
wantErr: errors.New("err 3"),
wantAttempts: 2,
wantErr: err2,
},
}

Expand All @@ -114,17 +116,15 @@ func TestWithRetry(t *testing.T) {
transport.EXPECT().Push(mock.Anything, mock.Anything).Return(givenErr).Once()
}

wrapped := notifier.WithRetry(transport, tc.maxRetries, func(b *backoff.ExponentialBackOff) {
b.InitialInterval = 1 * time.Millisecond
b.MaxInterval = 10 * time.Millisecond
b.MaxElapsedTime = 20 * time.Millisecond
wrapped := notifier.WithRetry(transport, tc.maxTries, func() backoff.BackOff {
return backoff.NewConstantBackOff(10 * time.Millisecond)
})

assert.Equal(t, transport.Key(), wrapped.Key(), "Key should be the same")

err := wrapped.Push(context.Background(), fakeNotification)

assert.Equal(t, tc.wantErr, err, "Error should match")
assert.ErrorIs(t, err, tc.wantErr, "Error should match")
})
}
}
Expand Down

0 comments on commit a5b6367

Please sign in to comment.