Skip to content

The processRequest() function on CurlRequest.php has a logic issue. Need to update the logic. #185

Closed
@ghost

Description

Test Server Settings:
-- OS: CentOS Linux 8
-- Kernel: Linux 4.18.0-193.6.3.el8_2.x86_64
-- Http: Apache/2.4.37
-- PHP: PHP 7.4.7
-- Curl: curl 7.61.1

Shopify API Settings:
-- API Type: Admin API (Restful)
-- Test Configurations: Set it to get product counts continuously. Add var_dump inside the function in interest. Run the code.

$shopify = new ShopifySDK([
   'ShopUrl' => 'SHOP URL HERE',
   'ApiKey' => 'API KEY HERE',
   'Password' => 'API PASSWORD HERE'
]);

$i = 200;
while($i > 0) {
   $count = $shopify->Product->count();
   $i--;
}

File: CurlRequest.php
Funciton: Shown below

    /**
     * Execute a request, release the resource and return output
     *
     * @param resource $ch
     *
     * @throws CurlException if curl request is failed with error
     *
     * @return string
     */
    protected static function processRequest($ch)
    {
        # Check for 429 leaky bucket error
        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);

        self::$lastHttpResponseHeaders = $response->getHeaders();

        return $response->getBody();
    }

Problem Area: Shown below

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

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

Issues:

Issue 1: The response header name X-Shopify-Shop-Api-Call-Limit is not recognized. The header name must be in lowercase in order to get the proper value. Otherwise, the return value will be always null:

   $limitHeader = explode('/', $response->getHeader('x-shopify-shop-api-call-limit'), 2);

Issue 2: The header x-shopify-shop-api-call-limit does not always exist. After 40 counts of the bucket size, this header disappears from the response header list. Instead, the header retry-after will show up. Due to this response header behavior, the following code will never be called, since $limitHeader[1] will never be set when $lastHttpCode is 429.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions