Shopify GraphQL Admin API rate limiting cost and sleep time

筅森魡賤 提交于 2021-02-10 06:14:35

问题


I am trying to consume Shopify GraphQL API for Admin in PHP ( Laravel ).
Rate limiting and throttling works differently in GraphQL api as compared to REST api, its calculated based on the cost of the query.



Few points to keep in mind:

  • Maximum available cost is 1000 for one api call (query).
  • If you have consumed some points from 1000, every second, 50 points will be restored.
  • If you have less points of cost in your bucket, and you make a query of cost higher than that, it will throttle.

The query that I am passing to api has an estimated cost of 502, represented by requestedQueryCost. Whereas, actualQueryCost represents the actual response returned by the api for a specific shop.

In above snapshot, its the worst case scenario, requestedQueryCost is equal to acutalQueryCost for a store with heavy number of orders.

Now, when this query is executed I have consumed 502 points, 498 left, 1 second elapsed, 50 points added = 548 , and I can make a second api call to fetch second page of data. After second api call I will have less points left, so I will have to put sleep for 1 or 2 seconds to gain the points to make api call.

In the case shown in snapshot, i had to put 10 seconds sleep wait in order to restore 500 points to make next api call.

Problem: How to best decide sleep (wait) time for different shops? We don't want all shops to wait for 10 seconds even if they have less query cost.

Note: For code reference, see my answer below.


回答1:


You get a clean slate of cost calls per shop. If one shop is at zero, you may still have 1000 waiting for you on another shop. You should ensure your calling mechanism is clear on that! Only sleep a thread per shop. You should be able to assign a request to a thread, so that if it sleeps you are still operating with other threads. I would be laughing my ass off if PHP operated with one thread assigned to all requests. That would be so 1982 computing!




回答2:


Below is my rough draft of a solution, still looking for expert opinion on how to effectively handle it so that each Shop has to wait for the time they deserve, according to the amount of data. Please advise.

public function getRequiredOrders()
{
    $firstRequestTimeStamp = now();
    $ordersGraph = $this->shop->api()->graph($this->firstQuery())->body->orders;
    $this->transform($ordersGraph); //transforming to required format

    $previousRequestTimeStamp = $firstRequestTimeStamp;

    while($ordersGraph->pageInfo->hasNextPage) {

        $nextRequestTimeStamp = now();
        $timeElapsed = $nextRequestTimeStamp->diffInSeconds($previousRequestTimeStamp);
        $restoredPoints = $timeElapsed * 50; //50 points are restored every 1 second
        $pointsLeft = $this->shop->api()->getApiCalls('graph', 'left');
        $totalPointsLeft = $pointsLeft + $restoredPoints;

        if($totalPointsLeft >=502){ //one must know the maximum cost of their query
            $lastEdgeCursor = end($ordersGraph->edges)->cursor;
            $nextQuery = $this->nextQuery($lastEdgeCursor);
            $previousRequestTimeStamp = $nextRequestTimeStamp;
            $ordersGraph = $this->shop->api()->graph($nextQuery)->body->orders;
            $this->transform($ordersGraph);
        }else{
            sleep(1);
            continue;
        }

    }
    return $this->allOrders;
}


来源:https://stackoverflow.com/questions/53469612/shopify-graphql-admin-api-rate-limiting-cost-and-sleep-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!