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

Rate Limiter #38

Open
jamesdoc opened this issue May 31, 2024 · 4 comments
Open

Rate Limiter #38

jamesdoc opened this issue May 31, 2024 · 4 comments
Assignees

Comments

@jamesdoc
Copy link

BullMQ offers the ability to set a rate limiter on a worker. Eg:

const worker = new Worker(
  "getFromApiQueue",
  async job => processJob(job),
  {
    connection: redisOpts,
    // call no more than once a minute
    limiter: { 
      max: 1,
      duration: 60000,
    },
  }
);

From the documentation here I'm not clear how to pass this configuration onto BullMQ. Is this possible at the moment?

@RomainLanz
Copy link
Owner

Hey @jamesdoc! 👋🏻

You can define this configuration inside the config/queue.ts file.

defineConfig({
  worker: {
    limiter: {
      // ...
    },
  },
})

@jamesdoc
Copy link
Author

jamesdoc commented Jun 3, 2024

Ahh, that makes sense and works well. Thank you @RomainLanz, however that appears to apply globally across all workers.

Is it then possible to apply different limiters to different workers? (eg for different API calls with different rate limits)

Eg - this simple proof of concept works as I expected it to outside of Adonis

const worker30s = new Worker(
  "oneInThirty",
  async job => processJob(job),
  {
    connection: redisOpts,
    // One job every 30 seconds
    limiter: {
      max: 1,
      duration: 30000,
    },
  }
);

const worker10s = new Worker(
  "oneInTen",
  async job => processJob(job),
  {
    connection: redisOpts,
    // One job every 10 seconds
    limiter: {
      max: 1,
      duration: 10000,
    },
  }
);

@RomainLanz
Copy link
Owner

It is not doable at the moment, I may add the possibility to define queue worker directly in the configuration, allowing you to configure then from here.

I need to think about it.

@jamesdoc
Copy link
Author

jamesdoc commented Jun 8, 2024

Thanks Romain. I appreciate the thought and response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants