在阅读了节流文档https://docs.developer.amazonservices.com/en_US/products/Products_Throttling.html和https://docs.developer.amazonservices.com/en_US/dev_guide/DG_Throttling.html之后,我开始使用quotaRemaining和quotaResetsAt响应头,这样就不会超出引用限制。但是,每当我在快速连续的时间内触发几个请求时,我就会得到以下异常。
文档中没有提到任何关于突发限制的内容。它谈到了最大请求配额,但我不知道这如何适用于我的情况。我正在调用ListMatchingProducts应用程序接口
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发布于 2019-02-17 18:47:03
我想我想通了。ListMatchingProducts提到最大请求配额是20。实际上,这意味着您可以快速连续地触发最多20个请求,但在此之后,您必须等待,直到恢复速率“补充”您的请求“信用”(即在我的情况下,每5秒1个请求)。
此恢复速率将(每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
}
}https://stackoverflow.com/questions/54731559
复制相似问题