1.0.2 • Published 6 years ago

bucket-decorator v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Bucket Decorator

Bucket Decorator is a npm module for help you to use safe web services or other stuff without getting api limit error (Too many requests)

  • Intended to use with webServices with traffic policy with Leaky Bucket algorithm
  • No need to change your current implementation, just decorate used methods
  • Magic

Requirements

  • reflect-metadata module

Installation

  • npm install bucket-decorator

Usage

  • Import the module:
import { Bucket } from 'bucket-decorator';
  • Decorate your method:
class MyAwesomeClass {
    ...
    @Bucket.Limiter({
        limit: 40,
        leakRate: 2,
        limitKey: 'my namespace',
    })
    myAwesomeMethod(@Bucket.LimitKey() token){...}
    ...
}

API Reference

@Bucket.Limiter(options: BucketOptionsInterface)

You can decorate any method | Property | type | Default value | Description | --- | --- | --- | --- | limit | number | 100 | Maxim 'bucket' size. Method calls what 'get' into bucket are executed instantly. Other ones are in queue. | leakRate | number | 10 | Leak rate for your bucket. Every second 'bucket' will have place for more leakRate calls. | limitKey | string | '' | Is optional. You can have multiple 'buckets' with different 'names'.

@Bucket.LimitKey()

You can decorate any string propery of your method. This will override BucketOptionsInterface.limitKey option

Example

import 'reflect-metadata';

import {
    Bucket
} from 'bucket-decorator';


class Test {

    @Bucket.Limiter({
        leakRate: 2,
        limit: 40
    })
    doMyWork( @Bucket.LimitKey() myToken) {

        console.log(`Working for ${myToken}`);
    }
}


(async () => {

    let testInstance = new Test();
    for (let i = 0; i < 100; i++) {

        await testInstance.doMyWork('my-secret-token');
    }
})();

Building

  • npm install or yarn
  • npm run start or yarn start

Changelog

1.0.2

  • Add default values for BucketOptionsInterface
  • Fix queue for function calls

1.0.1 - Initial Release