sidekiq-congestion 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/README.md +36 -1
- data/lib/sidekiq/congestion.rb +8 -0
- data/lib/sidekiq/congestion/limiter.rb +20 -0
- data/lib/sidekiq/congestion/request.rb +54 -0
- data/lib/sidekiq/congestion/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d982878cf7a24a459b3f0fab33a4a323a080c56a
|
4
|
+
data.tar.gz: 74d14a82bf221c29a1b760a9929baac3a6bb05ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24596d57dc5467fcf45af8c1286979f2558d37a538e452e4cda666024f9134b4a0c3087f4c8c7c7dcc433c5a031e99f4fd7741441523dab42259a11aa17b646c
|
7
|
+
data.tar.gz: d1513ae196e8a684ed90203b6597294e106a0a0925cc27a79bc9e19b7eed00dd92683a9ac4e2058b7c92cffc1476821a4edb93ca3970780d95a7a1be89ce362f
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[](https://travis-ci.org/parrish/Sidekiq-Congestion)
|
4
4
|
[](https://codeclimate.com/github/parrish/Sidekiq-Congestion)
|
5
5
|
[](https://codeclimate.com/github/parrish/Sidekiq-Congestion)
|
6
|
+
[](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,41 @@ 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
|
+
}
|
63
|
+
end
|
64
|
+
```
|
30
65
|
|
31
66
|
## Development
|
32
67
|
|
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] = true
|
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,54 @@
|
|
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
|
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 rejection_method
|
50
|
+
options.fetch(:reject_with, :reschedule).to_sym
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
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.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Parrish
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: congestion
|
@@ -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
|