Skip to content

Commit

Permalink
Merge da97ddc into ba5eba8
Browse files Browse the repository at this point in the history
  • Loading branch information
sndsgd committed Feb 1, 2017
2 parents ba5eba8 + da97ddc commit cfb4f21
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -27,25 +27,26 @@ Install `sndsgd/rate` using [Composer](https://getcomposer.org/).
```php
# define the rate limits
$clientIp = $di["request"]->getClientIp();
$clientIp = $di->getClient()->getIp();
$limits = [
new \sndsgd\rate\Limit("Search-PerSecond", $clientIp, 1, 3),
new \sndsgd\rate\Limit("Search-PerHour", $clientIp, 600, 3600),
];

# create a limiter, and increment the hit counts for all limits
$limiter = new \sndsgd\rate\limiter\RedisLimiter($di["redis"], $limits);
$redis = $di->getRedis();
$limiter = new \sndsgd\rate\limiter\RedisLimiter($redis, $limits);
$limiter->increment();

# copy the rate limit headers to the response
$response = $di["response"];
$response = $di->getResponse();
foreach ($limiter->getHeaders() as $header) {
list($key, $value) = preg_split("/\:\s?/", $header, 2);
$response->addHeader($key, $value);
}

# if the limit was exceeded, prevent futher execution
if ($limiter->isExceeded()) {
throw new \sndsgd\exception\TooManyRequestsException();
throw new \sndsgd\http\exception\TooManyRequestsException();
}
```

0 comments on commit cfb4f21

Please sign in to comment.