Skip to content
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

Retry when rate-limit ceiling reached #23

Merged
merged 3 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 12 additions & 4 deletions lib/AcmeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,13 @@ private function doPost($resource, array $payload) {
$uri = (yield $this->getResourceUri($resource));

$attempt = 0;
$statusCode = null;

do {
$attempt++;

if ($attempt > 3) {
throw new AcmeException("POST request to {$uri} failed, received too many badNonce errors.");
throw new AcmeException("POST request to {$uri} failed, received too many errors (last code: ${statusCode}).");
}

$enc = new Base64UrlSafeEncoder();
Expand All @@ -305,13 +306,20 @@ private function doPost($resource, array $payload) {
/** @var Response $response */
$response = (yield $this->http->request($request));
$this->saveNonce($response);

if ($response->getStatus() === 400) {
$statusCode = $response->getStatus();
if ($statusCode === 400) {
$info = json_decode($response->getBody());

if ($info && isset($info->type) && ($info->type === "urn:acme:badNonce" or $info->type === "urn:acme:error:badNonce")) {
if (!empty($info->type) && ($info->type === "urn:acme:badNonce" or $info->type === "urn:acme:error:badNonce")) {
continue;
}
} else if ($statusCode === 429) {
/**
* Hit rate limit
* @{link} https://letsencrypt.org/docs/rate-limits/
*/
sleep(1);
continue;
}
} catch (Throwable $e) {
throw new AcmeException("POST request to {$uri} failed: " . $e->getMessage(), null, $e);
Expand Down
2 changes: 1 addition & 1 deletion test/AcmeClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function failsWithInvalidDirectoryResponseButCorrectErrorResponse() {
* @test
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The perm bits unintentionally changed?

* @depends boulderConfigured
* @expectedException \Kelunik\Acme\AcmeException
* @expectedExceptionMessage too many badNonce errors
* @expectedExceptionMessage too many errors (last code: 400)
*/
public function failsWithTooManyBadNonceErrors() {
$http = $this->getMockBuilder(HttpClient::class)->getMock();
Expand Down