Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

A rate-limiter written in TypeScript that supports annotations.

License

Notifications You must be signed in to change notification settings

binary-factory/rate-limiter

Repository files navigation

rate-limiter

Build Status Coverage Status Standard Version

A powerful rate-limiter with annotation and channel support written in TypeScript.

Features

  • Annotation support with @throttle()
  • Implements the token-bucket-algorythm. Exported as TokenBucket
  • Support of idependent configurable Channels that are isolated from each other.

Installation

npm install @binary-factory/rate-limiter

Usage

Simple with default channel

import { throttle, Channels } from '@binary-factory/rate-limiter';
class API {
    @throttle()
    static async request() {
        return 'OK';
    }
}
Channels.create(10, 'second');

Simple with named channels

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');

Advanced

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);

About

A rate-limiter written in TypeScript that supports annotations.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published