Options for throttling requests #1906
-
What problem are you trying to solve?I want to define a Describe the featureI think it would be nice if Maybe there is already a way to achieve this, but I failed at finding it through the docs, issues and discussions. Checklist
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
|
This can be implemented via an agent. |
Beta Was this translation helpful? Give feedback.
-
|
Not to doubt the validity of your answer, but it seems a bit steep. |
Beta Was this translation helpful? Give feedback.
-
|
It's not exactly the same as throttling, but I had to ensure a minimal delay between 2 requests, so I made this debouncing plugin: https://www.npmjs.com/package/got-plugin-debounce |
Beta Was this translation helpful? Give feedback.
-
|
Got doesn't have built-in throttling, but import PQueue from 'p-queue';
import got from 'got';
const queue = new PQueue({
intervalCap: 30, // Max 30 requests
interval: 60000, // Per 60 seconds
concurrency: 10 // Optional: max concurrent requests
});
// Create a throttled Got instance
const throttledGot = got.extend({
handlers: [
(options, next) => {
return queue.add(() => next(options));
}
]
});
// Use it like normal Got
await throttledGot('https://api.example.com/data').json();
// Also works with pagination
for await (const item of throttledGot.paginate('https://api.example.com/items')) {
console.log(item);
}This approach wraps all requests automatically, so you don't need to remember to throttle individual calls. The If you prefer a simpler per-request delay, you can use the |
Beta Was this translation helpful? Give feedback.
Got doesn't have built-in throttling, but
p-queueis the cleanest solution for this. You can create a wrapper that automatically throttles all requests: