sidekiq-circuit-breaker 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 01760e5030cfb4c3d6032deed7cc1747c6d5c955d9fc021ce0b2b7ee97a9f908
4
+ data.tar.gz: f79eb83bf8fdfd2221b73484b62309d976810faa99b4641f4ca5552ff2d3c786
5
+ SHA512:
6
+ metadata.gz: 1931812d56c30710e8107152ee9ad9dc2b6174d12f090ed3d9c5aa0f6eac3c99604dd21791510175e9c6c2db5eed7adb36d899804c28ddb4128f90df258ffdd3
7
+ data.tar.gz: 268d6a3d12d94a6426de7528c373aa1df68092fa8e943dca216c4df18aabf9fbd9a49901f42c606ed0eb342d5dcb161ce3a276d0852dd97ed7076606c024b4f4
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.4
7
+ before_install: gem install bundler -v 1.16.3
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sidekiq-circuitbreaker.gemspec
6
+ gemspec
7
+
8
+ gem 'pry'
9
+ gem 'activesupport'
10
+
11
+ # Gemfile
12
+ group :test do
13
+ gem 'rspec-sidekiq'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sidekiq-circuit-breaker (0.1.0)
5
+ sidekiq (>= 5.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.0)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ coderay (1.1.2)
16
+ concurrent-ruby (1.0.5)
17
+ connection_pool (2.2.2)
18
+ diff-lcs (1.3)
19
+ i18n (1.0.1)
20
+ concurrent-ruby (~> 1.0)
21
+ method_source (0.9.0)
22
+ minitest (5.11.3)
23
+ pry (0.11.3)
24
+ coderay (~> 1.1.0)
25
+ method_source (~> 0.9.0)
26
+ rack (2.0.5)
27
+ rack-protection (2.0.3)
28
+ rack
29
+ rake (10.5.0)
30
+ redis (4.0.1)
31
+ rspec (3.7.0)
32
+ rspec-core (~> 3.7.0)
33
+ rspec-expectations (~> 3.7.0)
34
+ rspec-mocks (~> 3.7.0)
35
+ rspec-core (3.7.1)
36
+ rspec-support (~> 3.7.0)
37
+ rspec-expectations (3.7.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.7.0)
40
+ rspec-mocks (3.7.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.7.0)
43
+ rspec-sidekiq (3.0.3)
44
+ rspec-core (~> 3.0, >= 3.0.0)
45
+ sidekiq (>= 2.4.0)
46
+ rspec-support (3.7.1)
47
+ sidekiq (5.1.3)
48
+ concurrent-ruby (~> 1.0)
49
+ connection_pool (~> 2.2, >= 2.2.0)
50
+ rack-protection (>= 1.5.0)
51
+ redis (>= 3.3.5, < 5)
52
+ thread_safe (0.3.6)
53
+ tzinfo (1.2.5)
54
+ thread_safe (~> 0.1)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ activesupport
61
+ bundler (~> 1.16)
62
+ pry
63
+ rake (~> 10.0)
64
+ rspec (~> 3.0)
65
+ rspec-sidekiq
66
+ sidekiq-circuit-breaker!
67
+
68
+ BUNDLED WITH
69
+ 1.16.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nando Sousa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Sidekiq::Circuitbreaker
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sidekiq/circuitbreaker`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sidekiq-circuitbreaker'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sidekiq-circuitbreaker
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sidekiq-circuitbreaker.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sidekiq/circuitbreaker"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,72 @@
1
+ require 'sidekiq'
2
+
3
+ module Sidekiq
4
+ module CircuitBreaker
5
+ module API
6
+ extend self
7
+
8
+ def open_circuit(scope, ttl = 120)
9
+ raise ArgumentError, 'scope must not be nil' if scope.nil?
10
+ Sidekiq.redis do |conn|
11
+ conn.setex(open_key(scope), ttl, 0)
12
+ # reset failure count
13
+ conn.del(failure_key(scope))
14
+ end
15
+ true
16
+ end
17
+
18
+ def register_failure_for_scope(scope, ttl = 120)
19
+ raise ArgumentError, 'scope must not be nil' if scope.nil?
20
+ key = failure_key(scope)
21
+
22
+ _, failure_count = Sidekiq.redis do |conn|
23
+ conn.multi do
24
+ conn.expire(key, ttl)
25
+ conn.incr(key)
26
+ end
27
+ end
28
+
29
+ failure_count
30
+ end
31
+
32
+ def register_success_for_scope(scope)
33
+ raise ArgumentError, 'scope must not be nil' if scope.nil?
34
+ Sidekiq.redis do |conn|
35
+ conn.del(failure_key(scope), open_key(scope))
36
+ end
37
+ end
38
+
39
+ def time_to_open_the_circuit(scope)
40
+ Sidekiq.redis do |conn|
41
+ conn.ttl(open_key(scope))
42
+ end
43
+ end
44
+
45
+ def circuit_opened?(scope)
46
+ Sidekiq.redis do |conn|
47
+ conn.exists(open_key(scope))
48
+ end
49
+ end
50
+
51
+ def failure_count_for_scope(scope)
52
+ Sidekiq.redis do |conn|
53
+ conn.get(failure_key(scope))
54
+ end.to_i
55
+ end
56
+
57
+ private
58
+
59
+ def redis_key(suffix)
60
+ "sidekiq:circuit_breaker:#{suffix}"
61
+ end
62
+
63
+ def open_key(scope)
64
+ redis_key("open:#{scope}")
65
+ end
66
+
67
+ def failure_key(scope)
68
+ redis_key("failure:#{scope}")
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ module Sidekiq
2
+ module CircuitBreaker
3
+ class Configuration
4
+ attr_reader :scope
5
+
6
+ attr_accessor :failure_threshold
7
+ attr_accessor :max_open_time
8
+
9
+ def initialize
10
+ self.failure_threshold = 7
11
+ self.max_open_time = 120
12
+ end
13
+
14
+ def set_scope(identifier = nil)
15
+ @scope = identifier
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,54 @@
1
+ require 'sidekiq/circuit_breaker/api'
2
+
3
+ module Sidekiq
4
+ module CircuitBreaker
5
+ class Manager
6
+
7
+ def initialize(scope, options)
8
+ @scope = scope
9
+ @options = options
10
+ end
11
+
12
+ def evaluate_failure
13
+ count = register_failure
14
+ open if count >= @options.failure_threshold
15
+ end
16
+
17
+ def open?
18
+ circuit_opened?(@scope)
19
+ end
20
+
21
+ def closed?
22
+ !open?
23
+ end
24
+
25
+ def failure_count
26
+ failure_count_for_scope(@scope)
27
+ end
28
+
29
+ def open
30
+ open_circuit(@scope, @options.max_open_time)
31
+ end
32
+
33
+ def time_to_open
34
+ time_to_open_the_circuit(@scope)
35
+ end
36
+
37
+ def register_failure
38
+ register_failure_for_scope(@scope)
39
+ end
40
+
41
+ def failure_count
42
+ failure_count_for_scope(@scope)
43
+ end
44
+
45
+ def register_success
46
+ register_success_for_scope(@scope)
47
+ end
48
+
49
+ private
50
+
51
+ include Sidekiq::CircuitBreaker::API
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,95 @@
1
+ require 'sidekiq/circuit_breaker/manager'
2
+ require 'sidekiq/circuit_breaker/middleware'
3
+ require 'sidekiq/client'
4
+
5
+ module Sidekiq
6
+ module CircuitBreaker
7
+ module Middleware
8
+ class Client
9
+ def call(worker_class, msg, queue, redis_pool)
10
+ worker = constantize(worker_class)
11
+ circuit_breaker = worker.respond_to?(:sidekiq_circuit_breaker_enabled?)
12
+ return yield unless circuit_breaker
13
+
14
+ options = worker.sidekiq_circuit_breaker_options
15
+ scope = extract_scope(options, msg) || worker_class
16
+
17
+ mgr = CircuitBreaker::Manager.new(scope, options)
18
+ if mgr.open? && msg['at'].nil?
19
+ msg['at'] = (Time.now + mgr.time_to_open).to_f
20
+ end
21
+
22
+ yield
23
+ end
24
+
25
+ private
26
+
27
+ def extract_scope(options, msg)
28
+ scope = options.scope
29
+ return scope if scope.is_a?(String)
30
+
31
+ options.scope.call(*msg['args'])
32
+ end
33
+
34
+ def constantize(str)
35
+ names = str.split('::')
36
+ names.shift if names.empty? || names.first.empty?
37
+
38
+ names.inject(Object) do |constant, name|
39
+ constant.const_defined?(name, false) ? constant.const_get(name, false) : constant.const_missing(name)
40
+ end
41
+ end
42
+ end
43
+
44
+ class Server
45
+ def call(worker, msg, queue)
46
+ circuit_breaker_enabled = worker.respond_to?(:sidekiq_circuit_breaker_enabled?)
47
+ return unless circuit_breaker_enabled
48
+
49
+ def worker.perform(*args)
50
+ manager = sidekiq_circuit_breaker_manager(args)
51
+
52
+ if manager.open?
53
+ begin
54
+ Sidekiq::Client.push(
55
+ 'class' => self.class,
56
+ 'args' => args
57
+ )
58
+ return
59
+ end
60
+ end
61
+
62
+ super(*args)
63
+ end
64
+
65
+ options = worker.class.sidekiq_circuit_breaker_options
66
+ manager = worker.sidekiq_circuit_breaker_manager(args, options)
67
+
68
+ begin
69
+ yield
70
+ rescue Exception => e
71
+ manager.evaluate_failure
72
+ raise e
73
+ end
74
+
75
+ manager.register_success
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ Sidekiq.configure_client do |config|
83
+ config.client_middleware do |chain|
84
+ chain.add Sidekiq::CircuitBreaker::Middleware::Client
85
+ end
86
+ end
87
+
88
+ Sidekiq.configure_server do |config|
89
+ config.client_middleware do |chain|
90
+ chain.add Sidekiq::CircuitBreaker::Middleware::Client
91
+ end
92
+ config.server_middleware do |chain|
93
+ chain.add Sidekiq::CircuitBreaker::Middleware::Server
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module CircuitBreaker
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,46 @@
1
+ require 'sidekiq/circuit_breaker/configuration'
2
+
3
+ module Sidekiq
4
+ module CircuitBreaker
5
+ module Worker
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ def sidekiq_circuit_breaker_manager(args)
11
+ options = self.class.sidekiq_circuit_breaker_options
12
+ scope = scope(args)
13
+ CircuitBreaker::Manager.new(scope, options)
14
+ end
15
+
16
+ private
17
+
18
+ def scope(args)
19
+ options = self.class.sidekiq_circuit_breaker_options
20
+ extract_scope(options, args) || self.class.name
21
+ end
22
+
23
+ def extract_scope(options, args)
24
+ scope = options.scope
25
+ return scope if scope.is_a?(String)
26
+
27
+ options.scope.call(*args)
28
+ end
29
+
30
+
31
+ module ClassMethods
32
+ def sidekiq_circuit_breaker_enabled?
33
+ true
34
+ end
35
+
36
+ def sidekiq_circuit_breaker
37
+ yield(sidekiq_circuit_breaker_options) if block_given?
38
+ end
39
+
40
+ def sidekiq_circuit_breaker_options
41
+ @@setup ||= Sidekiq::CircuitBreaker::Configuration.new
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq/circuit_breaker/middleware'
3
+ require 'sidekiq/circuit_breaker/worker'
4
+
5
+ module Sidekiq
6
+ module CircuitBreaker; end
7
+ end
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sidekiq/circuit_breaker/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-circuit-breaker"
8
+ spec.version = Sidekiq::CircuitBreaker::VERSION
9
+ spec.authors = ["Nando Sousa"]
10
+ spec.email = ["nandosousafr@gmail.com"]
11
+
12
+ spec.summary = 'Circuit Breaker functionality to Sidekiq'
13
+ spec.description = 'Circuit Breaker functionality to Sidekiq'
14
+ spec.homepage = 'http://shipit.resultadosdigitais.com.br/open-source/'
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency 'sidekiq', '>= 5.0.0'
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-circuit-breaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nando Sousa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Circuit Breaker functionality to Sidekiq
70
+ email:
71
+ - nandosousafr@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/sidekiq/circuit_breaker.rb
87
+ - lib/sidekiq/circuit_breaker/api.rb
88
+ - lib/sidekiq/circuit_breaker/configuration.rb
89
+ - lib/sidekiq/circuit_breaker/manager.rb
90
+ - lib/sidekiq/circuit_breaker/middleware.rb
91
+ - lib/sidekiq/circuit_breaker/version.rb
92
+ - lib/sidekiq/circuit_breaker/worker.rb
93
+ - sidekiq-circuit-breaker.gemspec
94
+ homepage: http://shipit.resultadosdigitais.com.br/open-source/
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.7.6
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Circuit Breaker functionality to Sidekiq
118
+ test_files: []