beardon / mws-api

Amazon Marketplace Web Services client for Node.js.
79 stars 55 forks source link

Error: Request is throttled #99

Open MantasR opened 6 years ago

MantasR commented 6 years ago

It doesn't happen all the time, however since I have imports of orders on cron job (every 3 minutes) I sometimes find my server crashed with this error:

{ Error: Request is throttled
    at Promise.try (/home/api/e-shop-sync/node_modules/mws-api/lib/client.js:144:33)
    at tryCatcher (/home/api/e-shop-sync/node_modules/bluebird/js/release/util.js:16:23)
    at Function.Promise.attempt.Promise.try (/home/api/e-shop-sync/node_modules/bluebird/js/release/method.js:39:29)
    at request.then (/home/api/e-shop-sync/node_modules/mws-api/lib/client.js:140:31)
    at tryCatcher (/home/api/e-shop-sync/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/api/e-shop-sync/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/api/e-shop-sync/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/api/e-shop-sync/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/api/e-shop-sync/node_modules/bluebird/js/release/promise.js:693:18)
    at Promise._fulfill (/home/api/e-shop-sync/node_modules/bluebird/js/release/promise.js:638:18)
    at Request._callback (/home/api/e-shop-sync/node_modules/bluebird/js/release/nodeback.js:42:21)
    at Request.self.callback (/home/api/e-shop-sync/node_modules/request/request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:194:7)
    at Request.<anonymous> (/home/api/e-shop-sync/node_modules/request/request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:191:7)
    at IncomingMessage.<anonymous> (/home/api/e-shop-sync/node_modules/request/request.js:1091:12)
    at Object.onceWrapper (events.js:293:19)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12) code: 'RequestThrottled', type: {}
}

Here is my setup:

    if (!marketplaceCredentials.other.meta) {

        marketplaceCredentials.other.meta = {
            retry: true, // retry requests when throttled
            next: true, // auto-paginate
            limit: Infinity // only get this number of items (NOT the same as MaxRequestsPerPage)
        }

    }

    this.mws = new MWSClient(marketplaceCredentials.other);

And here is where I am getting orders:

    if (!query) {
        return Promise.reject("Query not provided");
    }

    if (!query.fromDate) {
        return Promise.reject("From date not provided");
    }

    return new Promise((resolve, reject) => {

        this.mws.Orders.ListOrders({
            MarketplaceId: this.credentials.other.marketplaceId,
            LastUpdatedAfter: new Date(query.fromDate),
        }).then(({result, metadata}) => {

            let that = this;

            console.log("getOrders => orders.length", result.length);

            return Promise.map(result, function (orderItem, index) {

                return that.mws.Orders.ListOrderItems({AmazonOrderId: orderItem.AmazonOrderId}).then(res => {
                    console.log("orderItem.AmazonOrderId", orderItem.AmazonOrderId);
                    orderItem.listOrderItems = res.result;
                    return Promise.resolve(orderItem);
                });

            }, {concurrency: 5});

        }).then(function (result) {

            resolve(result);

        }).catch(error => {
            console.log(error);
            reject(error);
        });

    });
eli8levit commented 6 years ago

The reason is that amazon has maximum request quota of requests you are able to send. More info: amazon doc

eli8levit commented 6 years ago

I have a problem related to this issue

When amount of orders is more then 600 there is now way to get all orders at once because the request is throttled. maxResultsPerPage value must be less than 100 so in order to get 600 orders it has to be 6 requests to amazon when maximum request quota is 6: Amazon doc How to solve this problem??

l290347877 commented 4 years ago

I have a problem related to this issue

When amount of orders is more then 600 there is now way to get all orders at once because the request is throttled. maxResultsPerPage value must be less than 100 so in order to get 600 orders it has to be 6 requests to amazon when maximum request quota is 6: Amazon doc How to solve this problem??

How to solve this problem, "nextToken" is filtered out