ya_circuit_breaker 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +13 -0
- data/circuit_breaker.gemspec +23 -0
- data/lib/circuit_breaker.rb +92 -0
- data/lib/circuit_breaker/circuit_broken_error.rb +3 -0
- data/lib/circuit_breaker/version.rb +3 -0
- data/test/circuit_breaker_test.rb +135 -0
- data/test/test_helper.rb +7 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 19c5a1febb2815b14631a08290d37e85614995ad
|
4
|
+
data.tar.gz: b69c97cc1822d3082498dd4c86cefc3054829a14
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba7df7178c31b07f93fceb26081a59affc43110f4f0bf51335db9437bbcff97394af04050ce03160804900092f1cb5a9b5ea7f95ec01b6cbf003e48ad27e080b
|
7
|
+
data.tar.gz: 4230a0d18be188e4cca94d19bf07e6addc571023738da7f1b4c88f6f1cd0512bf43a1ade11e927b633c9cfab0d9264aaa75c499d28d73b3e9158a517847eb7ac
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Patrick Huesler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# CircuitBreaker
|
2
|
+
|
3
|
+
Implementation of the circuit breaker pattern to protect against high latencies or outages of services.
|
4
|
+
See Martin Fowlers [writeup](http://martinfowler.com/bliki/CircuitBreaker.html) for more information.
|
5
|
+
|
6
|
+
There are other implementations by [wsargent.](https://github.com/wsargent/circuit_breaker) and [soundcloud](https://github.com/soundcloud/simple_circuit_breaker).
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'ya_circuit_breaker'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ya_circuit_breaker
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
````
|
26
|
+
require 'circuit_breaker'
|
27
|
+
|
28
|
+
options = {
|
29
|
+
# number of failures before the circuit breaker trips
|
30
|
+
failure_threshold: 5,
|
31
|
+
# invocation timeout in seconds
|
32
|
+
invocation_timeout: 0.5,
|
33
|
+
# a list of timeouts for consecutive failures in seconds. can be used for exponential backoff
|
34
|
+
reset_timeouts: [2, 4, 8, 16, 32, 64, 128]
|
35
|
+
}
|
36
|
+
|
37
|
+
circuit_breaker = CircuitBreaker::Basic.new(options)
|
38
|
+
|
39
|
+
begin
|
40
|
+
circuit_breaker.execute do
|
41
|
+
http_api_call()
|
42
|
+
end
|
43
|
+
rescue CircuitBreaker::CircuitBrokenError
|
44
|
+
$stderr.puts "Circuit tripped"
|
45
|
+
rescue Timeout::Error
|
46
|
+
$stderr.puts "Call took too long"
|
47
|
+
rescue Error
|
48
|
+
$stderr.puts "Error thrown by 'http_api_call'"
|
49
|
+
end
|
50
|
+
````
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it ( https://github.com/wooga/circuit_breaker/fork )
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'circuit_breaker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ya_circuit_breaker"
|
8
|
+
spec.version = CircuitBreaker::VERSION
|
9
|
+
spec.authors = ["Patrick Huesler", "Nadezda Zryanina"]
|
10
|
+
spec.email = ["patrick.huesler@gmail.com", "n.s.zryanina@gmail.com"]
|
11
|
+
spec.summary = %q{Basic circuit breaker in Ruby}
|
12
|
+
spec.description = %q{Prevent long running external calls from blocking an application}
|
13
|
+
spec.homepage = "https://github.com/wooga/circuit_breaker"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require "circuit_breaker/version"
|
2
|
+
require "circuit_breaker/circuit_broken_error"
|
3
|
+
require "timeout"
|
4
|
+
|
5
|
+
|
6
|
+
module CircuitBreaker
|
7
|
+
class Basic
|
8
|
+
|
9
|
+
DEFAULTS = {
|
10
|
+
failure_threshold: 5,
|
11
|
+
invocation_timeout: 2,
|
12
|
+
reset_timeouts: 10
|
13
|
+
}
|
14
|
+
|
15
|
+
attr_reader :failure_count, :last_failure_time, :failure_threshold
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
options = DEFAULTS.merge(options)
|
19
|
+
@failure_threshold = options[:failure_threshold]
|
20
|
+
@invocation_timeout = options[:invocation_timeout]
|
21
|
+
@reset_timeouts = Array(options[:reset_timeouts])
|
22
|
+
@last_failure_time = nil
|
23
|
+
@failure_count = 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def closed?
|
27
|
+
state == :closed
|
28
|
+
end
|
29
|
+
|
30
|
+
def open?
|
31
|
+
state == :open
|
32
|
+
end
|
33
|
+
|
34
|
+
def half_open?
|
35
|
+
state == :half_open
|
36
|
+
end
|
37
|
+
|
38
|
+
def trip!
|
39
|
+
@failure_count = @failure_threshold
|
40
|
+
@last_failure_time = Time.now
|
41
|
+
end
|
42
|
+
|
43
|
+
def reset!
|
44
|
+
@failure_count = 0
|
45
|
+
@last_failure_time = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def retry_counter
|
49
|
+
@failure_count - @failure_threshold
|
50
|
+
end
|
51
|
+
|
52
|
+
def reset_timeout
|
53
|
+
@reset_timeouts[[@reset_timeouts.size - 1, retry_counter].min]
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def state
|
58
|
+
case
|
59
|
+
when (@failure_count >= @failure_threshold) &&
|
60
|
+
(Time.now - @last_failure_time) > reset_timeout
|
61
|
+
:half_open
|
62
|
+
when @failure_count >= @failure_threshold
|
63
|
+
:open
|
64
|
+
else
|
65
|
+
:closed
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def execute &block
|
70
|
+
if closed? || half_open?
|
71
|
+
begin
|
72
|
+
Timeout::timeout(@invocation_timeout) do
|
73
|
+
block.call if block_given?
|
74
|
+
end
|
75
|
+
reset!
|
76
|
+
rescue Timeout::Error
|
77
|
+
record_failure
|
78
|
+
raise
|
79
|
+
end
|
80
|
+
else
|
81
|
+
raise CircuitBrokenError.new("Circuit is broken")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def record_failure
|
86
|
+
@failure_count += 1
|
87
|
+
@last_failure_time = Time.now
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
def successful_method
|
5
|
+
nil
|
6
|
+
end
|
7
|
+
|
8
|
+
def slow_method(sleep_time)
|
9
|
+
sleep(sleep_time)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe CircuitBreaker::Basic do
|
13
|
+
|
14
|
+
it "should initialize in a closed state" do
|
15
|
+
assert CircuitBreaker::Basic.new.closed?
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should trip the circuit breaker" do
|
19
|
+
circuit_breaker = CircuitBreaker::Basic.new
|
20
|
+
circuit_breaker.trip!
|
21
|
+
|
22
|
+
assert circuit_breaker.open?
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should reset the circuit breaker" do
|
26
|
+
circuit_breaker = CircuitBreaker::Basic.new
|
27
|
+
circuit_breaker.trip!
|
28
|
+
assert circuit_breaker.open?
|
29
|
+
|
30
|
+
circuit_breaker.reset!
|
31
|
+
|
32
|
+
assert circuit_breaker.closed?
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should execute the call in an closed state" do
|
36
|
+
circuit_breaker = CircuitBreaker::Basic.new
|
37
|
+
assert CircuitBreaker::Basic.new.closed?
|
38
|
+
|
39
|
+
@mock = MiniTest::Mock.new
|
40
|
+
@mock.expect(:do_something, nil)
|
41
|
+
|
42
|
+
circuit_breaker.execute do
|
43
|
+
@mock.do_something
|
44
|
+
end
|
45
|
+
|
46
|
+
@mock.verify
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should trip after the failure threshold has been exceeded" do
|
50
|
+
invocation_timeout = 0.1
|
51
|
+
circuit_breaker = CircuitBreaker::Basic.new(failure_threshold: 1, invocation_timeout: invocation_timeout)
|
52
|
+
|
53
|
+
assert_raises Timeout::Error do
|
54
|
+
circuit_breaker.execute do
|
55
|
+
slow_method(invocation_timeout + 0.1)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
assert_raises CircuitBreaker::CircuitBrokenError do
|
60
|
+
circuit_breaker.execute do
|
61
|
+
assert false, "The call should not be executed in open state"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should reset after reset timeout" do
|
67
|
+
reset_timeouts = 0.1
|
68
|
+
circuit_breaker = CircuitBreaker::Basic.new(reset_timeouts: reset_timeouts)
|
69
|
+
|
70
|
+
# switch into an open state
|
71
|
+
circuit_breaker.trip!
|
72
|
+
|
73
|
+
assert circuit_breaker.open?
|
74
|
+
|
75
|
+
sleep(reset_timeouts)
|
76
|
+
|
77
|
+
assert circuit_breaker.half_open?
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should change from half open to closed on success" do
|
81
|
+
reset_timeouts = 0.1
|
82
|
+
circuit_breaker = CircuitBreaker::Basic.new(reset_timeouts: reset_timeouts)
|
83
|
+
|
84
|
+
# switch into an open state
|
85
|
+
circuit_breaker.trip!
|
86
|
+
|
87
|
+
sleep(reset_timeouts)
|
88
|
+
|
89
|
+
assert circuit_breaker.half_open?
|
90
|
+
|
91
|
+
circuit_breaker.execute do
|
92
|
+
successful_method
|
93
|
+
end
|
94
|
+
|
95
|
+
assert circuit_breaker.closed?
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should change from hald open to closed on failure" do
|
99
|
+
invocation_timeout = 0.1
|
100
|
+
reset_timeouts = 0.1
|
101
|
+
circuit_breaker = CircuitBreaker::Basic.new(invocation_timeout: invocation_timeout, reset_timeouts: reset_timeouts)
|
102
|
+
circuit_breaker.trip!
|
103
|
+
|
104
|
+
sleep(reset_timeouts)
|
105
|
+
|
106
|
+
assert circuit_breaker.half_open?
|
107
|
+
|
108
|
+
assert_raises Timeout::Error do
|
109
|
+
circuit_breaker.execute do
|
110
|
+
slow_method(invocation_timeout + 0.1)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
assert circuit_breaker.open?
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should back off exponentially" do
|
118
|
+
invocation_timeout = 0.1
|
119
|
+
reset_timeout = 0.1
|
120
|
+
reset_timeouts = [reset_timeout, 2 * reset_timeout, 3 * reset_timeout]
|
121
|
+
circuit_breaker = CircuitBreaker::Basic.new(failure_threshold: 1, invocation_timeout: invocation_timeout, reset_timeouts: reset_timeouts)
|
122
|
+
|
123
|
+
reset_timeouts.each do |timeout|
|
124
|
+
assert_raises Timeout::Error do
|
125
|
+
circuit_breaker.execute do
|
126
|
+
slow_method(invocation_timeout + 0.1)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
assert circuit_breaker.open?
|
130
|
+
sleep(timeout)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ya_circuit_breaker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patrick Huesler
|
8
|
+
- Nadezda Zryanina
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.5'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.5'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
description: Prevent long running external calls from blocking an application
|
43
|
+
email:
|
44
|
+
- patrick.huesler@gmail.com
|
45
|
+
- n.s.zryanina@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- circuit_breaker.gemspec
|
57
|
+
- lib/circuit_breaker.rb
|
58
|
+
- lib/circuit_breaker/circuit_broken_error.rb
|
59
|
+
- lib/circuit_breaker/version.rb
|
60
|
+
- test/circuit_breaker_test.rb
|
61
|
+
- test/test_helper.rb
|
62
|
+
homepage: https://github.com/wooga/circuit_breaker
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.2.1
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Basic circuit breaker in Ruby
|
86
|
+
test_files:
|
87
|
+
- test/circuit_breaker_test.rb
|
88
|
+
- test/test_helper.rb
|