circuitbox 2.0.0.pre1 → 2.0.0.pre2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98b0453c7aac74192f9ecfe6e1409cd18f11dcc186deac73eeff80b1d8e8b948
4
- data.tar.gz: 390bebb8b034c0d40de18d35aaa2785ba40bef371fe282fc0f3a726f176d7483
3
+ metadata.gz: 8b5dd933e4972d92fa8a3715fb5546b99442f50ca34056a4fe90faec5586377d
4
+ data.tar.gz: 3391ed4af7fbb92abfebd1f95f814d6c2dac42752a45f0cf001f57ffc2f8ee42
5
5
  SHA512:
6
- metadata.gz: d9cb247b2544c06f9223457ea52a9c67453c3994a7f441597c1cc3f20d39c4219cbda02c9a944255cbe7d96d8052d2d9fc3b3fe292ae9a7ee0cea6091026396d
7
- data.tar.gz: e9aef10565a19824743a88698ea82c6756e2e2d55c5941f1b919c4cf9c8979cc7731d1ce07d3b3d73af6b2adc49e7ae29e75d8bf7a50af6fc472482874490ea8
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[:your_service, exceptions: [Net::ReadTimeout]] do
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
  ```
@@ -12,11 +12,7 @@ class Circuitbox
12
12
  class << self
13
13
  include Configuration
14
14
 
15
- def [](service_name, options = {})
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 open_flag?
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
- if open_flag?
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 open_flag? || !half_open?
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 open_flag?
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 !open_flag? && circuit_store.delete(half_open_storage_key)
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
- notify_and_increment_event('success')
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
- notify_and_increment_event('failure')
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
- # Send notification and increment stat store
209
- def notify_and_increment_event(event)
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,4 +1,6 @@
1
1
  require_relative 'memory_store'
2
+ require_relative 'timer/monotonic'
3
+ require_relative 'timer/null'
2
4
  require_relative 'timer/simple'
3
5
  require_relative 'notifier/active_support'
4
6
  require_relative 'notifier/null'
@@ -1,6 +1,6 @@
1
- # frozen-string-literal: true
1
+ # frozen_string_literal: true
2
2
 
3
- require_relative 'memory_store/compactor'
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
- @compactor = Compactor.new(store: @store, frequency: compaction_frequency)
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
- @compactor.run
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.expired?
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
@@ -1,3 +1,3 @@
1
1
  class Circuitbox
2
- VERSION = '2.0.0.pre1'.freeze
2
+ VERSION = '2.0.0.pre2'.freeze
3
3
  end
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.pre1
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-01-10 00:00:00.000000000 Z
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