public
Description: A throttler for ActiveResource requests.
Homepage: http://aiaio.github.com/active_resource_throttle
Clone URL: git://github.com/aiaio/active_resource_throttle.git
name age message
file HISTORY Tue Dec 16 11:00:48 -0800 2008 initial commit [aiaio]
file LICENSE Tue Dec 16 11:00:48 -0800 2008 initial commit [aiaio]
file README.rdoc Thu May 07 08:25:54 -0700 2009 Updated rate [aiaio]
file Rakefile Tue Dec 16 11:00:48 -0800 2008 initial commit [aiaio]
file active_resource_throttle.gemspec Mon Feb 23 09:12:21 -0800 2009 fixed Hash extensions to work with Rails [aiaio]
directory lib/ Mon Feb 23 09:12:21 -0800 2009 fixed Hash extensions to work with Rails [aiaio]
directory test/ Tue Dec 16 11:00:48 -0800 2008 initial commit [aiaio]
README.rdoc

ActiveResource Throttle

A rate limiter for ActiveResource requests.

DESCRIPTION:

Problem

You’re writing a library to consume a RESTful web service. That service publishes a throttle limit. So you need to throttle your requests to prevent the dreaded 503.

Solution

ActiveResource Throttle adds request throttling to ActiveResource. Specify the limits in your ActiveResource base class, and no longer will your client code have to worry about the number and frequency of its requests.

INSTALL:

  gem sources -a http://gems.github.com
  gem install aiaio-active_resource_throttle

USAGE:

  require "active_resource_throttle"

  class MyResource < ActiveResource::Base
    include ActiveResourceThrottle
    self.site = "http://example.com/api/"
    throttle(:interval => 60, :requests => 20, :sleep_interval => 10)
  end

  class Person < MyResource; end
  class Post < MyResource; end
  1. Require activeresource_throttle.
  2. Include ActiveResourceThrottle in the ActiveResource class. If you’re creating a library to access several resources, it’s necessary to create a generic base class for the api you’re accessing. Specify the site, login credentials, and throttle, and then subclass the base class for the various resources. Note that the throttle will work across subclasses.
  3. Invoke the #throttle class method with the required options interval and requests. You may also specify a sleep_interval. The settings in the example code above will allow for a maximum of 20 requests per minute. When that limit is reached, requests will be paused for 10 seconds.

ISSUES:

ActiveResource Throttle will not work properly across multiple instances of ActiveResource (e.g., in a Rails application with more than one Mongrel). At the moment, it should be used in single-process scripts. Also, the gem is not yet threadsafe.

Expect a threadsafe release in future versions.