-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Call the rate limiter many times in the same php code with different limits #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,26 +27,47 @@ | |
class RateExceededException extends Exception {} | ||
|
||
class RateLimiter { | ||
private $prefix, $memcache; | ||
private $prefix, $memcache , $keysvisited; | ||
// how long should we keep memcache entries | ||
public $maxMinutes=10; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add whitespace around the |
||
|
||
public function __construct(Memcache $memcache, $ip, $prefix = "rate") { | ||
$this->memcache = $memcache; | ||
if (!$memcache) { | ||
echo "Problem connecting to memcache server"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In accordance with the rest of the code, this should not output something but throw an exception. |
||
exit; | ||
} | ||
$this->prefix = $prefix . $ip; | ||
$keysvisited=array(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add whitespace around the |
||
} | ||
|
||
public function limitRequestsInMinutes($allowedRequests, $minutes) { | ||
$requests = 0; | ||
|
||
foreach ($this->getKeys($minutes) as $key) { | ||
$requestsInCurrentMinute = $this->memcache->get($key); | ||
|
||
// if the key is read for a second or third tim in the same | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo |
||
// php execution, we remove the previous additions so that the | ||
// last call reports correct numbers | ||
if ($this->keysvisited[$key]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be checked with an |
||
$requestsInCurrentMinute-=$this->keysvisited[$key]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add whitespace around the |
||
} | ||
if (false !== $requestsInCurrentMinute) $requests += $requestsInCurrentMinute; | ||
} | ||
|
||
if (false === $requestsInCurrentMinute) { | ||
$this->memcache->set($key, 1, 0, $minutes * 60 + 1); | ||
if (! $this->keysvisited[$key] ) { | ||
if (false === $requestsInCurrentMinute) { | ||
$this->memcache->set($key, 1, 0, $this->maxMinutes * 60 + 1); | ||
} else { | ||
$this->memcache->increment($key, 1); | ||
} | ||
$this->keysvisited[$key]=1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add whitespace around the |
||
} else { | ||
$this->memcache->increment($key, 1); | ||
$this->keysvisited[$key]++; | ||
} | ||
|
||
echo " You already have $requests requests in $minutes min<BR>"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This library should not output anything. This text could be placed in the Exception description, though. |
||
if ($requests > $allowedRequests) throw new RateExceededException; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra whitespace ahead of the comma.