Sidekiq::Congestion
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
- Fork it ( https://github.com/parrish/sidekiq-congestion/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