Skip to content

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

Merged
merged 3 commits into from
Sep 13, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

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.

{
$curl = curl_init($url);

Expand Down Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
So when ever we have error 429(rate limit reached) we use X-Ratelimit-Reset to determine how long the script need to wait before make another call to sendgrid.
This way we don't use a random time or predefined time.

sleep($sleepDurations > 0 ? $sleepDurations : 0);
return $this->makeRequest($method, $url, $body, $headers, false);
}

return new Response($statusCode, $responseBody, $responseHeaders);
return $response;
}

/**
Expand Down