2017 © Pedro Peláez
 

kohana-module kthrottler

Kthrottler Kohana module as a composer package

image

illusorium/kthrottler

Kthrottler Kohana module as a composer package

  • Monday, December 19, 2016
  • by illusorium
  • Repository
  • 0 Watchers
  • 0 Stars
  • 28 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

, (*1)

KThrottler

There is also a Ruby version (for Rails), see here: http://github.com/fredwu/action_throttler, (*2)

Introduction

KThrottler is an easy to use Kohana module to quickly throttle application actions based on configurable duration and limit., (*3)

Brought to you by Wuit., (*4)

Features

  • Easy to use, easy to configure
  • Lightweight
  • Supports Kohana v3

Prerequisites

  • Kohana's built-in DB module

Usage

Download and install the module

git clone git://github.com/fredwu/kthrottler.git

Enable the module

In your bootstrap file (application/bootstrap.php), enable KThrottler like this:, (*5)

Kohana::modules(array(
    'database'    => MODPATH.'database',
    'kthrottler'  => MODPATH.'kthrottler',
));

Set up the database table

Import the kthrottler_logs_db.sql file supplied., (*6)

Configure the actions

The configuration file is located at config/kthrottler.php. Please copy it to your application/config folder., (*7)

The configuration array looks like this:, (*8)

return array
(
    'mail' => array
    (
        'duration' => '1 hour',
        'limit'    => 10,
    ),
);

You can add as many configuration elements as you like, just make sure you label them properly (i.e. like mail in the example)., (*9)

In the example, we are setting the mail action to perform at most 10 times within 1 hour duration., (*10)

Register the actions in your app

Now we will need to register the actions so they are recorded in the database., (*11)

To simply run an action, in your app (presumably somewhere in the controller), do this:, (*12)

KThrottler::actions()->run('mail');

KThrottler::actions()->run() will return true or false depending on whether or not the action is being throttled., (*13)

KThrottler::actions()->run() has an alias KThrottler::actions()->can_run() and a negative alias KThrottler::actions()->cannot_run()., (*14)

Typically, we would want to produce feedback to the user when an action is throttled, you can do so by:, (*15)

if (`KThrottler::actions()->cannot_run('mail')`)
    // tell the user that this action is not performed
end

KThrottler::actions()->run() also takes an optional reference parameter:, (*16)

KThrottler::actions()->run('mail', $current_user), (*17)

The reference parameter is very useful because we can track and throttle the action based on a reference, such as a user. The parameter accepts a String, an Integer or an ORM object., (*18)

Note that KThrottler::actions()->run() and its aliases will perform the action when possible. If you only want to check to see if an action can be performed, you can do this:, (*19)

`KThrottler::actions()->can_be_run('mail', $current_user)`

KThrottler::actions()->can_be_run() returns true or false without performing the action, and it also has a negative alias, KThrottler::actions()->cannot_be_run()., (*20)

Author

Copyright (c) 2010 Fred Wu (http://fredwu.me) and Wuit, released under the MIT license, (*21)

The Versions

19/12 2016

dev-master

9999999-dev

Kthrottler Kohana module as a composer package

  Sources   Download

The Requires