Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions ratelimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,47 @@
class RateExceededException extends Exception {}

class RateLimiter {
private $prefix, $memcache;
private $prefix, $memcache , $keysvisited;
Copy link
Owner

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.

// how long should we keep memcache entries
public $maxMinutes=10;
Copy link
Owner

Choose a reason for hiding this comment

The 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";
Copy link
Owner

Choose a reason for hiding this comment

The 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();
Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The 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]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be checked with an isset() or it will generate a warning.

$requestsInCurrentMinute-=$this->keysvisited[$key];
Copy link
Owner

Choose a reason for hiding this comment

The 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;
Copy link
Owner

Choose a reason for hiding this comment

The 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>";
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}

Expand Down