3.0.0 • Published 6 years ago

limitdb v3.0.0

Weekly downloads
99
License
ISC
Repository
github
Last release
6 years ago

Build Status

limitdb is a database for limits on top of leveldb.

Currently limitdb uses the Token Bucket Algorithm.

Installation

npm i limitdb

Configure

Create an instance of Limitdb as follows:

const Limitdb = require('limitdb');
const limitdb = new Limitdb({
  path: '/tmp/limitdb',
  types: {
    ip: {
      size: 10,
      per_second: 5
    }
  }
});

Options available:

  • path (string): a path for the leveldb database.
  • inMemory (boolean): set this to true to run the entire database in memory.
  • types (object): setup your bucket types.

The type defines the characteristics of a the bucket:

  • size is the maximun content of the bucket. This is the maximun burst you allow.
  • per_interval is the amount of tokens that the bucket receive on every interval.
  • interval defines the inverval in milliseconds.

You can also define your rates using per_second, per_minute, per_hour, per_day. So per_second: 1 is equivalent to per_interval: 1, interval: 1000.

If you omit size, limitdb assumes that size is the value of per_interval. So size: 10, per_second: 10 is the same than per_second: 10.

If you don't specify a filling rate with per_interval or any other per_x, the bucket is fixed and you have to manually reset it using PUT.

You can also define overrides inside your type definitions as follows:

const Limitdb = require('limitdb');
const limitdb = new Limitdb({
  path: '/tmp/limitdb',
  types: {
    ip: {
      size: 10,
      per_second: 5,
      overrides: {
        '127.0.0.1': {
          size: 100,
          per_second: 50
        }
      }
    }
  }
});

In this case the specific bucket for 127.0.0.1 of type ip will have a greater limit.

It is also possible to define overrides by regex:

overrides: {
  'local-ips': {
    match:      /192\.168\./
    size:       100,
    per_second: 50
  }
}

and also it is possible to configure expiration of overrides:

overrides: {
  '54.32.12.31': {
    size:       100,
    per_second: 50,
    until:      new Date(2016, 4, 1)
  }
}

TAKE

limitdb.take({ type: 'ip', key: '54.21.23.12' }, (err, result) => {
  console.dir(result);
});

limitdb.take takes as argument an object with:

  • type: the bucket type.
  • key: the identifier of the bucket.
  • count: the amount of tokens you need. This is optional and the default is 1.

The result object has:

  • conformant (boolean): true if the requested amount is conformant to the limit.
  • remaining (int): the amount of remaining tokens in the bucket.
  • reset (int / unix timestamp): unix timestamp of the date when the bucket will be full again.
  • limit (int): the size of the bucket.

PUT

You can manually reset a fill a bucket using PUT:

limitdb.put({ type: 'ip', key: '54.21.23.12' }, err => {});

limitdb.put takes as argument an object with:

  • type: the bucket type.
  • key: the identifier of the bucket.
  • count: the amount of tokens you want to put in the bucket. This is optional and the default is the size of the bucket.

STATUS

limitdb.status({ type: 'ip', prefix: '54' }, (err, result) => {
  console.dir(result.items);
});

limitdb.status takes as argument an object with

  • type: the bucket type.
  • prefix: a prefix of buckets to search.

The result object has:

  • items: an array of buckets with:
  • key: the key of the bucket.
  • size: the size of the bucket.
  • reset: the reset time.

Author

Auth0

License

This project is licensed under the MIT license. See the LICENSE file for more info.

3.0.0

6 years ago

2.10.0

6 years ago

2.9.0

6 years ago

2.7.0

7 years ago

2.6.1

7 years ago

2.6.0

7 years ago

2.5.0

7 years ago

2.4.0

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.0

7 years ago

2.1.7

8 years ago

2.1.6

8 years ago

2.1.5

8 years ago

2.1.4

8 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.5.1

9 years ago

1.5.0

9 years ago

1.4.0

9 years ago

1.3.1

9 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago