touhonoob / ratelimit Goto Github PK
View Code? Open in Web Editor NEWPHP Rate Limiting Library With Token Bucket Algorithm
Home Page: http://touhonoob.github.io/RateLimit/
PHP Rate Limiting Library With Token Bucket Algorithm
Home Page: http://touhonoob.github.io/RateLimit/
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";
}
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
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.
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?
$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.
I cant use this on my PHP 7 install as it does not support APCu
I ran a quick test and seems allowance won't be handled correctly under concurrency.
The instructions for using Predis is outdated. It should be the following:
$adapter = new PredisAdapter((new \Predis\Client('localhost')));
I am using this in my Slim application as middleware.
For some reason, some of the requests get passed the "firewall".
The instructions for using Predis is outdated. It should be the following:
$adapter = new PredisAdapter((new \Predis\Client('localhost')));
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.