2017 © Pedro Peláez
 

library rate

Rate limiting for PHP

image

sndsgd/rate

Rate limiting for PHP

  • Wednesday, February 15, 2017
  • by sndsgd
  • Repository
  • 1 Watchers
  • 1 Stars
  • 349 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

sndsgd/rate

Latest Version Software License Build Status Coverage Status Total Downloads, (*1)

Rate limiting for PHP., (*2)

Requirements

This project is unstable and subject to changes from release to release., (*3)

You need PHP >= 7.0 to use this library, however, the latest stable version of PHP is recommended., (*4)

Install

Install sndsgd/rate using Composer., (*5)

Usage

Note: At the moment, this library only contains rate limiting tools., (*6)

# define the rate limits
$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
$redis = $di->getRedis();
$limiter = new \sndsgd\rate\limiter\RedisLimiter($redis, $limits);
$limiter->increment();

# copy the rate limit headers to the 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\http\exception\TooManyRequestsException();
}

The Versions

15/02 2017

dev-master

9999999-dev https://github.com/sndsgd/rate

Rate limiting for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

15/02 2017

dev-clean

dev-clean https://github.com/sndsgd/rate

Rate limiting for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

24/12 2016

0.1.2

0.1.2.0 https://github.com/sndsgd/rate

Rate limiting for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

23/07 2016

0.1.1

0.1.1.0 https://github.com/sndsgd/rate

Rate limiting for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

30/06 2016

0.1.0

0.1.0.0 https://github.com/sndsgd/rate

Rate limiting for PHP

  Sources   Download

MIT

The Requires