Shopify API の Rate Limit の参考情報

Shopify API の Rate Limit を考慮するための参考情報をご紹介します。

shopify

背景 Shopify API には Rate Limit がある

Shopify 公式ドキュメント Shopify API rate limits の通り Shopify API には Rate Limit があります。

Shopify API Node.js (Official module)

自分の場合は shopify-api-node という npm を利用しているので、これの autoLimit オプションを忘れずに指定しています。

autoLimit – Optional – This option allows you to regulate the request rate in order to avoid hitting the rate limit. Requests are limited using the token bucket algorithm. Accepted values are a boolean or a plain JavaScript object. When using an object, the calls property and the interval property specify the refill rate and the bucketSize property the bucket size. For example { calls: 2, interval: 1000, bucketSize: 35 } specifies a limit of 2 requests per second with a burst of 35 requests. When set to true requests are limited as specified in the above example. Defaults to false.

引用元: Options – MONEI/Shopify-api-node at 3.6.10

内部処理で stopcock を利用している

shopify-api-node/index.js#L74-L84 にて stopcock という npm を利用しているので、もし興味があればこれも併せてみてみると良いかもです。

  if (options.autoLimit) {
    const conf = transform(
      options.autoLimit,
      (result, value, key) => {
        if (key === 'calls') key = 'limit';
        result[key] = value;
      },
      { bucketSize: 35 }
    );

    this.request = stopcock(this.request, conf);

Limit the execution rate of a function using the token bucket algorithm. Useful for scenarios such as REST APIs consumption where the amount of requests per unit of time should not exceed a given threshold.

トークンバケットアルゴリズムを使用して、関数の実行速度を制限します。単位時間あたりのリクエスト数が特定の閾値を超えてはならないRESTAPIの消費などのシナリオに役立ちます。

引用元: stopcock – npm

以上、Shopify API の Rate Limit と上手く付き合いながら開発していきたい、現場からお送りしました。