• Stars
    star
    190
  • Rank 196,592 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Redis-backed, per-worker rate limits for job processing.

sidekiq-rate-limiter

Gem Version Test Status Coverage Status

Redis-backed, per-worker rate limits for job processing.

Compatibility

sidekiq-rate-limiter is actively tested against MRI versions 2.7 and 3.1.

sidekiq-rate-limiter works by using a custom fetch class, the class responsible for pulling work from the queue stored in Redis. Consequently, you'll want to be careful about using other gems that use the same strategy, sidekiq-priority being one example.

I've attempted to support the same options as used by sidekiq-throttler. So, if your worker already looks like this example I lifted from the sidekiq-throttler wiki:

class MyWorker
  include Sidekiq::Worker

  sidekiq_options throttle: { threshold: 50, period: 1.hour }

  def perform(user_id)
    # Do some heavy API interactions.
  end
end

Then you wouldn't need to change anything.

Installation

Add this line to your application's Gemfile:

gem 'sidekiq-rate-limiter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sidekiq-rate-limiter

Configuration

See server.rb for an example of how to configure sidekiq-rate-limiter. Alternatively, you can add the following to your initializer or what-have-you:

require 'sidekiq-rate-limiter/server'

Or, if you prefer, amend your Gemfile like so:

gem 'sidekiq-rate-limiter', require: 'sidekiq-rate-limiter/server'

By default, the limiter uses the name sidekiq-rate-limiter. You can define the constant Sidekiq::RateLimiter::DEFAULT_LIMIT_NAME prior to requiring to change this. Alternatively, you can include a name parameter in the configuration hash included in sidekiq_options

For example, the following:

  class Job
    include Sidekiq::Worker

    sidekiq_options queue: 'some_silly_queue',
                    rate: {
                      name:   'my_super_awesome_rate_limit',
                      limit:  50,
                      period: 3600, ## An hour
                    }

    def perform(*args)
      ## do stuff
      ## ...

The configuration above would result in any jobs beyond the first 50 in a one-hour period being delayed. The server will continue to fetch items from Redis, & will place any items that are beyond the threshold at the back of their queue.

Dynamic Configuration

The simplest way to set the rate-limiting options (:name, :limit, and :period) is to assign them each a static value (as above). In some cases, you may wish to calculate values for these options for each specific job. You can do this by supplying a Proc for any or all of these options.

The Proc may receive as its arguments the same values that will be passed to perform when the job is finally performed.

class Job
  include Sidekiq::Worker

  sidekiq_options queue: "my_queue",
                  rate: {
                    name:   ->(user_id, rate_limit) { user_id },
                    limit:  ->(user_id, rate_limit) { rate_limit },
                    period: ->{ Date.today.monday? ? 2.hours : 4.hours }, # can ignore arguments
                  }

  def perform(user_id, rate_limit)
    ## do something

Caveat: Normally, Sidekiq stores the sidekiq_options with the job on your Redis server at the time the job is enqueued, and it is these stored values that are used for rate-limiting. This means that if you deploy a new version of your code with different sidekiq_options, the already-queued jobs will continue to behave according to the options that were in place when they were created. When you supply a Proc for one or more of your configuration options, your rate-limiting options can no longer be stored in Redis, but must instead be calculated when the job is fetched by your Sidekiq server for potential execution. If your application code changes while a job is in the queue, it may run with different sidekiq_options than existed when it was first enqueued.

Motivation

Sidekiq::Throttler is great for smaller quantities of jobs but falls down a bit for larger queues (see issue #8). In addition, jobs that are limited multiple times are counted as 'processed' each time, so the stats balloon quickly.

TODO

  • While it subclasses instead of monkey patching, setting Sidekiq.options[:fetch] is still asking for interaction issues. It would be better for this to be directly in Sidekiq or to use some other means to accomplish this goal.

Contributing

  1. Fork
  2. Commit
  3. Pull Request

License

MIT. See LICENSE for details.

More Repositories

1

pgl_ddl_deploy

Transparent Logical DDL Replication
PLpgSQL
76
star
2

saigo

A collection of exercises for Go learners and instructors
Go
30
star
3

givens

Easy test setup without side effects.
TypeScript
24
star
4

business_calendar

Concise, internationalized business date handling.
Ruby
14
star
5

scout

An SQS listener for rails apps and sidekiq
Go
14
star
6

landable

an API for your CMS
Ruby
11
star
7

pg_fact_loader

Build fact tables asynchronously with Postgres
PLpgSQL
10
star
8

prodder

Tool to maintain structure.sql and seed table contents based on production DBs
Ruby
10
star
9

sidekiq-instrument

StatsD instrumentation for Sidekiq
Ruby
8
star
10

audit_star

Database change auditing.
Go
8
star
11

obvious_data

Treat DB functions, triggers, etc more like code, and make them more discoverable
Ruby
7
star
12

enova-python

enova-python is a publicly maintained version of the archived https://github.com/poise/poise-python cookbook
Ruby
6
star
13

tokyo

A collection of general-purpose Go packages
Go
5
star
14

sidekiq-apriori

Ruby
5
star
15

mutiny

Reusable, declarative HTML5 widgets
JavaScript
3
star
16

landable_publicist

Coming Soon
Ruby
2
star
17

trende

trende takes information about events happening and displays them in human-readable maps and graphs.
Ruby
2
star
18

pglogical_ticker

Time-based replication delay for pglogical
PLpgSQL
2
star
19

katas

Archive of the katas published for Enova.
HTML
2
star
20

active_record_stats

ActiveRecord usage metrics to StatsD for Rails, Resque, Sidekiq
Ruby
2
star
21

embeddable_asset

Dynamically embed your assets into your Stylesheets
Ruby
1
star
22

churnhub

git changes statistics for volatile codebases
Ruby
1
star
23

hacknight

IFTTT, reddit, twitter hacknight!
1
star