Skip to content

Fix new resource rate limit issue #82

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 2 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
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
29 changes: 20 additions & 9 deletions lib/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


use PHPShopify\Exception\CurlException;
use PHPShopify\Exception\ResourceRateLimitException;

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -47,6 +48,7 @@ protected static function init($url, $httpHeaders = array())
//Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHPClassic/PHPShopify');

$headers = array();
Expand Down Expand Up @@ -143,23 +145,32 @@ public static function delete($url, $httpHeaders = array())
protected static function processRequest($ch)
{
# Check for 429 leaky bucket error
while(1) {
$output = curl_exec($ch);
self::$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if(self::$lastHttpCode != 429) {
break;
}
usleep(500000);
while (1) {
$output = curl_exec($ch);
$response = new CurlResponse($output);

self::$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (self::$lastHttpCode != 429) {
break;
}

$limitHeader = explode('/', $response->getHeader('X-Shopify-Shop-Api-Call-Limit'), 2);

if (isset($limitHeader[1]) && $limitHeader[0] < $limitHeader[1]) {
throw new ResourceRateLimitException($response->getBody());
}

usleep(500000);
}

if (curl_errno($ch)) {
throw new Exception\CurlException(curl_errno($ch) . ' : ' . curl_error($ch));
}

// close curl resource to free up system resources
curl_close($ch);

return $output;
return $response->getBody();
}

}
73 changes: 73 additions & 0 deletions lib/CurlResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace PHPShopify;

class CurlResponse
{
/** @var array */
private $headers = [];
/** @var string */
private $body;

public function __construct($response)
{
$this->parse($response);
}

/**
* @param string $response
*/
private function parse($response)
{
$response = \explode("\r\n\r\n", $response);
if (\count($response) > 1) {
// We want the last two parts
$response = \array_slice($response, -2, 2);
list($headers, $body) = $response;
foreach (\explode("\r\n", $headers) as $header) {
$pair = \explode(': ', $header, 2);
if (isset($pair[1])) {
$this->headers[$pair[0]] = $pair[1];
}
}
} else {
$body = $response[0];
}

$this->body = $body;
}

/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}

/**
* @param string $key
*
* @return string
*/
public function getHeader($key)
{
return isset($this->headers[$key]) ? $this->headers[$key] : null;
}

/**
* @return string
*/
public function getBody()
{
return $this->body;
}

public function __toString()
{
$body = $this->getBody();
$body = $body ? : '';

return $body;
}
}
8 changes: 8 additions & 0 deletions lib/Exception/ResourceRateLimitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PHPShopify\Exception;

class ResourceRateLimitException extends ApiException
{

}