Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

An easy to use Rails plugin to quickly throttle application actions based on configurable duration and limit.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 action_throttler.gemspec
Octocat-spinner-32 init.rb
Octocat-spinner-32 install.rb
Octocat-spinner-32 uninstall.rb
README.md

Action Throttler

There is also a PHP version (for Kohana), see here: http://github.com/fredwu/kthrottler

Introduction

Action Throttler is an easy to use Rails plugin to quickly throttle application actions based on configurable duration and limit.

Brought to you by Envato and Wuit.

Features

  • Easy to use, easy to configure
  • Lightweight
  • Supports both Rails 2 and 3

Limitations

  • Supports ActiveRecord only
  • Uses Rake instead of Rails Generator for file generation (for compatibility purpose)

Usage

Download and install the plugin

Rails 2

script/plugin install http://fredwu@github.com/fredwu/action_throttler.git

Rails 3

rails plugin install http://fredwu@github.com/fredwu/action_throttler.git

Set up the plugin

To generate the migration file and migrate the database:

rake action_throttler:migrate
rake db:migrate

Or alternatively:

rake action_throttler:auto_migrate

Configure the actions

The configuration file is located at PATH_TO_YOUR_APP/config/initializers/action_throttler.rb.

The configuration block looks like this:

ActionThrottler::Actions.add :mail do |action|
  action.duration = 1.hour
  action.limit    = 10
end

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

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

Register the actions in your app

Now we will need to register the actions so they are recorded in the database.

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

ActionThrottler::Actions.run(:mail)

ActionThrottler::Actions.run will return true or false depending on whether or not the action is being throttled.

ActionThrottler::Actions.run has an alias ActionThrottler::Actions.can_run and a negative alias ActionThrottler::Actions.cannot_run.

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

if ActionThrottler::Actions.cannot_run(:mail)
  # tell the user that this action is not performed
end

ActionThrottler::Actions.run also takes an optional reference parameter:

ActionThrottler::Actions.run(:mail, @current_user)

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 ActiveRecord object.

Note that ActionThrottler::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:

ActionThrottler::Actions.can_be_run?(:mail, @current_user)

ActionThrottler::Actions.can_be_run? returns true or false without performing the action, and it also has a negative alias, ActionThrottler::Actions.cannot_be_run?.

Author

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

Something went wrong with that request. Please try again.