0.12.0 • Published 2 years ago
@da440dil/js-counter v0.12.0
js-counter
Distributed rate limiting using Redis.
Supported Redis clients: node-redis v3 and v4, ioredis v4.
Example usage with node-redis v4:
import { promisify } from 'util';
import { createClient } from 'redis';
import { createLimiter } from '@da440dil/js-counter';
const sleep = promisify(setTimeout);
async function main() {
const client = createClient();
await client.connect();
// Create limiter with 2 limits.
const limiter = createLimiter(
client,
// First limit: no more than 3 limiter calls within 1 second.
{ size: 1000, limit: 3 },
// Second limit: no more than 5 limiter calls within 2 seconds.
{ size: 2000, limit: 5 }
);
const key = 'key';
const limit = async (): Promise<void> => {
const result = await limiter.limit(key);
console.log(
'Result: { ok: %s, counter: %d, remainder: %d, ttl: %d }',
result.ok, result.counter, result.remainder, result.ttl
);
};
await Promise.all([limit(), limit(), limit(), limit()]);
await sleep(1000); // wait for the next window to start
await Promise.all([limit(), limit()]);
// Output:
// Result: { ok: true, counter: 1, remainder: 2, ttl: 1000 }
// Result: { ok: true, counter: 2, remainder: 1, ttl: 1000 }
// Result: { ok: true, counter: 3, remainder: 0, ttl: 1000 }
// Result: { ok: false, counter: 3, remainder: 0, ttl: 1000 }
// Result: { ok: true, counter: 5, remainder: 0, ttl: 978 }
// Result: { ok: false, counter: 5, remainder: 0, ttl: 978 }
await client.quit();
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
npm run file examples/limiter.ts
npm run file benchmarks/benchmark.ts
0.12.0
2 years ago
0.11.0
2 years ago
0.10.1
2 years ago
0.10.0
2 years ago
0.9.1
2 years ago
0.9.0
3 years ago
0.8.0
3 years ago
0.7.0
3 years ago
0.6.2
3 years ago
0.6.1
3 years ago
0.6.0
3 years ago
0.5.0
3 years ago
0.4.0
3 years ago
0.3.0
3 years ago
0.2.0
4 years ago
0.1.0
5 years ago
0.0.15
5 years ago
0.0.14
5 years ago
0.0.13
5 years ago
0.0.12
5 years ago
0.0.11
5 years ago
0.0.10
5 years ago
0.0.9
5 years ago
0.0.7
5 years ago
0.0.6
5 years ago
0.0.5
5 years ago
0.0.4
5 years ago
0.0.3
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago
0.0.0
5 years ago