An open API service indexing awesome lists of open source software.

https://github.com/da440dil/js-counter

Distributed rate limiting on Node.js
https://github.com/da440dil/js-counter

distributed javascript nodejs rate rate-limit rate-limiter rate-limiting redis typescript

Last synced: 2 months ago
JSON representation

Distributed rate limiting on Node.js

Awesome Lists containing this project

README

          

# js-counter

[![Build Status](https://travis-ci.com/da440dil/js-counter.svg?branch=master)](https://travis-ci.com/da440dil/js-counter)
[![Coverage Status](https://coveralls.io/repos/github/da440dil/js-counter/badge.svg?branch=master)](https://coveralls.io/github/da440dil/js-counter?branch=master)

Distributed rate limiting using [Redis](https://redis.io/).

Supported Redis clients: [node-redis](https://github.com/NodeRedis/node-redis) v3 and v4, [ioredis](https://github.com/luin/ioredis) v4.

[Example](./examples/limiter.ts) usage with [node-redis](https://github.com/NodeRedis/node-redis) v4:
```typescript
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 => {
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
```

[Benchmarks](./benchmarks)
```
npm run file benchmarks/benchmark.ts
```