From 751e635f0045f8a92d98318e380d3c7beebebffe Mon Sep 17 00:00:00 2001 From: Eitaro Fukamachi Date: Tue, 17 Sep 2024 05:44:22 +0000 Subject: [PATCH 1/4] Revert "Merge pull request #59 from pokepay/revert-58-retry" This reverts commit cae733c029cb776fe5068e5a810419f745bdb897, reversing changes made to 19cd37e3ed4298c31dcba44a459cfcc8bc60b47a. --- lib/Error/RequestIdConflict.php | 15 +++++++++++ lib/HttpClient.php | 1 + lib/PartnerAPI.php | 46 ++++++++++++++++++++++++++------- lib/Request/Base.php | 2 +- 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 lib/Error/RequestIdConflict.php diff --git a/lib/Error/RequestIdConflict.php b/lib/Error/RequestIdConflict.php new file mode 100644 index 00000000..a06008b0 --- /dev/null +++ b/lib/Error/RequestIdConflict.php @@ -0,0 +1,15 @@ +requestId = $requestId; + } +} diff --git a/lib/HttpClient.php b/lib/HttpClient.php index b0d8379d..1c36a9bc 100644 --- a/lib/HttpClient.php +++ b/lib/HttpClient.php @@ -37,6 +37,7 @@ public function request($callId, $method, $url, $headers, $params, $responseClas if ($this->curlOptions) { $opts += $this->curlOptions; } + $opts += array(CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5); curl_setopt_array($curl, $opts); diff --git a/lib/PartnerAPI.php b/lib/PartnerAPI.php index c4e4bb59..0fa20ae0 100644 --- a/lib/PartnerAPI.php +++ b/lib/PartnerAPI.php @@ -18,6 +18,7 @@ class PartnerAPI private $curlOptions = array(); private $acceptLanguage; + private $maxRetries = 2; private $appName; private $appVersion; @@ -129,16 +130,43 @@ public function send($request) if ($this->acceptLanguage) { array_push($headers, 'Accept-Language: ' . $this->acceptLanguage); } - array_push($headers, 'User-Agent: ' . $this->computeUserAgent()); - return $this->clientInstance->request( - $request->getCallId(), - $request->getMethod(), - $this->apiBase . $request->getPath(), - $headers, - $request->getParams() + $request->getDefaultParams(), - $request->responseClass - ); + $retry = 0; + while (true) { + try { + return $this->clientInstance->request( + $request->getCallId(), + $request->getMethod(), + $this->apiBase . $request->getPath(), + $headers, + $request->getParams() + $request->getDefaultParams(), + $request->responseClass + ); + } catch (Error\ApiConnection $e) { + // Retry when timeout + if ($e->errno != CURLE_OPERATION_TIMEDOUT || $retry >= $this->maxRetries) { + throw $e; + } + } catch (Error\HttpRequest $e) { + if (array_key_exists('type', $e->response) + && $e->response['type'] === 'request_id_conflict') + { + throw new Error\RequestIdConflict( + $e->code, + $e->rawResponse, + $e->response, + $request->getParams()['request_id'] + ); + } + // Retry on 503 + if ($e->code != 503 || $retry >= $this->maxRetries) { + throw $e; + } + } + ++$retry; + sleep(3); + $request->setCallId(null); // Re-generate the call ID + } } } diff --git a/lib/Request/Base.php b/lib/Request/Base.php index 28b72fb4..d4cdefb2 100644 --- a/lib/Request/Base.php +++ b/lib/Request/Base.php @@ -24,7 +24,7 @@ public function getCallId() public function setCallId($newCallId) { - $this->callId = $newCallId; + $this->callId = $newCallId || Uuid::uuid4(); } public function getMethod() From a4822d45a898d35a0cc6a4c24449ec9c7865f528 Mon Sep 17 00:00:00 2001 From: Eitaro Fukamachi Date: Tue, 17 Sep 2024 07:11:31 +0000 Subject: [PATCH 2/4] Retry only non-POST requests or ones with `request_id`. --- lib/PartnerAPI.php | 18 +++++++++++++++--- lib/Request/Base.php | 9 +++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/PartnerAPI.php b/lib/PartnerAPI.php index 0fa20ae0..36da5f4e 100644 --- a/lib/PartnerAPI.php +++ b/lib/PartnerAPI.php @@ -144,12 +144,19 @@ public function send($request) $request->responseClass ); } catch (Error\ApiConnection $e) { + if (!$request->isRetriable()) + { + throw $e; + } + // Retry when timeout - if ($e->errno != CURLE_OPERATION_TIMEDOUT || $retry >= $this->maxRetries) { + if ($e->errno != CURLE_OPERATION_TIMEDOUT || $retry >= $this->maxRetries) + { throw $e; } } catch (Error\HttpRequest $e) { - if (array_key_exists('type', $e->response) + if (is_array($e->response) + && array_key_exists('type', $e->response) && $e->response['type'] === 'request_id_conflict') { throw new Error\RequestIdConflict( @@ -159,8 +166,13 @@ public function send($request) $request->getParams()['request_id'] ); } + if (!$request->isRetriable()) + { + throw $e; + } // Retry on 503 - if ($e->code != 503 || $retry >= $this->maxRetries) { + if ($e->code != 503 || $retry >= $this->maxRetries) + { throw $e; } } diff --git a/lib/Request/Base.php b/lib/Request/Base.php index d4cdefb2..69cef873 100644 --- a/lib/Request/Base.php +++ b/lib/Request/Base.php @@ -51,4 +51,13 @@ public function getParams() { return array(); } + + public function isRetriable() + { + $params = $this->getParams(); + return ( + $this->method != 'POST' + || (is_array($params) && array_key_exists('request_id', $params)) + ); + } } From 63eb34a04030545122ca434b16af13a374596b54 Mon Sep 17 00:00:00 2001 From: Eitaro Fukamachi Date: Wed, 18 Sep 2024 08:37:12 +0000 Subject: [PATCH 3/4] Retry only GET or PATCH. --- lib/Request/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Request/Base.php b/lib/Request/Base.php index 69cef873..0b2c55e1 100644 --- a/lib/Request/Base.php +++ b/lib/Request/Base.php @@ -56,7 +56,7 @@ public function isRetriable() { $params = $this->getParams(); return ( - $this->method != 'POST' + $this->method == 'GET' || $this->method == 'PATCH' || (is_array($params) && array_key_exists('request_id', $params)) ); } From 43c2ceb0f1a0455af8a48263c0c91d5052193025 Mon Sep 17 00:00:00 2001 From: Eitaro Fukamachi Date: Fri, 20 Sep 2024 07:03:57 +0000 Subject: [PATCH 4/4] Sleep only on 503. --- lib/PartnerAPI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/PartnerAPI.php b/lib/PartnerAPI.php index 36da5f4e..bb1613b5 100644 --- a/lib/PartnerAPI.php +++ b/lib/PartnerAPI.php @@ -175,9 +175,9 @@ public function send($request) { throw $e; } + sleep(3); } ++$retry; - sleep(3); $request->setCallId(null); // Re-generate the call ID } }