sidekiq-congestion 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +1 -2
- data/README.md +51 -1
- data/lib/sidekiq/congestion.rb +8 -0
- data/lib/sidekiq/congestion/limiter.rb +20 -0
- data/lib/sidekiq/congestion/request.rb +65 -0
- data/lib/sidekiq/congestion/version.rb +1 -1
- data/sidekiq-congestion.gemspec +2 -2
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 95b0f4fc3bcc9fdd6dfe8d063528ed1fce9fa7813070a25b654006501615a6a9
|
4
|
+
data.tar.gz: 709724df22352370b559eced7db0417c0205454c21ac6594b7faf73456057f9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 260ce82e14a221f77541d0f3542a39dbc2f0de4da29c2c06b3e2a99c3e831bf6dd3d8a2ee2278801151c7224b42894051e93b976c77b6dba8d94ec7b447b1968
|
7
|
+
data.tar.gz: cdc3386329f39df3c37c3aa6367cc6aad46e5896c6d462e449237aef4b2210ae800c0926daa6c01544a504028730f5f30aa905ecfbed81ceb2a0b9940fccbff3
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[![Build Status](https://travis-ci.org/parrish/Sidekiq-Congestion.svg?branch=master)](https://travis-ci.org/parrish/Sidekiq-Congestion)
|
4
4
|
[![Test Coverage](https://codeclimate.com/github/parrish/Sidekiq-Congestion/badges/coverage.svg)](https://codeclimate.com/github/parrish/Sidekiq-Congestion)
|
5
5
|
[![Code Climate](https://codeclimate.com/github/parrish/Sidekiq-Congestion/badges/gpa.svg)](https://codeclimate.com/github/parrish/Sidekiq-Congestion)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/sidekiq-congestion.svg)](http://badge.fury.io/rb/sidekiq-congestion)
|
6
7
|
|
7
8
|
Sidekiq middleware for [Congestion](https://github.com/parrish/Congestion)
|
8
9
|
|
@@ -26,7 +27,56 @@ Or install it yourself as:
|
|
26
27
|
|
27
28
|
## Usage
|
28
29
|
|
29
|
-
|
30
|
+
[Documentation of Congestion configuration can be found here](https://github.com/parrish/Congestion#user-content-configuration)
|
31
|
+
|
32
|
+
However, `Sidekiq::Congestion` disables rejection tracking by default.
|
33
|
+
|
34
|
+
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](http://en.wikipedia.org/wiki/Linearizability).
|
35
|
+
|
36
|
+
In an initializer:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# Set whatever default options you'd like
|
40
|
+
# Congestion.default_options[:track_rejected] = false
|
41
|
+
|
42
|
+
Sidekiq.configure_server do |config|
|
43
|
+
config.server_middleware do |chain|
|
44
|
+
chain.add Sidekiq::Congestion::Limiter
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
In a worker:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class YourWorker
|
53
|
+
include Sidekiq::Worker
|
54
|
+
|
55
|
+
# Allow 5 calls/hour, with at least 5 minutes between calls
|
56
|
+
# When the request is not allowed, it is rescheduled
|
57
|
+
sidekiq_options congestion: {
|
58
|
+
interval: 1.hour,
|
59
|
+
max_in_interval: 5,
|
60
|
+
min_delay: 5.minutes,
|
61
|
+
reject_with: :reschedule, # (or :cancel)
|
62
|
+
track_rejected: false, # false by default, see above
|
63
|
+
# Restrict the scope of the limit via job params
|
64
|
+
# `key` is called with the same arguments as perform
|
65
|
+
key: ->(user) {
|
66
|
+
"user_#{ user.id }_unique_job_identifier"
|
67
|
+
},
|
68
|
+
# Conditionally enable/disable the limit
|
69
|
+
# returning false will bypass the limit
|
70
|
+
enabled: ->(user) {
|
71
|
+
!user.admin?
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
def perform(user_id)
|
76
|
+
# ...
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
30
80
|
|
31
81
|
## Development
|
32
82
|
|
data/lib/sidekiq/congestion.rb
CHANGED
@@ -1,6 +1,14 @@
|
|
1
|
+
require 'congestion'
|
2
|
+
require 'sidekiq'
|
1
3
|
require 'sidekiq/congestion/version'
|
2
4
|
|
3
5
|
module Sidekiq
|
4
6
|
module Congestion
|
7
|
+
require 'sidekiq/congestion/request'
|
8
|
+
require 'sidekiq/congestion/limiter'
|
9
|
+
::Congestion.default_options[:track_rejected] = false
|
10
|
+
::Congestion.redis = ->{
|
11
|
+
::Sidekiq.redis{ |redis| redis }
|
12
|
+
}
|
5
13
|
end
|
6
14
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Sidekiq
|
2
|
+
module Congestion
|
3
|
+
class Limiter
|
4
|
+
def call(worker, job, queue, &block)
|
5
|
+
request = Sidekiq::Congestion::Request.new worker, job, queue
|
6
|
+
request.enabled? ? handle(request, block) : yield
|
7
|
+
end
|
8
|
+
|
9
|
+
def handle(request, block)
|
10
|
+
if request.allowed?
|
11
|
+
block.call
|
12
|
+
elsif request.reschedule?
|
13
|
+
request.reschedule!
|
14
|
+
else
|
15
|
+
# cancel request
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module Sidekiq
|
4
|
+
module Congestion
|
5
|
+
class Request
|
6
|
+
extend Forwardable
|
7
|
+
attr_accessor :worker, :args, :options
|
8
|
+
|
9
|
+
def_delegators :congestion,
|
10
|
+
:allowed?, :backoff, :first_request, :last_request,
|
11
|
+
:rejected?, :too_frequent?, :too_many?, :total_requests
|
12
|
+
|
13
|
+
def initialize(worker, job, queue)
|
14
|
+
self.worker = worker
|
15
|
+
self.args = job['args']
|
16
|
+
opts = worker.sidekiq_options_hash || { }
|
17
|
+
self.options = opts['congestion']
|
18
|
+
end
|
19
|
+
|
20
|
+
def enabled?
|
21
|
+
!!options && _enabled?
|
22
|
+
end
|
23
|
+
|
24
|
+
def reschedule?
|
25
|
+
rejection_method == :reschedule
|
26
|
+
end
|
27
|
+
|
28
|
+
def reschedule!
|
29
|
+
worker.class.perform_in backoff, *args
|
30
|
+
end
|
31
|
+
|
32
|
+
def key
|
33
|
+
@key ||= case options[:key]
|
34
|
+
when Proc
|
35
|
+
options[:key].call *args
|
36
|
+
when String, Symbol
|
37
|
+
options[:key].to_s
|
38
|
+
else
|
39
|
+
worker.class.name
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def congestion
|
44
|
+
@congestion ||= ::Congestion.request key, options
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
|
49
|
+
def _enabled?
|
50
|
+
case options[:enabled]
|
51
|
+
when Proc
|
52
|
+
options[:enabled].call *args
|
53
|
+
when nil
|
54
|
+
true
|
55
|
+
else
|
56
|
+
!!options[:enabled]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def rejection_method
|
61
|
+
options.fetch(:reject_with, :reschedule).to_sym
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/sidekiq-congestion.gemspec
CHANGED
@@ -19,10 +19,10 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.add_runtime_dependency 'congestion', '~> 0.
|
22
|
+
spec.add_runtime_dependency 'congestion', '~> 0.1'
|
23
23
|
spec.add_runtime_dependency 'sidekiq', '>= 3.0'
|
24
24
|
spec.add_development_dependency 'bundler', '>= 1.5'
|
25
|
-
spec.add_development_dependency 'rake', '
|
25
|
+
spec.add_development_dependency 'rake', '>= 1.13.3'
|
26
26
|
spec.add_development_dependency 'rspec', '~> 3.2'
|
27
27
|
spec.add_development_dependency 'rspec-its', '~> 1.2'
|
28
28
|
spec.add_development_dependency 'guard-rspec', '~> 4.5'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-congestion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Parrish
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: congestion
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: '0.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: '0.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sidekiq
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 1.13.3
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 1.13.3
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -170,6 +170,8 @@ files:
|
|
170
170
|
- bin/console
|
171
171
|
- bin/setup
|
172
172
|
- lib/sidekiq/congestion.rb
|
173
|
+
- lib/sidekiq/congestion/limiter.rb
|
174
|
+
- lib/sidekiq/congestion/request.rb
|
173
175
|
- lib/sidekiq/congestion/version.rb
|
174
176
|
- sidekiq-congestion.gemspec
|
175
177
|
homepage: https://github.com/parrish/sidekiq-congestion
|
@@ -191,8 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
191
193
|
- !ruby/object:Gem::Version
|
192
194
|
version: '0'
|
193
195
|
requirements: []
|
194
|
-
|
195
|
-
rubygems_version: 2.4.6
|
196
|
+
rubygems_version: 3.0.6
|
196
197
|
signing_key:
|
197
198
|
specification_version: 4
|
198
199
|
summary: Redis rate limiter Sidekiq middleware
|