How to use the async-sema.RateLimit function in async-sema

To help you get started, we’ve selected a few async-sema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zeit / async-sema / examples / rate-limiting.js View on Github external
async function h() {
	console.log('Uniform distribution of requests over time');
	const n = 50;
	const lim = RateLimit(5, { uniformDistribution: true });
	const start = process.hrtime();

	for (let i = 0; i < n; i++) {
		await lim();
		process.stdout.write('.');
	}
	process.stdout.write('\n');

	const hrt = process.hrtime(start);
	const elapsed = (hrt[0] * 1000 + hrt[1] / 1e6) / 1000;
	const rps = n / elapsed;
	console.log(rps.toFixed(3) + ' req/s');
}
github zeit / async-sema / examples / rate-limiting.js View on Github external
async function g() {
	console.log('Custom rate limit time unit');

	const n = 20;
	const lim = RateLimit(5, { timeUnit: 60 * 1000 });
	const start = process.hrtime();

	for (let i = 0; i < n; i++) {
		await lim();
		process.stdout.write('.');
	}
	process.stdout.write('\n');

	const hrt = process.hrtime(start);
	const elapsed = (hrt[0] * 1000 + hrt[1] / 1e6) / 1000;
	const rps = n / (elapsed / 60);
	console.log(rps.toFixed(3) + ' req/min');
}
github zeit / async-sema / examples / rate-limiting.js View on Github external
async function f() {
	console.log('Naive requests per second rate limiting');

	const n = 50;
	const lim = RateLimit(5);
	const start = process.hrtime();

	for (let i = 0; i < n; i++) {
		await lim();
		process.stdout.write('.');
	}
	process.stdout.write('\n');

	const hrt = process.hrtime(start);
	const elapsed = (hrt[0] * 1000 + hrt[1] / 1e6) / 1000;
	const rps = n / elapsed;
	console.log(rps.toFixed(3) + ' req/s');
}

async-sema

Semaphore using `async` and `await`

MIT
Latest version published 3 years ago

Package Health Score

74 / 100
Full package analysis