From 497bd25cf14b0263df3ce4f1b39a2f7317e5ed63 Mon Sep 17 00:00:00 2001 From: "alexis.hermanns" Date: Mon, 7 Oct 2024 15:25:30 +0200 Subject: [PATCH 1/2] fix(connector): [ECP-113] rate limit request for woocommerce --- includes/class-lengow-connector.php | 79 +++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/includes/class-lengow-connector.php b/includes/class-lengow-connector.php index 7c6cb5d..ff771b4 100755 --- a/includes/class-lengow-connector.php +++ b/includes/class-lengow-connector.php @@ -84,6 +84,8 @@ class Lengow_Connector { const CODE_404 = 404; const CODE_500 = 500; const CODE_504 = 504; + const REQUEST_LIMIT = 500; + /** * @var array success HTTP codes for request. @@ -429,11 +431,88 @@ private function call( $api, $args, $type, $format, $body, $log_output ) { * @throws Lengow_Exception */ private function call_action( $api, $args, $type, $format, $body, $log_output ) { + $this->rate_limiting_requests($api); $result = $this->make_request( $type, $api, $args, $this->token, $body, $log_output ); return $this->format( $result, $format ); } + /** + * Rate limiting for Lengow API + */ + private function rate_limiting_requests(string $api): void + { + + switch($api) { + case self::API_ORDER: + $wait = $this->get_wait_limit_order_requests(); + break; + case self::API_ORDER_ACTION: + $wait = $this->get_wait_limit_action_requests(); + break; + case self::API_ORDER_MOI: + $wait = $this->get_wait_limit_order_requests(); + break; + default: + $wait = null; + break; + } + + if (!is_null($wait) && $wait > 0) { + Lengow_Main::log( + Lengow_Log::CODE_CONNECTOR, + Lengow_Main::set_log_message('API call blocked due to rate limiting - wait %1 seconds', [$wait]) + ); + sleep($wait); + } + } + + /** + * Limit the number of order requests + */ + private function get_wait_limit_order_requests(): ?int + { + static $nbRequest = 0; + static $timeStart = null; + if (is_null($timeStart)) { + $timeStart = time(); + } + $nbRequest++; + if ($nbRequest >= self::REQUEST_LIMIT) { + $timeDiff = time() - $timeStart; + $nbRequest = 0; + $timeStart = time(); + if ($timeDiff < 60) { + return (60 - $timeDiff); + } + } + + return null; + } + + /** + * Limit the number of action requests + */ + private function get_wait_limit_action_requests(): ?int + { + static $nbRequest = 0; + static $timeStart = null; + if (is_null($timeStart)) { + $timeStart = time(); + } + $nbRequest++; + if ($nbRequest >= self::REQUEST_LIMIT) { + $timeDiff = time() - $timeStart; + $nbRequest = 0; + $timeStart = time(); + if ($timeDiff < 60) { + return (60 - $timeDiff); + } + } + + return null; + } + /** * Get authorization token from Middleware. * From 8534779ca4a725e47cf577b30737bb00b78c3d6f Mon Sep 17 00:00:00 2001 From: "alexis.hermanns" Date: Mon, 7 Oct 2024 15:27:51 +0200 Subject: [PATCH 2/2] fix(connector): syntax for woo_commerce --- includes/class-lengow-connector.php | 44 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/includes/class-lengow-connector.php b/includes/class-lengow-connector.php index ff771b4..2ef6534 100755 --- a/includes/class-lengow-connector.php +++ b/includes/class-lengow-connector.php @@ -472,18 +472,18 @@ private function rate_limiting_requests(string $api): void */ private function get_wait_limit_order_requests(): ?int { - static $nbRequest = 0; - static $timeStart = null; - if (is_null($timeStart)) { - $timeStart = time(); + static $nb_request = 0; + static $time_start = null; + if (is_null($time_start)) { + $time_start = time(); } - $nbRequest++; - if ($nbRequest >= self::REQUEST_LIMIT) { - $timeDiff = time() - $timeStart; - $nbRequest = 0; - $timeStart = time(); - if ($timeDiff < 60) { - return (60 - $timeDiff); + $nb_request++; + if ($nb_request >= self::REQUEST_LIMIT) { + $time_diff = time() - $time_start; + $nb_request = 0; + $time_start = time(); + if ($time_diff < 60) { + return (60 - $time_diff); } } @@ -495,18 +495,18 @@ private function get_wait_limit_order_requests(): ?int */ private function get_wait_limit_action_requests(): ?int { - static $nbRequest = 0; - static $timeStart = null; - if (is_null($timeStart)) { - $timeStart = time(); + static $nb_request = 0; + static $time_start = null; + if (is_null($time_start)) { + $time_start = time(); } - $nbRequest++; - if ($nbRequest >= self::REQUEST_LIMIT) { - $timeDiff = time() - $timeStart; - $nbRequest = 0; - $timeStart = time(); - if ($timeDiff < 60) { - return (60 - $timeDiff); + $nb_request++; + if ($nb_request >= self::REQUEST_LIMIT) { + $time_diff = time() - $time_start; + $nb_request = 0; + $time_start = time(); + if ($time_diff < 60) { + return (60 - $time_diff); } }