-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add rate limiting to api * add test for rate limiting + refactor * introduce retries with exponential backoff * add tests for backoff * cleanup + add logging on retries * resolve RateLimit naming conflict * fix review comments, add configurable logging
- Loading branch information
Showing
3 changed files
with
243 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,9 @@ func setup(opts ...Option) { | |
mux = http.NewServeMux() | ||
server = httptest.NewServer(mux) | ||
|
||
// disable rate limits and retries in testing - prepended so any provided value overrides this | ||
opts = append([]Option{UsingRateLimit(100000), UsingRetryPolicy(0, 0, 0)}, opts...) | ||
|
||
// Cloudflare client configured to use test server | ||
client, _ = New("deadbeef", "[email protected]", opts...) | ||
client.BaseURL = server.URL | ||
|
@@ -100,3 +103,101 @@ func TestClient_Auth(t *testing.T) { | |
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestClient_RetryCanSucceedAfterErrors(t *testing.T) { | ||
setup(UsingRetryPolicy(2, 0, 1)) | ||
defer teardown() | ||
|
||
requestsReceived := 0 | ||
// could test any function, using ListLoadBalancerPools | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, "GET", "Expected method 'GET', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
|
||
// we are doing three *retries* | ||
if requestsReceived == 0 { | ||
// return error causing client to retry | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, `{ | ||
"success": false, | ||
"errors": [ "server created some error"], | ||
"messages": [], | ||
"result": [] | ||
}`) | ||
} else if requestsReceived == 1 { | ||
// return error causing client to retry | ||
w.WriteHeader(429) | ||
fmt.Fprint(w, `{ | ||
"success": false, | ||
"errors": [ "this is a rate limiting error"], | ||
"messages": [], | ||
"result": [] | ||
}`) | ||
} else { | ||
// return success response | ||
fmt.Fprint(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": [ | ||
{ | ||
"id": "17b5962d775c646f3f9725cbc7a53df4", | ||
"created_on": "2014-01-01T05:20:00.12345Z", | ||
"modified_on": "2014-02-01T05:20:00.12345Z", | ||
"description": "Primary data center - Provider XYZ", | ||
"name": "primary-dc-1", | ||
"enabled": true, | ||
"monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc", | ||
"origins": [ | ||
{ | ||
"name": "app-server-1", | ||
"address": "0.0.0.0", | ||
"enabled": true | ||
} | ||
], | ||
"notification_email": "[email protected]" | ||
} | ||
], | ||
"result_info": { | ||
"page": 1, | ||
"per_page": 20, | ||
"count": 1, | ||
"total_count": 2000 | ||
} | ||
}`) | ||
} | ||
requestsReceived++ | ||
|
||
} | ||
|
||
mux.HandleFunc("/user/load_balancers/pools", handler) | ||
|
||
_, err := client.ListLoadBalancerPools() | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestClient_RetryReturnsPersistentErrorResponse(t *testing.T) { | ||
setup(UsingRetryPolicy(2, 0, 1)) | ||
defer teardown() | ||
|
||
// could test any function, using ListLoadBalancerPools | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, "GET", "Expected method 'GET', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
|
||
// return error causing client to retry | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, `{ | ||
"success": false, | ||
"errors": [ "server created some error"], | ||
"messages": [], | ||
"result": [] | ||
}`) | ||
|
||
} | ||
|
||
mux.HandleFunc("/user/load_balancers/pools", handler) | ||
|
||
_, err := client.ListLoadBalancerPools() | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters