Shopify GraphQL Admin API速率限制成本和睡眠时间 [英] Shopify GraphQL Admin API rate limiting cost and sleep time

查看:115
本文介绍了Shopify GraphQL Admin API速率限制成本和睡眠时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PHP(Laravel)中使用Shopify GraphQL API进行管理.
与REST api相比,GraphQL api中的速率限制和节流的工作方式不同,REST api是根据查询的 cost 计算得出的.

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:

  • 一个API调用(查询)的最大可用费用为1000.
  • 如果您消耗了1000点中的某些点,则每秒将恢复50点.
  • 如果您的存储桶中的成本点较少,而您查询的成本点高于该成本点,则会降低成本.

我要传递给api的查询的估计费用为 502 ,由 requestedQueryCost 表示.而 actualQueryCost 代表api针对特定商店返回的实际响应.

在上面的快照中,对于具有大量订单的商店,最坏的情况 requestedQueryCost 等于 acutalQueryCost .

现在,执行此查询时,我消耗了502点,还剩498点,经过1秒,添加了50点= 548 ,并且我可以进行第二次api调用以获取第二页数据.在第二次api调用后,我剩下的点数减少了,因此我将不得不 sleep 保持1或2秒钟,以获取要进行api调用的点数.

在快照所示的情况下,我必须放置 10秒睡眠等待时间才能恢复 500点以便进行下一个api调用.

问题:如何最好地确定不同商店的睡眠(等待)时间?我们不希望所有商店都等待10秒,即使它们的查询成本较低.

注意:有关代码参考,请参见下面的答案.

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.

推荐答案

下面是我的解决方案初稿,仍在寻找有关如何有效处理该问题的专家意见,这样每个商店都必须等待应得的时间,根据数据量.请指教.

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;
}

这篇关于Shopify GraphQL Admin API速率限制成本和睡眠时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆