2017 © Pedro Peláez
 

library throttler

Simple rate limiter and usage tracker component.

image

franzip/throttler

Simple rate limiter and usage tracker component.

  • Tuesday, July 28, 2015
  • by franzip
  • Repository
  • 1 Watchers
  • 0 Stars
  • 129 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 2 % Grown

The README.md

Build Status Coverage Status, (*1)

Throttler

A simple, general purpose, rate limiter and usage tracker library., (*2)

Install composer in your project:, (*3)

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

Create a composer.json file in your project root:, (*4)

{
    "require": {
        "franzip/throttler": "1.0.*"
    }
}

Install via composer, (*5)

php composer.phar install

Description

Once instantiated, the object must always be explicitly activated with the provided start() method., (*6)

Basically, you set a timeframe (it can be 24 hours, 2 seconds, 30 minutes, etc.), a global limit and the things you want to keep track of, and you're ready to go., (*7)

You can also set an additional limit that will applied as limit to each single component you are tracking., (*8)

You can change the given parameters only after you stop the current instance from tracking with the provided stop() method., (*9)

The Throttler will detect when a given timeframe is expired: all its internal states will be reset and a new timeframe will be computed., (*10)

If your code need to access any Throttler attributes, just use the provided getters (getName, getGlobalThreshold, etc.). See the constructor., (*11)

Constructor

$throttler = new Throttler($name, $globalThreshold, $metric, $metricFactor = 1,
                           $componentThreshold = null, $components = array());

Allowed $metric

  • 'sec': Compute the timeframe in (seconds * $metricFactor)
  • 'min': Compute the timeframe in (minutes * $metricFactor)
  • 'hrs': Compute the timeframe in (hours * $metricFactor)

Basic Usage (default arguments)

Example 1: using Throttler to cap incoming requests. The total amount of requests will be capped to 30 per hour., (*12)

use Franzip\Throttler\Throttler;

// Setting things up
// Max 30 total requests per hour
$throttler = new Throttler('requests', 30, 'hrs');

// Nothing happens and the call will return false since there is nothing to track
$throttler->start();

// Add remote addresses to track...
$throttler->addComponents('AddressToTrack1');
$throttler->addComponents('AddressToTrack2');
$throttler->addComponents('AddressToTrack3');
// Bulk adding
$throttler->addComponents(array('AddressToTrack4',
                                'AddressToTrack5',
                                'AddressToTrack6',
                                ...));
...

// Start tracking (timeframe starts now)
$throttler->start();

if ($throttler->updateComponent('AddressToTrack1')) {
    // handle update success
} else {
    // handle update failure
}

$numOfRequests = 31;

// will return false since the global limit is 30
$throttler->updateComponent('AddressToTrack1', $numOfRequests);

...

// Remove all stuff to track
$throttler->stop();
$throttler->setComponents(array());

Usage (custom arguments)

Example 2: using Throttler to cap incoming requests., (*13)

The total amount of requests will be capped to 100 per day., (*14)

The amount of incoming requests from each address will be capped to 10 per day., (*15)

use Franzip\Throttler\Throttler;

// Setting things up
// Max 100 total requests per day
// Max 10 requests from each tracked address per day
$throttler = new Throttler('requests', 100, 'hrs', 24,
                           10, array('AddressToTrack1',
                                     'AddressToTrack2',
                                     ...));

// Start tracking (timeframe starts now)
$throttler->start();

if ($throttler->updateComponent('AddressToTrack1')) {
    // handle success
} else {
    // handle failure
}

isActive()

Return whether tracking is turned on., (*16)

use Franzip\Throttler\Throttler;

$throttler = new Throttler('requests', 100, 'hrs');

// false
$throttler->isActive();

// false, there's nothing to track yet.
$throttler->start();

// false
$throttler->isActive();

$throttler->addComponents(array('foo', 'bar'));

// true, we have something to track
$throttler->start();

// true
$throttler->isActive();

// reset the instance (this will also stop tracking)
$throttler->reset();

// false
$throttler->isActive();

reset()

You can revert a Throttler object status to when it was instantiated. Just use the reset() method at anytime., (*17)

use Franzip\Throttler\Throttler;

$throttler = new Throttler('requests', 100, 'hrs');
// Change time cap to 2 hours
$throttler->setMetricFactor(2);
// Change time cap to 2 minutes
$throttler->setMetric('min');
// Change global limit to 50
$throttler->setGlobalThreshold(50);

...

$throttler->reset();
// reverted to 100
$throttler->getGlobalThreshold();
// reverted to 'hrs'
$throttler->getMetric();

TODOs

  • [x] A decent exceptions system.
  • [x] Move validation to external class.
  • [x] Allow bulk components adding.
  • [ ] Refactoring messy tests.
  • [ ] Refactoring update-checking methods.

License

MIT Public License., (*18)

The Versions

28/07 2015

dev-master

9999999-dev http://github.com/franzip/throttler

Simple rate limiter and usage tracker component.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Francesco Pezzella

tracking tracker throttle usage