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

Fix/6715 expiration update race conditions #6750

Merged
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
24 changes: 14 additions & 10 deletions inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,25 @@ public function send_post_request( $params = [], $safe = false ) {
private function send_request( $method, $params = [], $safe = false ) {
$api_url = $this->get_api_url();

if ( get_transient( $this->get_transient_key() . '_timeout_active' ) ) {
$transient_key = $this->get_transient_key();
if ( get_transient( $transient_key . '_timeout_active' ) ) {
return new WP_Error( 429, __( 'Too many requests.', 'rocket' ) );
}
// Get previous_expiration early to avoid multiple parallel requests increasing the expiration multiple times.
$previous_expiration = (int) get_transient( $transient_key . '_timeout' );

$params['method'] = strtoupper( $method );

$response = $this->send_remote_request( $api_url, $method, $params, $safe );

if ( is_wp_error( $response ) ) {
$this->set_timeout_transients();
$this->set_timeout_transients( $previous_expiration );
return $response;
}

$body = wp_remote_retrieve_body( $response );
if ( empty( $body ) || ( is_array( $response ) && ! empty( $response['response']['code'] ) && 200 !== $response['response']['code'] ) ) {
$this->set_timeout_transients();
$this->set_timeout_transients( $previous_expiration );
return new WP_Error( 500, __( 'Not valid response.', 'rocket' ) );
}

Expand All @@ -89,20 +92,21 @@ private function send_request( $method, $params = [], $safe = false ) {

/**
* Set the timeout transients.
*
* @param string $previous_expiration The previous value of _timeout_active transient.
*/
private function set_timeout_transients() {
private function set_timeout_transients( $previous_expiration ) {
$transient_key = $this->get_transient_key();

$timeout = (int) get_transient( $transient_key . '_timeout' );
$timeout = ( 0 === $timeout )
$expiration = ( 0 === $previous_expiration )
? 300
: ( 2 * $timeout <= DAY_IN_SECONDS
? 2 * $timeout
: ( 2 * $previous_expiration <= DAY_IN_SECONDS
? 2 * $previous_expiration
: DAY_IN_SECONDS
);

set_transient( $transient_key . '_timeout', $timeout, WEEK_IN_SECONDS );
set_transient( $transient_key . '_timeout_active', true, $timeout );
set_transient( $transient_key . '_timeout', $expiration, WEEK_IN_SECONDS );
set_transient( $transient_key . '_timeout_active', true, $expiration );
}

/**
Expand Down
Loading