-
Notifications
You must be signed in to change notification settings - Fork 68
Automatically retry when rate limit is reached #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,10 +134,11 @@ private function buildUrl($queryParams = null) | |
* @param string $url the final url to call | ||
* @param array $body request body | ||
* @param array $headers any additional request headers | ||
* @param bool $retryOnLimit should retry if rate limit is reach? | ||
* | ||
* @return Response object | ||
*/ | ||
public function makeRequest($method, $url, $body = null, $headers = null) | ||
public function makeRequest($method, $url, $body = null, $headers = null, $retryOnLimit = true) | ||
{ | ||
$curl = curl_init($url); | ||
|
||
|
@@ -169,8 +170,17 @@ public function makeRequest($method, $url, $body = null, $headers = null) | |
$responseHeaders = array_map('trim', $responseHeaders); | ||
|
||
curl_close($curl); | ||
|
||
$response = new Response($statusCode, $responseBody, $responseHeaders); | ||
|
||
if ($statusCode == 429 && $retryOnLimit) { | ||
$headers = $response->headers(true); | ||
$sleepDurations = $headers['X-Ratelimit-Reset'] - time(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain the idea behind this algorithm. I want to make sure we don't overload our servers when something has gone wrong. I was hoping for a backoff strategy that eventually fails after a certain period of time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every request we made to sendgrid will have X-Ratelimit-Reset in the header, where it tell us when the rate limit is reset in sendgrid. |
||
sleep($sleepDurations > 0 ? $sleepDurations : 0); | ||
return $this->makeRequest($method, $url, $body, $headers, false); | ||
} | ||
|
||
return new Response($statusCode, $responseBody, $responseHeaders); | ||
return $response; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, this should be set to null to so we don't have a breaking change.