circuitbox 2.0.0.pre1 → 2.0.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/circuitbox.rb +1 -5
- data/lib/circuitbox/circuit_breaker.rb +10 -18
- data/lib/circuitbox/configuration.rb +2 -0
- data/lib/circuitbox/memory_store.rb +13 -5
- data/lib/circuitbox/version.rb +1 -1
- metadata +2 -3
- data/lib/circuitbox/memory_store/compactor.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b5dd933e4972d92fa8a3715fb5546b99442f50ca34056a4fe90faec5586377d
|
4
|
+
data.tar.gz: 3391ed4af7fbb92abfebd1f95f814d6c2dac42752a45f0cf001f57ffc2f8ee42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de9aede3e20400e85a98b821b5cc56034effd8dac20ff8486ef51c8de48019b16a3e4ee152b2509029e5fd1ba7432875d745343b88998a3c971bf470bc60e81c
|
7
|
+
data.tar.gz: 716dc649ca0f3da37a7de004f2609ddee6fbdb0d217dab1fe9d6835ab24224957b1b8ce0b5da916ae21e905f8578fa771c613be7433d4e260beee480f5eb5023
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ Resources about the circuit breaker pattern:
|
|
10
10
|
## Usage
|
11
11
|
|
12
12
|
```ruby
|
13
|
-
Circuitbox
|
13
|
+
Circuitbox.circuit(:your_service, exceptions: [Net::ReadTimeout]) do
|
14
14
|
Net::HTTP.get URI('http://example.com/api/messages')
|
15
15
|
end
|
16
16
|
```
|
data/lib/circuitbox.rb
CHANGED
@@ -12,11 +12,7 @@ class Circuitbox
|
|
12
12
|
class << self
|
13
13
|
include Configuration
|
14
14
|
|
15
|
-
def
|
16
|
-
circuit(service_name, options)
|
17
|
-
end
|
18
|
-
|
19
|
-
def circuit(service_name, options = {})
|
15
|
+
def circuit(service_name, options)
|
20
16
|
circuit = (cached_circuits[service_name] ||= CircuitBreaker.new(service_name, options))
|
21
17
|
|
22
18
|
return circuit unless block_given?
|
@@ -53,7 +53,7 @@ class Circuitbox
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def run(circuitbox_exceptions: true)
|
56
|
-
if
|
56
|
+
if open?
|
57
57
|
skipped!
|
58
58
|
raise Circuitbox::OpenCircuitError.new(service) if circuitbox_exceptions
|
59
59
|
else
|
@@ -78,11 +78,7 @@ class Circuitbox
|
|
78
78
|
end
|
79
79
|
|
80
80
|
def open?
|
81
|
-
|
82
|
-
true
|
83
|
-
else
|
84
|
-
false
|
85
|
-
end
|
81
|
+
circuit_store.key?(open_storage_key)
|
86
82
|
end
|
87
83
|
|
88
84
|
def error_rate(failures = failure_count, success = success_count)
|
@@ -123,7 +119,7 @@ class Circuitbox
|
|
123
119
|
|
124
120
|
def half_open_failure
|
125
121
|
@state_change_mutex.synchronize do
|
126
|
-
return if
|
122
|
+
return if open? || !half_open?
|
127
123
|
|
128
124
|
trip
|
129
125
|
end
|
@@ -135,7 +131,7 @@ class Circuitbox
|
|
135
131
|
|
136
132
|
def open!
|
137
133
|
@state_change_mutex.synchronize do
|
138
|
-
return if
|
134
|
+
return if open?
|
139
135
|
|
140
136
|
trip
|
141
137
|
end
|
@@ -160,7 +156,7 @@ class Circuitbox
|
|
160
156
|
# If the circuit is not open, the half_open key will be deleted from the store
|
161
157
|
# if half_open exists the deleted value is returned and allows us to continue
|
162
158
|
# if half_open doesn't exist nil is returned, causing us to return early
|
163
|
-
return unless !
|
159
|
+
return unless !open? && circuit_store.delete(half_open_storage_key)
|
164
160
|
end
|
165
161
|
|
166
162
|
# Running event outside of the synchronize block to allow other threads
|
@@ -169,23 +165,19 @@ class Circuitbox
|
|
169
165
|
logger.debug(circuit_closed_message)
|
170
166
|
end
|
171
167
|
|
172
|
-
def open_flag?
|
173
|
-
circuit_store.key?(open_storage_key)
|
174
|
-
end
|
175
|
-
|
176
168
|
def half_open?
|
177
169
|
circuit_store.key?(half_open_storage_key)
|
178
170
|
end
|
179
171
|
|
180
172
|
def success!
|
181
|
-
|
173
|
+
increment_and_notify_event('success')
|
182
174
|
logger.debug(circuit_success_message)
|
183
175
|
|
184
176
|
close! if half_open?
|
185
177
|
end
|
186
178
|
|
187
179
|
def failure!
|
188
|
-
|
180
|
+
increment_and_notify_event('failure')
|
189
181
|
logger.debug(circuit_failure_message)
|
190
182
|
|
191
183
|
if half_open?
|
@@ -205,10 +197,10 @@ class Circuitbox
|
|
205
197
|
notifier.notify(service, event)
|
206
198
|
end
|
207
199
|
|
208
|
-
#
|
209
|
-
def
|
210
|
-
notify_event(event)
|
200
|
+
# Increment stat store and send notification
|
201
|
+
def increment_and_notify_event(event)
|
211
202
|
circuit_store.increment(stat_storage_key(event), 1, expires: (option_value(:time_window) * 2))
|
203
|
+
notify_event(event)
|
212
204
|
end
|
213
205
|
|
214
206
|
def check_sleep_window
|
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative 'memory_store/
|
3
|
+
require_relative 'memory_store/monotonic_time'
|
4
4
|
require_relative 'memory_store/container'
|
5
5
|
|
6
6
|
class Circuitbox
|
@@ -10,7 +10,8 @@ class Circuitbox
|
|
10
10
|
def initialize(compaction_frequency: 60)
|
11
11
|
@store = {}
|
12
12
|
@mutex = Mutex.new
|
13
|
-
@
|
13
|
+
@compaction_frequency = compaction_frequency
|
14
|
+
@compact_after = current_second + compaction_frequency
|
14
15
|
end
|
15
16
|
|
16
17
|
def store(key, value, opts = {})
|
@@ -53,13 +54,15 @@ class Circuitbox
|
|
53
54
|
private
|
54
55
|
|
55
56
|
def fetch_container(key)
|
56
|
-
|
57
|
+
current_time = current_second
|
58
|
+
|
59
|
+
compact(current_time) if @compact_after < current_time
|
57
60
|
|
58
61
|
container = @store[key]
|
59
62
|
|
60
63
|
return unless container
|
61
64
|
|
62
|
-
if container.
|
65
|
+
if container.expired_at?(current_time)
|
63
66
|
@store.delete(key)
|
64
67
|
nil
|
65
68
|
else
|
@@ -72,5 +75,10 @@ class Circuitbox
|
|
72
75
|
return unless container
|
73
76
|
container.value
|
74
77
|
end
|
78
|
+
|
79
|
+
def compact(current_time)
|
80
|
+
@store.delete_if { |_, value| value.expired_at?(current_time) }
|
81
|
+
@compact_after = current_time + @compaction_frequency
|
82
|
+
end
|
75
83
|
end
|
76
84
|
end
|
data/lib/circuitbox/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: circuitbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.
|
4
|
+
version: 2.0.0.pre2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fahim Ferdous
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-05-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -198,7 +198,6 @@ files:
|
|
198
198
|
- lib/circuitbox/excon_middleware.rb
|
199
199
|
- lib/circuitbox/faraday_middleware.rb
|
200
200
|
- lib/circuitbox/memory_store.rb
|
201
|
-
- lib/circuitbox/memory_store/compactor.rb
|
202
201
|
- lib/circuitbox/memory_store/container.rb
|
203
202
|
- lib/circuitbox/memory_store/monotonic_time.rb
|
204
203
|
- lib/circuitbox/notifier/active_support.rb
|
@@ -1,35 +0,0 @@
|
|
1
|
-
# frozen-string-literal: true
|
2
|
-
|
3
|
-
require_relative 'monotonic_time'
|
4
|
-
|
5
|
-
class Circuitbox
|
6
|
-
class MemoryStore
|
7
|
-
class Compactor
|
8
|
-
include MonotonicTime
|
9
|
-
|
10
|
-
attr_reader :store, :frequency, :compact_after
|
11
|
-
|
12
|
-
def initialize(store:, frequency:)
|
13
|
-
@store = store
|
14
|
-
@frequency = frequency
|
15
|
-
set_next_compaction_time
|
16
|
-
end
|
17
|
-
|
18
|
-
def run
|
19
|
-
compaction_attempted_at = current_second
|
20
|
-
|
21
|
-
return unless compact_after < compaction_attempted_at
|
22
|
-
|
23
|
-
@store.delete_if { |_, value| value.expired_at?(compaction_attempted_at) }
|
24
|
-
|
25
|
-
set_next_compaction_time
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def set_next_compaction_time
|
31
|
-
@compact_after = current_second + frequency
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|