Coder Social home page Coder Social logo

throttle's Introduction

Build Status

README

What is it?

A basic throttling implementation

Installation

Use Composer (recommended)

The recommended way to install msDateTime is through composer.

If you don't have Composer yet, download it following the instructions on http://getcomposer.org/ or just run the following command:

curl -s http://getcomposer.org/installer | php

Just create a composer.json file for your project:

{
    "require": {
        "michaelesmith/throttle": "*"
    }
}

For more info on composer see https://github.com/composer/composer

Examples

###Basic

$throttle = new Throttle(new \Doctrine\Common\Cache\ApcCache(), new \MS\Throttle\Time());
$throttle->addInterval(60, 2); // adds an interval where 2 increments are allowed in 60 seconds
$throttle->addInterval(600, 5); // adds an interval where 5 increments are allowed in 10 minutes

// in each action you want to limit
try {
    $throttle->increment($request->getClientIp());
} catch(\MS\Throttle\RateException $e) {
    $e->getInterval(); // the interval that hit the rate limit
    printf('You can only make %d requests in %d seconds', $e->getInterval()->count, $e->getInterval()->seconds);
}

You can use any Doctrine\Common\Cache adapter but you need to use one that is persistent across requests for most cases.

###Garbage Collection

By using a request persistent cache adapter you will at some point need to purge outdated entries. There are 2 strategies for this:

// garbage collect only the current key on every increment
$throttle->increment($key = $request->getClientIp());
$throttle->garbageCollect($key);
// this can be expensive and will not purge old entries if the key is not used

// a better way use a cli command via crontab
$throttle->garbageCollect();
// not passing a key will cause it to collect all keys

throttle's People

Contributors

aitboudad avatar michaelesmith avatar

Watchers

 avatar  avatar

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.