circuit_breakage 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +59 -10
- data/circuit_breakage.gemspec +3 -3
- data/lib/circuit_breakage/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf00e430eba8f24feacafd2a94ebfc2e9ce53737
|
4
|
+
data.tar.gz: d6550c42f12736a6a87a6059df53ba2315fa320b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5752657e3fd2e10342190cbfab8539068087cb145a07070f02091dbdaa0da50249afede7656def46eef30dd524f242041e7c5ff0bc8573ef56a5aae509b0a79c
|
7
|
+
data.tar.gz: a51e540148c42f4de8d3008b40a14b77ae680010ae4d64f4f404b04cb0793822d9e850c63f29e1b82c45e4967a571aaf0920e522ecf9f5e07ce6293b9fb39699
|
data/README.md
CHANGED
@@ -10,20 +10,20 @@ description of the pattern.
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
###
|
13
|
+
### Simple Example
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
|
16
|
+
proc = ->(*args) do
|
17
17
|
# Some dangerous thing.
|
18
18
|
end
|
19
19
|
|
20
|
-
breaker = CircuitBreakage::Breaker.new(
|
21
|
-
breaker.failure_threshold =
|
22
|
-
breaker.duration
|
23
|
-
breaker.timeout
|
20
|
+
breaker = CircuitBreakage::Breaker.new(proc)
|
21
|
+
breaker.failure_threshold = 3 # only 3 failures before tripping circuit
|
22
|
+
breaker.duration = 10 # 10 seconds before retry
|
23
|
+
breaker.timeout = 0.5 # 500 milliseconds allowed before auto-fail
|
24
24
|
|
25
25
|
begin
|
26
|
-
breaker.call(*some_args) # args are passed through to
|
26
|
+
breaker.call(*some_args) # args are passed through to the proc
|
27
27
|
rescue CircuitBreakage::CircuitOpen
|
28
28
|
puts "Too many recent failures!"
|
29
29
|
rescue CircuitBreakage::CircuitTimeout
|
@@ -31,9 +31,58 @@ rescue CircuitBreakage::CircuitTimeout
|
|
31
31
|
end
|
32
32
|
```
|
33
33
|
|
34
|
-
A "failure" in this context means that the
|
34
|
+
A "failure" in this context means that the proc either raised an exception or
|
35
35
|
timed out.
|
36
36
|
|
37
|
+
### Slightly More Complex Example in Rails
|
38
|
+
|
39
|
+
This example shows one way you might choose to wrap a remote service call in a
|
40
|
+
Rails app.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# in your controller
|
44
|
+
class MyController
|
45
|
+
def show
|
46
|
+
widget_client = WidgetClient.new
|
47
|
+
@widgets = widget_client.get_widget(id)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# in lib/widget_client.rb
|
52
|
+
class WidgetClient
|
53
|
+
class << self
|
54
|
+
def breaker
|
55
|
+
if @breaker.nil?
|
56
|
+
@breaker = CircuitBreakage::Breaker.new method(:do_get_widget)
|
57
|
+
@breaker.failure_threshold = 3
|
58
|
+
@breaker.duration = 10
|
59
|
+
@breaker.timeout = 0.5
|
60
|
+
end
|
61
|
+
|
62
|
+
return @breaker
|
63
|
+
end
|
64
|
+
|
65
|
+
def do_get_widget(id)
|
66
|
+
# Do the remote service call here.
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def get_widget(id)
|
71
|
+
class.breaker.call(id)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
This makes it easy to control all calls to the remote service with a single
|
77
|
+
breaker. This way, they are all tripped and reset together, even if the service
|
78
|
+
is called by several different parts of your code. Don't forget to handle
|
79
|
+
errors somewhere -- probably either the controller or the client library,
|
80
|
+
depending on the needs of your code.
|
81
|
+
|
82
|
+
Note that we've actually used a `Method` object rather than a proc to
|
83
|
+
initialize the circuit breaker. That's fine -- breakers will actually work with
|
84
|
+
any object that responds to `call`.
|
85
|
+
|
37
86
|
### Redis-Backed "Shared" Circuit Breakers
|
38
87
|
|
39
88
|
The unique feature of this particular Circuit Breaker gem is that it also
|
@@ -50,7 +99,7 @@ the retry timer as appropriate.
|
|
50
99
|
connection = some_redis_connection
|
51
100
|
key = 'my_app/this_operation'
|
52
101
|
|
53
|
-
breaker = CircuitBreakage::RedisBackedBreaker.new(connection, key,
|
102
|
+
breaker = CircuitBreakage::RedisBackedBreaker.new(connection, key, proc)
|
54
103
|
breaker.lock_timeout = 30 # seconds before assuming a locking process has crashed
|
55
104
|
|
56
105
|
# Everything else is the same as above.
|
@@ -59,7 +108,7 @@ breaker.lock_timeout = 30 # seconds before assuming a locking process has crash
|
|
59
108
|
The `lock_timeout` setting is necessary since a process that crashes or is
|
60
109
|
killed might be holding the retry lock. This sets the amount of time other
|
61
110
|
processes will wait before deciding a lock has expired. It should be longer
|
62
|
-
than the amount of time you expect the
|
111
|
+
than the amount of time you expect the proc to take to run.
|
63
112
|
|
64
113
|
All circuit breakers using the same key and the same Redis instance will share
|
65
114
|
their state . It is strongly recommended that their settings
|
data/circuit_breakage.gemspec
CHANGED
@@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["John Hyland"]
|
10
10
|
spec.email = ["john@djspinmonkey.com"]
|
11
11
|
spec.summary = %q{Provides a simple circuit breaker pattern.}
|
12
|
-
spec.description = %q{Provides a circuit breaker pattern with
|
13
|
-
spec.homepage = "https://
|
14
|
-
spec.license = "
|
12
|
+
spec.description = %q{Provides a circuit breaker pattern with optional support for distributed state.}
|
13
|
+
spec.homepage = "https://github.com/djspinmonkey/circuit_breakage"
|
14
|
+
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: circuit_breakage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Hyland
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,8 +66,8 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description: Provides a circuit breaker pattern with
|
70
|
-
|
69
|
+
description: Provides a circuit breaker pattern with optional support for distributed
|
70
|
+
state.
|
71
71
|
email:
|
72
72
|
- john@djspinmonkey.com
|
73
73
|
executables: []
|
@@ -88,9 +88,9 @@ files:
|
|
88
88
|
- spec/breaker_spec.rb
|
89
89
|
- spec/redis_backed_breaker_spec.rb
|
90
90
|
- spec/spec_helper.rb
|
91
|
-
homepage: https://
|
91
|
+
homepage: https://github.com/djspinmonkey/circuit_breakage
|
92
92
|
licenses:
|
93
|
-
-
|
93
|
+
- MIT
|
94
94
|
metadata: {}
|
95
95
|
post_install_message:
|
96
96
|
rdoc_options: []
|