sidekiq-congestion 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa39832645b56750973469661ef9d904fe684edf
4
- data.tar.gz: e85779be4e776e654db4a44ddfcc8f6dcd114f7b
3
+ metadata.gz: d982878cf7a24a459b3f0fab33a4a323a080c56a
4
+ data.tar.gz: 74d14a82bf221c29a1b760a9929baac3a6bb05ce
5
5
  SHA512:
6
- metadata.gz: 618d7512f650c99938b29232e827a4be9cc31ea81b18aa49364b701cf0a038f377b9b205cbc027ff9c5750aa388ac358aca9776ca08f63ef512e85825f04d1b9
7
- data.tar.gz: b7dea32db308cc935806b76dd9a38b93ff6e960a14690a551464aafad4fe7943377e68e8d6b1ac2e04db08a515149905d7d17a55671e864ebc08ac24c763dad0
6
+ metadata.gz: 24596d57dc5467fcf45af8c1286979f2558d37a538e452e4cda666024f9134b4a0c3087f4c8c7c7dcc433c5a031e99f4fd7741441523dab42259a11aa17b646c
7
+ data.tar.gz: d1513ae196e8a684ed90203b6597294e106a0a0925cc27a79bc9e19b7eed00dd92683a9ac4e2058b7c92cffc1476821a4edb93ca3970780d95a7a1be89ce362f
data/.travis.yml CHANGED
@@ -2,10 +2,9 @@ language: ruby
2
2
  sudo: false
3
3
  cache: bundler
4
4
  rvm:
5
- - 1.9.3
6
5
  - 2.2.0
7
6
  - jruby-19mode
8
7
  services:
9
8
  - redis-server
10
- jruby_before_script: &jruby_before_script
9
+ jruby_before_script:
11
10
  - bundle exec jbundle install
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,41 @@ Or install it yourself as:
26
27
 
27
28
  ## Usage
28
29
 
29
- TODO: Write usage instructions here
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
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Congestion
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  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.1
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-14 00:00:00.000000000 Z
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