No commit activity in last 3 years
No release in over 3 years
Sidekiq middleware rate limiter that provides both time-based limits and quantity-based limits
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 3.0
 Project Readme

Sidekiq::Congestion

Build Status Test Coverage Code Climate Gem Version

Sidekiq middleware for Congestion

Provides rate limiting for Sidekiq workers.

Installation

Add this line to your application's Gemfile:

gem 'sidekiq-congestion'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sidekiq-congestion

Usage

Documentation of Congestion configuration can be found here

However, Sidekiq::Congestion disables rejection tracking by default.

Rejection tracking would cause attempted calls to your workers (even if they don't trigger a run) to count towards the worker limits -- which is probably undesirable. If your worker is high throughput, you may want to enable it just so the request tracking is atomic.

In an initializer:

# Set whatever default options you'd like
# Congestion.default_options[:track_rejected] = false

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add Sidekiq::Congestion::Limiter
  end
end

In a worker:

class YourWorker
  include Sidekiq::Worker

  # Allow 5 calls/hour, with at least 5 minutes between calls
  # When the request is not allowed, it is rescheduled
  sidekiq_options congestion: {
    interval: 1.hour,
    max_in_interval: 5,
    min_delay: 5.minutes,
    reject_with: :reschedule, # (or :cancel)
    track_rejected: false, # false by default, see above
    # Restrict the scope of the limit via job params
    # `key` is called with the same arguments as perform
    key: ->(user) {
      "user_#{ user.id }_unique_job_identifier"
    },
    # Conditionally enable/disable the limit
    # returning false will bypass the limit
    enabled: ->(user) {
      !user.admin?
    }
  }

  def perform(user_id)
    # ...
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To run the specs, run bundle exec rake.

Contributing

  1. Fork it ( https://github.com/parrish/sidekiq-congestion/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request