-
Notifications
You must be signed in to change notification settings - Fork 14
Call the rate limiter many times in the same php code with different limits #3
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
base: master
Are you sure you want to change the base?
Conversation
… different limits
akirk
left a comment
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.
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; |
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.
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 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(); |
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.
Please add whitespace around the =.
| 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 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; |
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.
Please add whitespace around the =.
| $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 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]; |
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.
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]) { |
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.
This should be checked with an isset() or it will generate a warning.
|
|
||
| class RateLimiter { | ||
| private $prefix, $memcache; | ||
| private $prefix, $memcache , $keysvisited; |
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.
With this branch we can do this:
etc....