Skip to content
Discussion options

You must be logged in to vote

Got doesn't have built-in throttling, but p-queue is the cleanest solution for this. You can create a wrapper that automatically throttles all requests:

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.pa…

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@paya-cz
Comment options

@paya-cz
Comment options

Comment options

You must be logged in to vote
1 reply
@szmarczak
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #1905 on October 25, 2021 19:24.