A powerful rate-limiter with annotation and channel support written in TypeScript.
- Annotation support with
@throttle()
- Implements the token-bucket-algorythm. Exported as
TokenBucket
- Support of idependent configurable
Channels
that are isolated from each other.
npm install @binary-factory/rate-limiter
import { throttle, Channels } from '@binary-factory/rate-limiter';
class API {
@throttle()
static async request() {
return 'OK';
}
}
Channels.create(10, 'second');
import { Channels, throttle } from '@binary-factory/rate-limiter';
class API {
@throttle('google-places')
static async requestToPlaces() {
return 'OK';
}
@throttle('google-translate')
static async requestToAnother() {
}
}
Channels.create('google-places', 10, 'second');
Channels.create('google-translate', 20, 'hour');
import { Channel, Channels, throttle } from '@binary-factory/rate-limiter';
class API {
@throttle({
channel: 'google-places',
cost: 5,
ttl: 5000 // Drop whether we have to wait more than 5secs.
})
static async requestToPlaces() {
return 'OK';
}
@throttle({
channel: 'google-places',
cost: 5,
ttl: 5000 // Drop whether we have to wait more than 5secs.
})
static async requestToPlacesRich() {
}
}
let channel = new Channel({
interval: 1000,
bucketSize: 25,
tokensPerInterval: 10,
tokens: 25
});
Channels.add(channel);