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

Add Composer & PSR-4 support #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ The class works around the problem that the timeframe is constantly moving, i.e.

The code is released under an MIT license.

Usage
Installation
-----
composer require akirk/php-ratelimiter

Usage
-----
```php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"]);
$rateLimiter = new \Akirk\Ratelimiter\Ratelimiter(new \Memcache(), $_SERVER["REMOTE_ADDR"]);
try {
// allow a maximum of 100 requests for the IP in 5 minutes
$rateLimiter->limitRequestsInMinutes(100, 5);
} catch (RateExceededException $e) {
} catch (\Akirk\Ratelimiter\RateExceededException $e) {
header("HTTP/1.0 529 Too Many Requests");
exit;
}
Expand All @@ -30,10 +33,11 @@ If you want to protect multiple resources with different limits, use the third p

```php
// script1.php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"], "script1");
$rateLimiter = new \Akirk\Ratelimiter\Ratelimiter(new \Memcache(), $_SERVER["REMOTE_ADDR"], "script1");
try { ... }

// script2.php
$rateLimiter = new RateLimiter(new Memcache(), $_SERVER["REMOTE_ADDR"], "script2");
$rateLimiter = new \Akirk\Ratelimiter\Ratelimiter(new \Memcache(), $_SERVER["REMOTE_ADDR"], "script2");
try { ... }
```

Expand Down
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "akirk/php-ratelimiter",
"description": "A small class that uses Memcache to allow only a certain number of requests per a certain amount of minutes.",
"type": "library",
"license": "MIT",
"minimum-stability": "stable",
"require": {
"php": ">=5.5",
"ext-memcache": "*"
},
"autoload": {
"psr-4": {
"Akirk\\Ratelimiter\\": "src/"
}
}
}
42 changes: 6 additions & 36 deletions ratelimiter.php → src/RateExceededException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
Copyright (c) 2013 Alexander Kirk
http://alexander.kirk.at/
Expand All @@ -24,39 +25,8 @@

*/

class RateExceededException extends Exception {}

class RateLimiter {
private $prefix, $memcache;
public function __construct(Memcache $memcache, $ip, $prefix = "rate") {
$this->memcache = $memcache;
$this->prefix = $prefix . $ip;
}

public function limitRequestsInMinutes($allowedRequests, $minutes) {
$requests = 0;

foreach ($this->getKeys($minutes) as $key) {
$requestsInCurrentMinute = $this->memcache->get($key);
if (false !== $requestsInCurrentMinute) $requests += $requestsInCurrentMinute;
}

if (false === $requestsInCurrentMinute) {
$this->memcache->set($key, 1, 0, $minutes * 60 + 1);
} else {
$this->memcache->increment($key, 1);
}

if ($requests > $allowedRequests) throw new RateExceededException;
}

private function getKeys($minutes) {
$keys = array();
$now = time();
for ($time = $now - $minutes * 60; $time <= $now; $time += 60) {
$keys[] = $this->prefix . date("dHi", $time);
}

return $keys;
}
}
namespace Akirk\Ratelimiter;

use Exception;

class RateExceededException extends Exception { }
77 changes: 77 additions & 0 deletions src/Ratelimiter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
Copyright (c) 2013 Alexander Kirk
http://alexander.kirk.at/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

namespace Akirk\Ratelimiter;

use Memcache;

class RateLimiter
{
private $prefix;
private $memcache;

public function __construct(Memcache $memcache, $ip, $prefix = "rate")
{
$this->memcache = $memcache;
$this->prefix = $prefix . $ip;
}

public function limitRequestsInMinutes($allowedRequests, $minutes)
{
$requests = 0;

foreach ($this->getKeys($minutes) as $key) {
$requestsInCurrentMinute = $this->memcache->get($key);

if (false !== $requestsInCurrentMinute) {
$requests += $requestsInCurrentMinute;
}
}

if (false === $requestsInCurrentMinute) {
$this->memcache->set($key, 1, 0, $minutes * 60 + 1);
} else {
$this->memcache->increment($key, 1);
}

if ($requests > $allowedRequests) {
throw new RateExceededException;
}
}

private function getKeys($minutes)
{
$keys = [];

$now = time();
for ($time = $now - $minutes * 60; $time <= $now; $time += 60) {
$keys[] = $this->prefix . date("dHi", $time);
}

return $keys;
}
}