energizer91 / smart-request-balancer

Smart request queue with fine tuning of rate and limit of requests
MIT License
97 stars 8 forks source link

Need some help; retry not working #20

Closed gruijter closed 4 years ago

gruijter commented 4 years ago

Rate limiting works, but retry doesn't. Why not? The makeRequest is a function that returns a promise. I do get the 'CONCURRENCY_LIMIT_EXCEEDED' sometimes, but retry is not executed.

// queue stuff
initQueue() {
    const config = {
        rules: {
            common: {
                rate: 3,
                limit: 1,
                priority: 1,
            },
        },
        overall: {
            rate: 10,
            limit: 1,
        },
        retryTime: 300,
        ignoreOverallOverheat: true,
    };
    const queue = new Queue(config);
}

queueMessage(path, msg) {
    const key = 'whatever';
    const rule = 'common';
    const requestHandler = (retry) => makeRequest(path, msg)
        .then((response) => response)
        .catch((error) => {
            if (error.message && error.message.includes('CONCURRENCY_LIMIT_EXCEEDED')) {
                return retry();
            }
            throw error;
        });
    return queue.request(requestHandler, key, rule);
}
gruijter commented 4 years ago

Ah wait, I now see the retry time is in seconds, not milliseconds.

So actually the retry is working!

Problem now is that it keeps retrying indefinitely when the rate limit is exceeded. I would like to have a limit on the number of retries. I.e. the promise rejects if after e.g. 3 retries it still does not succeed.

Is that possible? If not: please consider this as a feature request for maximum retries :)

energizer91 commented 4 years ago

hey @gruijter ! You're right, it's not possible now to make retry countdown. Sounds like a good feature request, thanks!