-
Notifications
You must be signed in to change notification settings - Fork 23
Support maxlag and Retry-After header #28
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -53,7 +53,23 @@ public function retry( $delay = true ) { | |
* @return callable | ||
*/ | ||
private function getRetryDelay() { | ||
return function( $numberOfRetries ) { | ||
return function( $numberOfRetries, Response $response = null ) { | ||
// The $response argument is only passed as of Guzzle 6.2.2. | ||
if( $response !== null ) { | ||
// Retry-After may be a number of seconds or an absolute date (RFC 7231, | ||
// section 7.1.3). | ||
$retryAfter = $response->getHeaderLine( 'Retry-After' ); | ||
|
||
if( is_numeric( $retryAfter ) ) { | ||
return 1000 * $retryAfter; | ||
} | ||
|
||
if( $retryAfter ) { | ||
$seconds = strtotime( $retryAfter ) - time(); | ||
return 1000 * max( 1, $seconds ); | ||
} | ||
} | ||
|
||
return 1000 * $numberOfRetries; | ||
}; | ||
} | ||
|
@@ -81,38 +97,40 @@ private function newRetryDecider() { | |
} | ||
|
||
if( $response ) { | ||
$headers = $response->getHeaders(); | ||
$data = json_decode( $response->getBody(), true ); | ||
|
||
// Retry on server errors | ||
if( $response->getStatusCode() >= 500 ) { | ||
$shouldRetry = true; | ||
} | ||
|
||
if ( array_key_exists( 'mediawiki-api-error', $headers ) ) { | ||
foreach( $headers['mediawiki-api-error'] as $mediawikiApiErrorHeader ) { | ||
if ( | ||
// Retry if we have a response with an API error worth retrying | ||
in_array( | ||
$mediawikiApiErrorHeader, | ||
array( | ||
'ratelimited', | ||
'readonly', | ||
'internal_api_error_DBQueryError', | ||
) | ||
) | ||
|| | ||
// Or if we have been stopped from saving as an 'anti-abuse measure' | ||
// Note: this tries to match "actionthrottledtext" i18n messagae for mediawiki | ||
( | ||
$mediawikiApiErrorHeader == 'failed-save' && | ||
strstr( $data['error']['info'], 'anti-abuse measure' ) | ||
foreach( $response->getHeader( 'Mediawiki-Api-Error' ) as $mediawikiApiErrorHeader ) { | ||
if ( | ||
// Retry if the API explicitly tells us to: | ||
// https://www.mediawiki.org/wiki/Manual:Maxlag_parameter | ||
$response->getHeaderLine( 'Retry-After' ) | ||
|| | ||
// Retry if we have a response with an API error worth retrying | ||
in_array( | ||
$mediawikiApiErrorHeader, | ||
array( | ||
'ratelimited', | ||
'maxlag', | ||
'readonly', | ||
'internal_api_error_DBQueryError', | ||
) | ||
) { | ||
$shouldRetry = true; | ||
} | ||
|
||
) | ||
|| | ||
// Or if we have been stopped from saving as an 'anti-abuse measure' | ||
// Note: this tries to match "actionthrottledtext" i18n messagae for mediawiki | ||
( | ||
$mediawikiApiErrorHeader == 'failed-save' && | ||
strstr( $data['error']['info'], 'anti-abuse measure' ) | ||
) | ||
) { | ||
$shouldRetry = true; | ||
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. This method is really long... though that was the case before already and you did not make it worse |
||
} | ||
|
||
} | ||
} | ||
|
||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there some documentation that states that the header may contain a date / timestamp rather than a value in seconds?
I can't see it on the wiki page and I imagine including code like this will lead to some odd wait times if a date is returned due to timezones.
Uh oh!
There was an error while loading. Please reload this page.
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.
The API doc doesn't mention in, but it is allowed according to RFC 7231, section 7.1.3, so I thought it was worth spending a few lines on.
The RFC requires the absolute date returned to be a full date with timezone in a format supported by
strtotime()
, so timezones are not a problem. But it may cause issues if the local clock is very far behind – but I don't expect that to be a common problem.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.
I have added a reference to RFC 7231 in a comment.
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.
Brilliant!