Skip to content

Drop retry strategy and up cache time #461

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 13 commits into from
Apr 18, 2025
Merged
15 changes: 11 additions & 4 deletions inc/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function retry_decider( int $retries, RequestInterface $request, ?

$should_retry = false;

if ( $response && $response->getStatusCode() >= 500 ) {
if ( $response && ( $response->getStatusCode() >= 500 || $response->getStatusCode() === 429 ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added status code 429 here so we retry when we are being rate limited as well.

$should_retry = true;
}

Expand All @@ -143,9 +143,15 @@ public static function retry_decider( int $retries, RequestInterface $request, ?
* @return int Number of milliseconds to delay.
*/
public static function retry_delay( int $retries, ?ResponseInterface $response ): int {
// Be default, implement a linear backoff strategy.
$retry_after = $retries;

// Implement an exponential backoff strategy with jitter, with the delay capped at 10s and a minimum of 1s.
// Full Jitter taken from https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/.
// Given we only retry 3 times, this will really never exceed 8s.
$retry_after_without_jitter = min( 10, 1 * pow( 2, $retries ) );
$retry_after = wp_rand( 1, $retry_after_without_jitter );

// If the response has a Retry-After header, use that value.
// If the value is a date, calculate the difference from now.
// If the value is a number, use that as the delay.
if ( $response instanceof ResponseInterface && $response->hasHeader( 'Retry-After' ) ) {
$retry_after = $response->getHeaderLine( 'Retry-After' );

Expand All @@ -154,6 +160,7 @@ public static function retry_delay( int $retries, ?ResponseInterface $response )
}
}

// Convert it to milliseconds.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept it this way so that both the calculated value, and the "retry-after" values are converted from s to ms.

$retry_after_ms = (int) $retry_after * 1000;
return apply_filters( 'remote_data_blocks_http_client_retry_delay', $retry_after_ms, $retries, $response );
}
Expand Down
Loading