Full Throttle
Pull the breaks on your background processing, protect api calls from abuse, and manage throughput with 0 hassle leveraging Redis to throttle concurrent processes.
Atomicity and thread-safety of throttle guaranteed by good use of Redis.
Usage
response = Throttle.for(:user_info, 200) { api["users/#{id}"].get }
user.update(response)
There we have :user_info
is the id of the throttle, 200
is the number of times that id can be executed in one second. Full Throttle is designed for high speed action, not long throttling windows.
get updated status
instance = Throttle.for(:user_info)
instance.status # [ bucket_time, bucket_count, bucket_size ]
handle or log throttled actions
begin
Throttle.for(:search_index, 300) { record.update_index! }
rescue Throttle::ThrottledError => e
...
end
Manage throughput without code pushes
don't hardcode limits and scale at will running on the console or on a cronjob to do more work at night and take it easy during the day
# no hardcoded limits
Throttle.new(:upstream_sync) { throw_pokeball!(x, y) }
# manage on the console or a rake task with
Throttle.for(:upstream_sync).
set_bucket_size!(hour < 7 || hour > 22 ? 6_000 : 1_000)
end
All state is kept on Redis. Threadsafety as offered by Redis' script
.
Installation
Add this line to your application's Gemfile:
gem 'throttle'
And then execute:
$ bundle
Or install it yourself as:
$ gem install throttle
Contributing
- Fork it ( https://github.com/rafaelbandeira3/full_throttle/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request