iq

ioredis-quota

General-purpose quota management.

Showing:

Popularity

Downloads/wk

17

GitHub Stars

8

Maintenance

Last Commit

4yrs ago

Contributors

0

Package

Dependencies

1

Size (min+gzip)

74.7KB

License

MIT

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

Build Status NPM Version Dependency Status

ioredis-quota

General-purpose quota management.

This NPM package is a simple quota manager with a simple, promisified API. It's open-source and it's written with TypeScript.

The source code is available on GitHub where you can also find our issue tracker.

Install

The package depends on ioredis but it should also work with any other Redis library that supports promises.

$ npm install --save ioredis ioredis-quota

Getting Started

To make the code clean, the examples are written in TypeScript.

You start by some initialization code.

import Redis from "ioredis";
import { Quota } from "ioredis-quota";

const quota = new Quota({
  redis: new Redis(),
  rates: [
    { key: "github-api", unit: "minute", limit: 10 }, // Allow up to 10 requests per minute.
    { key: "github-api", unit: "hour", limit: 100 },  // Allow up to 100 requests per hour.
  ]
});

Use the grant() method to verify that the request you are making does not exceed the rate limit.

try {
  await quota.grant(); // Grant 10 requests per minute and 100 requests per hour.
  // We have not exceeded the minutely nor daily quota so we can execute a request.
} catch (e) {
  // We reached the limits. Use `e.nextDate` to handle a retry.
}

Or use the schedule() method which uses the grant() method and returns the next appropriate date for execution (e.nextDate) instead of throwing an error.

const nextDate = await quota.schedule(); // => Sat Jun 17 14:58:57 CEST 2017

API

Quota({ redis, prefix, rates })

A core class which is used for checking quota.

OptionTypeRequiredDefaultDescription
redisObjectYes-Redis class instance.
prefixStringNoquotaA string which prefix all the keys.
ratesObject[]No[]List of quota definitions.
rates.keyStringYes-Quota unique name.
rates.unitStringYes-Quota unit (second, minute, hour, day, week, month, quarter or year).
rates.limitIntegerYes-The maximum value of the increment.

QuotaError(nextDate, message)

Quota error class which is thrown when the grant method does not succeed.

OptionTypeRequiredDefaultDescription
nextDateDateYes-A moment when quota is reset.
messageStringNoQuota limit exceeded.Error message.

quota.buildIdentifier({ key, unit }): String

Builds and returns the final Redis key.

OptionTypeRequiredDefaultDescription
keyStringYes-Quota key name.
unitStringYes-Quota unit (second, minute, hour, day, week, month, quarter or year).

quota.flush([{ key, unit }]): Promise

Atomically removes quota. Note that if no attributes are specified then all identifiers are deleted.

OptionTypeRequiredDefaultDescription
keyStringYes-Quota key name.
unitStringYes-Quota unit (second, minute, hour, day, week, month, quarter or year).

quota.grant(rates): Promise

Atomically verifies quota for each rate and throws the QuotaError if the rate's value exceeds the specified limit attribute. Please note that this method will increment the counter for each defined rate by one per unit (if you specify two rate objects with the same unit this means that this unit will be incremented by 2).

OptionTypeRequiredDefaultDescription
ratesObject, Object[]No[]List of quota definitions (appends class rates).
rates.keyStringYes-Quota unique name.
rates.unitStringYes-Quota unit (second, minute, hour, day, week, month, quarter or year).
rates.limitIntegerYes-The maximum value of the increment.

quota.parseIdentifier(identifier): String

Parses the identifier string and returns key's data (prefix, timestamp and key).

OptionTypeRequiredDefaultDescription
identifierStringYes-Redis key.

quota.schedule(rates): Promise

Atomically verifies quota for each rate and returns the next available date.

OptionTypeRequiredDefaultDescription
ratesObject, Object[]No[]List of quota definitions (appends class rates).
rates.keyStringYes-Quota unique name.
rates.unitStringYes-Quota unit (second, minute, hour, day, week, month, quarter or year).
rates.limitIntegerYes-The maximum value of the increment.

License (MIT)

Copyright (c) 2016 Kristijan Sedlak <xpepermint@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100