0.0.1 • Published 6 years ago

@kuroismith/ratelimiter v0.0.1

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

RateLimiter

A rate limiter based on token bucket algorithm using redis

Usage

  // redis client
  let client: RedisClient = redis.createClient({host: "127.0.0.1"});

  // In every 20000 milliseconds, the resource can be access 100 times
  const policy: RateLimiterPolicy = new SimpleRateLimiterPolicy({
    interval: 20000,
    capacity: 100,
    maxBurstTime: 1000
  });

  const limiter: RateLimiter = new RedisRateLimiter(policy, client);

  async action(userId) {
    try {
        const accessible = await limiter.access(userId);
        if (accessible) {
          // allowed
        } else {
          // limit is exceeded, action is not allowed
        }
    } catch (e) {
        // redis failed or similar
    }
  }