Skip to content

Commit

Permalink
add retry throttler
Browse files Browse the repository at this point in the history
  • Loading branch information
1pkg committed Oct 19, 2020
1 parent f94cc59 commit a21a7b7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ thr := NewThrottlerAll( // throttles only if all children throttle
| any | `func NewThrottlerAny(thrs ...Throttler) Throttler` | Throttles call if any of provided throttlers throttle. |
| not | `func NewThrottlerNot(thr Throttler) Throttler` | Throttles call if provided throttler doesn't throttle. |
| suppress | `func NewThrottlerSuppress(thr Throttler) Throttler` | Suppresses provided throttler to never throttle. |
| retry | `func NewThrottlerRetry(thr Throttler, retries uint64) Throttler` | Retries provided throttler error up until the provided retries threshold.<br> Internally retry uses square throttler with `DefaultRetriedDuration` initial duration. |

## Integrations

Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions throttlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,3 +770,26 @@ func (thr tsuppress) Release(ctx context.Context) error {
_ = thr.thr.Release(ctx)
return nil
}

type tretry struct {
thr Throttler
retries uint64
}

// NewThrottlerRetry creates new throttler instance that
// retries provided throttler error up until the provided retries threshold.
// Internally retry uses square throttler with `DefaultRetriedDuration` initial duration.
func NewThrottlerRetry(thr Throttler, retries uint64) Throttler {
return tretry{thr: thr, retries: retries}
}

func (thr tretry) Acquire(ctx context.Context) error {
return retried(thr.retries, func(ctx context.Context) error {
return thr.thr.Acquire(ctx)
})(ctx)
}

func (thr tretry) Release(ctx context.Context) error {
_ = thr.thr.Release(ctx)
return nil
}
23 changes: 21 additions & 2 deletions throttlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (t *tcase) result(index int) (dur time.Duration, err error) {
}

func TestThrottlers(t *testing.T) {
DefaultRetriedDuration = time.Millisecond
cctx, cancel := context.WithCancel(context.Background())
cancel()
table := map[string]tcase{
Expand Down Expand Up @@ -774,14 +775,32 @@ func TestThrottlers(t *testing.T) {
errors.New("throttler hasn't received any internal error"),
},
},
"Throttler suppress should not throttle on internal errors": {
"Throttler suppress should not throttle on internal error": {
tms: 3,
thr: NewThrottlerSuppress(NewThrottlerEcho(errors.New("test"))),
},
"Throttler suppress should throttle on non internal errors": {
"Throttler suppress should throttle on non internal error": {
tms: 3,
thr: NewThrottlerSuppress(NewThrottlerEcho(nil)),
},
"Throttler retry should throttle on recuring internal error": {
tms: 3,
thr: NewThrottlerRetry(NewThrottlerEcho(errors.New("test")), 2),
errs: []error{
errors.New("test"),
errors.New("test"),
errors.New("test"),
},
},
"Throttler retry should not throttle on retried recuring internal error": {
tms: 3,
thr: NewThrottlerRetry(NewThrottlerAfter(3), 2),
errs: []error{
errors.New("test"),
nil,
nil,
},
},
}
for tname, ptrtcase := range table {
t.Run(tname, func(t *testing.T) {
Expand Down

0 comments on commit a21a7b7

Please sign in to comment.