Git Product home page Git Product logo

ratelimit's Introduction

RateLimit

Build Status

PHP Rate Limiting Library With Token Bucket Algorithm with minimal external dependencies.

Installation

composer require palepurple/rate-limit

Storage Adapters

The RateLimiter needs to know where to get/set data.

Depending on which adapter you install, you may need to install additional libraries (predis/predis or tedivm/stash) or PHP extensions (e.g. Redis, Memcache, APC)

Example

require 'vendor/autoload.php';

use \PalePurple\RateLimit\RateLimit;
use \PalePurple\RateLimit\Adapter\APC as APCAdapter;
use \PalePurple\RateLimit\Adapter\Redis as RedisAdapter;
use \PalePurple\RateLimit\Adapter\Predis as PredisAdapter;
use \PalePurple\RateLimit\Adapter\Memcached as MemcachedAdapter;
use \PalePurple\RateLimit\Adapter\Stash as StashAdapter;


$adapter = new APCAdapter(); // Use APC as Storage
// Alternatives:
//
// $adapter = new RedisAdapter((new \Redis()->connect('localhost'))); // Use Redis as Storage
//
// $adapter = new PredisAdapter((new \Predis\Predis())->connect('localhost')); // Use Predis as Storage
//
// $memcache = new \Memcached();
// $memcache->addServer('localhost', 11211);
// $adapter = new MemcacheAdapter($memcache); 
//
// $stash = new \Stash\Pool(new \Stash\Driver\FileSystem());
// $adapter = new StashAdapter($stash);

$rateLimit = new RateLimit("myratelimit", 100, 3600, $adapter); // 100 Requests / Hour

$id = $_SERVER['REMOTE_ADDR']; // Use client IP as identity
if ($rateLimit->check($id)) {
  echo "passed";
} else {
  echo "rate limit exceeded";
}

Installing via Composer

curl -sS https://getcomposer.org/installer | php
composer.phar require palepurple/rate-limit

References

ratelimit's People

Contributors

bitbybit avatar davidgoodwin avatar icewild avatar touhonoob avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ratelimit's Issues

Which is it, PalePurple\RateLimit or touhonoob\RateLimit?

Your username is touhonoob, your README.md says to use PalePurple

use \PalePurple\RateLimit\RateLimit;
use \PalePurple\RateLimit\Adapter\APC as APCAdapter;
use \PalePurple\RateLimit\Adapter\Redis as RedisAdapter;
use \PalePurple\RateLimit\Adapter\Predis as PredisAdapter;
use \PalePurple\RateLimit\Adapter\Memcached as MemcachedAdapter;
use \PalePurple\RateLimit\Adapter\Stash as StashAdapter;

But the github.io site linked gives examples using touhonoob.

use \Touhonoob\RateLimit\RateLimit;
use \Touhonoob\RateLimit\Adapter\APC as RateLimitAdapterAPC;
use \Touhonoob\RateLimit\Adapter\Redis as RateLimitAdapterRedis;

And it seems that both the packages palepurple/ratelimit and touhonoob/ratelimit exist for composer. So what do I do?

Always getting `0` back fron ->check

I've tried creating a rate limit that only allows 1 request every 5 seconds, but i seem to always get 0 back from ->check(.

I have php-apcu installed and am using php-7.1.

My Code:

<?php

require 'vendor/autoload.php';

use \Touhonoob\RateLimit\RateLimit;
use \Touhonoob\RateLimit\Adapter\APCu as RateLimitAdapterAPCu;

$adapter = new RateLimitAdapterAPCu(); // Use APCu as Storage
$rateLimit = new RateLimit("myratelimit", 1, 5, $adapter); // 1 Requests / 5 seconds

$id = $_SERVER['REMOTE_ADDR']; // Use client IP as identity
if ($rateLimit->check($id) > 0) { // This always returns 0 so it's always exceeded?
  echo "passed";
} else {
  echo "rate limit exceeded";
}

Outdated Instructions for Predis

The instructions for using Predis is outdated. It should be the following:

$adapter = new PredisAdapter((new \Predis\Client('localhost')));

Bug in "1 per interval" case

$adapter = new RateLimitAdapterRedis();
$rateLimit = new RateLimit("myratelimit", 1, 3600, $adapter);

$id = 'foo';

if ($rateLimit->check($id) > 0) {
    echo "passed";
} else {
    echo "rate limit exceeded";
}

When I run this snippet I got "rate limit exceeded" message at first request.

Slim Middleware

I am using this in my Slim application as middleware.

For some reason, some of the requests get passed the "firewall".

RateLimit is not working as expected

Hi @touhonoob ,
Great work! I am just having a problem while setting this up. I am using this configuration and soemtime it's working and sometimes it not.

$limit_adapter = new PredisAdapter((new \Predis\Client('localhost')));
$rateLimit = new RateLimit("limit2", 100, 360, $limit_adapter); 
if(!empty($_SESSION['admin_id'])){
	$id = $_SESSION['admin_id']; 
}else{
	$id = $_SERVER['REMOTE_ADDR'];
}  
if($rateLimit->check($id)) {
	// passed
}else{ 
	 header('HTTP/1.1 429 Too many requests', true, 429);
}

When a user is blocked and he keeps reloading that tab. Then sometimes user gets passed and sometimes not passed even if the user is blocked. I don't know why this is happening can you please help me with that?

Thanks,
Rohit Kumar

Allow to instantiate RedisAdapter with connected Redis client

Now if I have already configured and connected Redis client, I need to create new and connect at RateLimitAdapterRedis
My idea is using prepared and connected Redis client such as:

$redis = new Redis();
$redis->pconnect('1.2.3.4', 6379);

$adapter = new RateLimitAdapterRedis($redis);
$rateLimit = new RateLimit("myratelimit", 100, 3600, $adapter);

@touhonoob what do you think about it? I can PR this, if you need.

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.