Rate limiting
Last updated
Was this helpful?
Was this helpful?
import { WorkerPro } from '@taskforcesh/bullmq-pro';
const worker = new WorkerPro('myQueue', processFn, {
group: {
limit: {
max: 100, // Limit to 100 jobs per second per group
duration: 1000,
}
},
connection
});import { WorkerPro } from '@taskforcesh/bullmq-pro';
const worker = new WorkerPro(
'myQueue',
async job => {
const groupId = job.opts.group.id;
const [isRateLimited, duration] = await doExternalCall(groupId);
if (isRateLimited) {
await worker.rateLimitGroup(job, duration);
// Do not forget to throw this special exception,
// since the job is no longer active after being rate limited.
throw Worker.RateLimitError();
}
},
{
connection,
},
);import { QueuePro } from '@taskforcesh/bullmq-pro';
const queue = new QueuePro('myQueue', { connection });
const groupId = '0';
const maxJobs = 100;
const ttl = await queue.getGroupRateLimitTtl(groupId, maxJobs);
if (ttl > 0) {
console.log('Group is rate limited');
}