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

Conversation

leonardoxc
Copy link

With this branch we can do this:

$memcache = new Memcache;
$memcache->connect('127.0.0.1',11211);

$rateLimiter = new RateLimiter($memcache, $_SERVER["REMOTE_ADDR"]);
try {
    // 10 requests / minute
    $rateLimiter->limitRequestsInMinutes(10, 1);
} catch (RateExceededException $e) {
    header("HTTP/1.0 529 Too Many Requests");
    exit;
}

try {
    // 30 requests / 5 minute
    $rateLimiter->limitRequestsInMinutes(30, 5);
} catch (RateExceededException $e) {
    header("HTTP/1.0 529 Too Many Requests");
    exit;
}

etc....

Copy link
Owner

@akirk akirk left a comment

Choose a reason for hiding this comment

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

Apart from the mix of coding standards, I am not sure about the logic you introduce. Why the subtraction part, instead of simply not counting it in the first place, if it had been visited in the current request already? So instead of $keysvisited we could do a $already_counted and skip the increment part when that has already been done.

private $prefix, $memcache;
private $prefix, $memcache , $keysvisited;
// 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.

$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

} 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 =.

}

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.

// php execution, we remove the previous additions so that the
// last call reports correct numbers
if ($this->keysvisited[$key]) {
$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 the key is read for a second or third tim in the same
// 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.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants