在阅读节流文档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 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
答案 0 :(得分: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
}
}