Fairplay is a rate limiter for Sidekiq
Add the fairplay gem to your app's Gemfile
gem 'fairplay', :git => "git@github.com:nisanth074/fairplay.git"and install it
bundle install
Add a rate limit policy to a Sidekiq worker you'd like to rate limit
# In app/workers/process_message.rb
class ProcessMessage
include Sidekiq::Worker
class RateLimitOnSenderEmail < Fairplay::RateLimitPolicy
limit 15
period 1.minute
penalty 30.seconds
end
def sender_email(message_id)
Message.find(message_id).sender_email
end
def perform(message_id)
# ...
end
endThen, start enqueuing jobs with Fairplay.enqueue
Fairplay.enqueue(ProcessMessage, message_id)Fairplay.enqueue normally enqueues a job for immediate execution. However, when a job reaches a rate limit, Fairplay.enqueue schedules each subsequent job with a time delay (the penalty) using Sidekiq's Scheduled Jobs feature).
Fairplay.queue stops rate limiting once
- A new rate limit period begins
- And all pending rate limited jobs have been processed
The behaviour above ensures that jobs are processed in the order in which they arrive.
-
Has this gem seen production use?
Yes! fairplay has been in use in production at SupportBee for many years. It has enqueued billions of jobs.
- Add an example that showcases multiple rate limits in a single Sidekiq worker
- Bump the version number and publish recent changes to rubygems.org