Skip to content

nisanthchunduru/fairplay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fairplay

Fairplay is a rate limiter for Sidekiq

Installation

Add the fairplay gem to your app's Gemfile

gem 'fairplay', :git => "git@github.com:nisanth074/fairplay.git"

and install it

bundle install

Usage

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
end

Then, start enqueuing jobs with Fairplay.enqueue

Fairplay.enqueue(ProcessMessage, message_id)

How it works

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

  1. A new rate limit period begins
  2. And all pending rate limited jobs have been processed

The behaviour above ensures that jobs are processed in the order in which they arrive.

FAQ

  1. 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.

TODOs

  • Add an example that showcases multiple rate limits in a single Sidekiq worker
  • Bump the version number and publish recent changes to rubygems.org

About

Rate limiter for Sidekiq queuing system

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages