Amazon MWS - Request Throttled



阅读节流文档后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

我想我已经弄清楚了。 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
  }
}

最新更新