Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
Retry on 400 (#124)
Browse files Browse the repository at this point in the history
* Retry requests on 400

* Retry requests on 400

* Retry requests on 400

* Adjust retry logic

* Adjust retry logic
  • Loading branch information
phamilton authored May 1, 2020
1 parent 3c38a10 commit da94623
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
12 changes: 8 additions & 4 deletions components/dispatcher/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (rcec retriableCloudEventClient) Dispatch(event cloudevents.Event, uri stri
}

// Build the runner for retry capabilities
runner := retry.New(retry.Config{DisableBackoff: rcec.exponentialBackoff, Times: rcec.calculateNumberOfRetries(), WaitBase: time.Millisecond * time.Duration(rcec.initialRetryInterval)})
runner := retry.New(retry.Config{DisableBackoff: !rcec.exponentialBackoff, Times: rcec.calculateNumberOfRetries(), WaitBase: time.Millisecond * time.Duration(rcec.initialRetryInterval)})

// Build the sending context for the event
sendingCtx := cloudevents.ContextWithTarget(context.Background(), uri)
Expand Down Expand Up @@ -116,8 +116,12 @@ func logResponse(logger *zap.Logger, statusCode int, err error) error {
return nil
}

// Convert defined max retry time to the approximate number
// of retries, taking into account the exponential backoff algorithm
// Determine the approximate number of retries that will take around maxRetryTime,
// depending on whether exponential backoff is enabled
func (rcec retriableCloudEventClient) calculateNumberOfRetries() int {
return int(math.Round(math.Log2(float64(rcec.maxRetryTime)/float64(rcec.initialRetryInterval))) + 1)
if rcec.exponentialBackoff {
return int(math.Round(math.Log2(float64(rcec.maxRetryTime) / float64(rcec.initialRetryInterval))))
} else {
return int(rcec.maxRetryTime / rcec.initialRetryInterval)
}
}
17 changes: 10 additions & 7 deletions components/dispatcher/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestHttpClient_Dispatch(t *testing.T) {
},
{
"Test all retries fail",
5,
11,
false,
func(w http.ResponseWriter, r *http.Request, callCount int) {
w.WriteHeader(http.StatusNotFound)
Expand Down Expand Up @@ -147,7 +147,7 @@ func setup(t *testing.T) (*retriableCloudEventClient, *httptest.Server, *http.Se
// test server
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client := NewRetriableCloudEventClient(true, 1000, 10000)
client := NewRetriableCloudEventClient(false, 1000, 10000)

return &client, server, mux
}
Expand All @@ -169,13 +169,16 @@ func TestHttpClient_calculateNumberOfRetries(t *testing.T) {
fields fields
want int
}{
{fields{maxRetryTime: 10000, initialRetryInterval: 1000}, 4},
{fields{maxRetryTime: 10000, initialRetryInterval: 5000}, 2},
{fields{maxRetryTime: 17000, initialRetryInterval: 1000}, 5},
{fields{maxRetryTime: 60000, initialRetryInterval: 5000}, 5},
{fields{maxRetryTime: 10000, initialRetryInterval: 1000, exponentialBackoff: true}, 3},
{fields{maxRetryTime: 10000, initialRetryInterval: 5000, exponentialBackoff: true}, 1},
{fields{maxRetryTime: 17000, initialRetryInterval: 1000, exponentialBackoff: true}, 4},
{fields{maxRetryTime: 60000, initialRetryInterval: 5000, exponentialBackoff: true}, 4},
{fields{maxRetryTime: 300000, initialRetryInterval: 500, exponentialBackoff: true}, 9},
{fields{maxRetryTime: 300000, initialRetryInterval: 500, exponentialBackoff: false}, 600},
{fields{maxRetryTime: 10000, initialRetryInterval: 5000, exponentialBackoff: false}, 2},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d max retry, initial interval %d", tt.fields.maxRetryTime, tt.fields.initialRetryInterval), func(t *testing.T) {
t.Run(fmt.Sprintf("%d max retry, initial interval %d, exponential backoff %t", tt.fields.maxRetryTime, tt.fields.initialRetryInterval, tt.fields.exponentialBackoff), func(t *testing.T) {
hc := retriableCloudEventClient{
exponentialBackoff: tt.fields.exponentialBackoff,
initialRetryInterval: tt.fields.initialRetryInterval,
Expand Down

0 comments on commit da94623

Please sign in to comment.