From 4a8fa3ffc077931486a72c1b20ac01d2992d23b7 Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Fri, 28 Jun 2024 12:21:57 +0200 Subject: [PATCH 1/2] Prevent multiple updates of the _timeout transient value --- .../APIHandler/AbstractSafeAPIClient.php | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index a539b9059d..23925fafd0 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -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' ) ); } @@ -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 ); } /** From 6fcb948bd819fe0aa71684f207376443997d2757 Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Fri, 28 Jun 2024 12:30:43 +0200 Subject: [PATCH 2/2] Fix linter --- .../JobManager/APIHandler/AbstractSafeAPIClient.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index 23925fafd0..2e4d0acb48 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -67,7 +67,7 @@ private function send_request( $method, $params = [], $safe = false ) { 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 + // 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 ); @@ -75,13 +75,13 @@ private function send_request( $method, $params = [], $safe = false ) { $response = $this->send_remote_request( $api_url, $method, $params, $safe ); if ( is_wp_error( $response ) ) { - $this->set_timeout_transients($previous_expiration); + $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($previous_expiration); + $this->set_timeout_transients( $previous_expiration ); return new WP_Error( 500, __( 'Not valid response.', 'rocket' ) ); } @@ -92,10 +92,10 @@ private function send_request( $method, $params = [], $safe = false ) { /** * Set the timeout transients. - * - * @param string $previous_expiration The previous value of _timeout_active transient + * + * @param string $previous_expiration The previous value of _timeout_active transient. */ - private function set_timeout_transients($previous_expiration) { + private function set_timeout_transients( $previous_expiration ) { $transient_key = $this->get_transient_key(); $expiration = ( 0 === $previous_expiration )