Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add external limiter support #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

colinskow
Copy link

This pull request allows Bee-Queue to integrate with any non-blocking external limiter library to throttle jobs. Recently I have been using Bottleneck to force jobs to wait until they are ready to run, but that ties up my concurrency and sometimes nothing runs. This is a much better solution!

PR includes:

  • 100% test coverage on new functionality
  • Detailed documentation
  • Updated Typescript types

Queue.process takes a function as an optional second argument, which if provided will be run just prior to executing each job. This allows you to query an external limiter and optionally reschedule the job if it is not cleared to run. The limiter query function helps the queue rapidly scan for jobs which are ready to run rather than tying up your concurrency with waiting jobs.

The limiter query function can use a callback or return a promise. The promise should resolve to an object which at minimum contains a boolean ready parameter, true if the job is ready to run.

limiterQueue.process(
  async (job) => {
    console.log(`Processing job ${job.id} after ${job.data.tries} tries`);
    // Do some work
  },
  async (job) => {
    const tries = job.data.tries + 1;
    const data = { ...job.data, tries };
    // You can plug in any non-blocking external rate limiter here
    const result = await limiter.check();
    if (result.ok) {
      return {
        ready: true,
        // (optional) this will replace the existing job data
        data
      }
    } else {
      return {
        ready: false,
        // (optional) this will replace the existing job data
        data,
        // (optional) reschedule the job to try again at this timestamp
        // if 0 or not specified, the job goes to the end of the waiting queue
        delayUntil: Date.now() + result.msUntilReady
      }
    }
  }
);

Additionally if the limiter check promise rejects or passes an error to the callback the job will be immediately failed with no retry logic.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling edc69f4 on colinskow:limiter into b9ffac9 on bee-queue:master.

@compwright compwright requested review from compwright and removed request for skeggse November 23, 2022 01:17
@compwright compwright changed the title Feature Proposal: External Limiter Support feat: add external limiter support Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants