Throttling ActiveResource

Rate limit. It's an essential, if somewhat bothersome, consideration in accessing any public API. Twitter's API limits its clients to 100 requests per 60 minutes. Exceed that, and you, the client, are locked out for a good ten minutes.

The other day, I was using ActiveResource to build a library to interface with the Harvest API, which implements a rate limit of 40 requests per 15 seconds. While a generous limit, I knew that the reporting tool I was building would need to make a lot of requests. And I was indeed getting locked out.

Since ActiveResource provides no throttling functionality, I decided to build a simple gem to add it. Thus was born ActiveResourceThrottle.

To use, create a generic base class to access the given RESTful API. Then, use the throttle method to specify the request limits you'd like imposed. Requests originating from any subclasses, representing the various API resources, will be throttled accordingly.

require 'active_resource_throttle'
class Twitter < ActiveResource::Base
throttle(:requests => 100, :interval => 3600, :sleep_interval => 10)
end

Further documentation can found in the README. Happy Throttling!



Labels: , ,

Leave a comment