GithubHelp home page GithubHelp logo

grinnz / redis-ratelimit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from semifor/redis-ratelimit

0.0 2.0 0.0 27 KB

Sliding window rate limiting with Redis storage

License: Other

Perl 82.58% Lua 17.42%

redis-ratelimit's Introduction

NAME

Redis::RateLimit - Sliding window rate limiting with Redis

Build Status

VERSION

version 1.0002

SYNOPSIS

use Redis;
use Redis::RateLimit;

my $rules = [
    { interval => 1, limit => 5 },
    { interval => 3600, limit => 1000, precision => 100 },
];

my $redis_client = Redis->new;
my $limiter = Redis::RateLimit->new(
    redis => $redis_client,
    rules => $rules,
);

for ( 1..10 ) {
    say 'Is rate limited? ', $limiter->incr('127.0.0.1') ? 'true' : 'false';
};

Output:

Is rate limited? false
Is rate limited? false
Is rate limited? false
Is rate limited? false
Is rate limited? false
Is rate limited? true
Is rate limited? true
Is rate limited? true
Is rate limited? true
Is rate limited? true

DESCRIPTION

A Perl library for efficient rate limiting using sliding windows stored in Redis.

This is a port of RateLimit.js without the non-blocking goodness.

Features

  • Uses a sliding window for a rate limit rule
  • Multiple rules per instance
  • Multiple instances of RateLimit side-by-side for different categories of users.
  • Whitelisting/blacklisting of keys

Background

See this excellent articles on how the sliding window rate limiting with Redis works:

For more information on the `weight` and `precision` options, see the second blog post above.

TODO

  • Port the middleware for Plack

ATTRIBUTES

redis

Redis client. If none is provided, a default is constructed for 127.0.0.1:6379.

prefix

A prefix to be included on each redis key. This prevents collisions with multiple applications using the same Redis DB. Defaults to 'ratelimit'.

client_prefix

Set this to a true value if using a Redis client that supports transparent prefixing. Defaults to 0.

rules

An arrayref of rules, each of which is a hashref with interval, limit, and optionally precision values.

METHODS

check($key | \@keys)

Returns true if any of the keys are rate limited.

incr($key | \@keys [, $weight ])

Returns true if any of the keys are rate limited, otherwise, it increments counts and returns false.

keys

Returns all of the rate limiter's with prefixes removed.

violated_rules($key | \@keys)

Returns a list of rate limit rules violated for any of the keys, or an empty list.

limited_keys($key | \@keys)

Returns a list of limited keys.

whitelist($key | \@keys)

Adds the keys to the whitelist so they are never rate limited.

unwhitelist($key | \@keys)

Removes the keys from the whitelist.

blacklist($key | \@keys)

Adds the keys to the blacklist so they are always rate limited.

unblacklist($key | \@keys)

Removes the keys from the blacklist.

AUTHOR

Marc Mims [email protected]

COPYRIGHT AND LICENSE

This software is Copyright (c) 2016 by Marc Mims.

This is free software, licensed under:

The MIT (X11) License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.