Skip to content

Commit

Permalink
adjust repeater service to match the current go-pkgz/repeater
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Feb 9, 2021
1 parent 2f99204 commit 3d7976c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 48 deletions.
20 changes: 10 additions & 10 deletions middleware/mocks/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions middleware/mocks/circuit_breaker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions middleware/mocks/logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 20 additions & 13 deletions middleware/mocks/repeater.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions middleware/repeater.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package middleware

import (
"context"
"errors"
"fmt"
"net/http"
)

// RepeaterSvc defines repeater interface
type RepeaterSvc interface {
Do(fun func() error, errs ...error) (err error)
Do(ctx context.Context, fun func() error, errs ...error) (err error)
}

// Repeater sets middleware with provided RepeaterSvc to retry failed requests
Expand All @@ -23,7 +24,7 @@ func Repeater(repeater RepeaterSvc, failOnCodes ...int) RoundTripperHandler {

var resp *http.Response
var err error
e := repeater.Do(func() error {
e := repeater.Do(req.Context(), func() error {
resp, err = next.RoundTrip(req)
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions middleware/repeater_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"context"
"errors"
"net/http"
"sync/atomic"
Expand All @@ -23,7 +24,7 @@ func TestRepeater_Passed(t *testing.T) {
return resp, errors.New("blah")
}}

repeater := &mocks.RepeaterSvcMock{DoFunc: func(fun func() error, errs ...error) (err error) {
repeater := &mocks.RepeaterSvcMock{DoFunc: func(ctx context.Context, fun func() error, errs ...error) (err error) {
for i := 0; i < 5; i++ {
if err = fun(); err == nil {
return nil
Expand Down Expand Up @@ -51,7 +52,7 @@ func TestRepeater_Failed(t *testing.T) {
return resp, errors.New("http error")
}}

repeater := &mocks.RepeaterSvcMock{DoFunc: func(fun func() error, errs ...error) (err error) {
repeater := &mocks.RepeaterSvcMock{DoFunc: func(ctx context.Context, fun func() error, errs ...error) (err error) {
for i := 0; i < 5; i++ {
if err = fun(); err == nil {
return nil
Expand All @@ -78,7 +79,7 @@ func TestRepeater_FailedStatus(t *testing.T) {
return resp, nil
}}

repeater := &mocks.RepeaterSvcMock{DoFunc: func(fun func() error, errs ...error) (err error) {
repeater := &mocks.RepeaterSvcMock{DoFunc: func(ctx context.Context, fun func() error, errs ...error) (err error) {
for i := 0; i < 5; i++ {
if err = fun(); err == nil {
return nil
Expand Down

0 comments on commit 3d7976c

Please sign in to comment.