亚马逊MWS - 请求限制

问题描述 投票:1回答:1

在阅读了Throttling文档https://docs.developer.amazonservices.com/en_US/products/Products_Throttling.htmlhttps://docs.developer.amazonservices.com/en_US/dev_guide/DG_Throttling.html之后,我开始尊重quotaRemainingquotaResetsAt响应标题,这样我就不会超出报价限制。但是,每当我快速连续发出一些请求时,我都会遇到以下异常。

文档没有提到任何爆发限制。它谈到最大请求配额,但我不知道这是如何适用于我的情况。我正在调用ListMatchingProducts api

Caused by: com.amazonservices.mws.client.MwsException: Request is throttled
    at com.amazonservices.mws.client.MwsAQCall.invoke(MwsAQCall.java:312)
    at com.amazonservices.mws.client.MwsConnection.call(MwsConnection.java:422)
    ... 19 more
amazon-mws
1个回答
0
投票

我想我想通了。 ListMatchingProducts提到最大请求配额是20.实际上这意味着您可以快速连续发出最多20个请求,但之后您必须等到恢复率“补充”您的请求“信用”(即在我的情况下1请求)每5秒钟)。

此恢复速率将(每5秒)开始然后重新填充配额,最多20个请求。以下代码对我有用......

class Client {
  private final int maxRequestQuota = 19
  private Semaphore maximumRequestQuotaSemaphore = new Semaphore(maxRequestQuota)
  private volatile boolean done = false

  Client() {
    new EveryFiveSecondRefiller().start()
  }

  ListMatchingProductsResponse fetch(String searchString) {
    maximumRequestQuotaSemaphore.acquire()
    // .....
  }

  class EveryFiveSecondRefiller extends Thread {
    @Override
    void run() {
      while (!done()) {
        int availablePermits = maximumRequestQuotaSemaphore.availablePermits()
        if (availablePermits == maxRequestQuota) {
          log.debug("Max permits reached. Waiting for 5 seconds")
          sleep(5000)
          continue
        }
        log.debug("Releasing a single permit. Current available permits are $availablePermits")
        maximumRequestQuotaSemaphore.release()
        sleep(5000)
      }
    }

    boolean done() {
      done
    }
  }

  void close() {
    done = true
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.