Coder Social home page Coder Social logo

throttle's Introduction

Build Status

What is it?

A basic throttling implementation

Versions

If you are looking for the old version compatible with PHP 5.3 try the v0.1 branch and update your composer requirement to "michaelesmith/throttle": "^0.1"

Installation

composer require "michaelesmith/throttle"

You will also need a PSR-6 compatible cache library such as "cache/cache" or "symfony/cache".

Examples

$throttle = new Throttle(new \Cache\Adapter\PHPArray\ArrayCachePool());
$throttle->add(new Condition(60, 2)); // adds an interval where 2 increments are allowed in 60 seconds
$throttle->add(new Condition(600, 5)); // adds an interval where 5 increments are allowed in 10 minutes

// in each action you want to limit
try {
    $throttle->increment($_SERVER['REMOTE_ADDR']); // some client identifier like an ip or session id
    // NOTE: $_SERVER['REMOTE_ADDR'] may not gove you the actual client IP if you are behind a reverse proxy
    // Here is how Symfony finds the client IP
    // @link: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L786-L805
} catch(RateException $e) {
    $condition = $e->getCondition(); // the condition that hit the rate limit
    printf('You can only make %d requests in %d seconds', $condition->getLimit(), $condition->getTtl());
}

You can use any PSR-6 compatible cache pool, but you need to use one that is persistent across requests for most cases unlike this example.

throttle's People

Contributors

michaelesmith avatar aitboudad avatar silvioq avatar

Stargazers

kwan avatar  avatar RJ Garcia avatar  avatar KitCat avatar  avatar

Watchers

 avatar  avatar James Cloos avatar

Forkers

aitboudad silvioq

throttle's Issues

PHP Cache and Increment Issue

I am using cache/cache library, and I keep getting the following error.

[01-Jun-2020 16:33:50 America/New_York] PHP Fatal error: Uncaught Cache\Adapter\Common\Exception\InvalidArgumentException: Invalid key "282822929-60". Valid filenames must match [a-zA-Z0-9.! ]. in /opt/lampp/htdocs/qubus/vendor/cache/cache/src/Adapter/Filesystem/FilesystemCachePool.php:145_

Here is a bit of my code:

use TriTan\Common\Container;
use MS\Throttle\Condition;
use MS\Throttle\Interval;
use MS\Throttle\RateException;
use MS\Throttle\Throttle;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;

$filesystemAdapter = new Local(Container::getInstance()->get('cache_path'));
$filesystem        = new Filesystem($filesystemAdapter);
$throttle = new Throttle(new FilesystemCachePool($filesystem));

$throttle->add(new Condition(60, 2));

try {
    $throttle->increment('282822929');
} catch (RateException $e) {
    $condition = $e->getCondition(); // the condition that hit the rate limit
    printf(
        'You can only make %d requests in %d seconds',
        $condition->getLimit(),
        $condition->getTtl()
    );
}

Any help with figuring this out is greatly appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.