rcb 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '029e8ceda425ad3aea544142ab9307fd74ebff19fa98e359d791d820528f5638'
4
- data.tar.gz: daf71c637318058ef6647531544c4fbe77404e08e0e705778ea77623f53d461d
3
+ metadata.gz: 51b22ffe23d17fa75f779c6c40bc810ff5d68e89a90a7ac3ff2f970f2974264e
4
+ data.tar.gz: 4741eaa76f5f29794c0dcdd83070cbec2c4544aa07ca8177a3af52fd8de5f221
5
5
  SHA512:
6
- metadata.gz: 79cf1a6056c40f5abf501905ea0a3ac12e98eaaafd86a45e537b4db66ce343b6bc7c759c5ba74a7c7a0f1c7d66ec4e4c4f881c8e02f6627cbb89acab426fba29
7
- data.tar.gz: b268096b37b3a3d13f7a7367ad5491b02dd65bbf21aa47637caa7f7afb67586470917b984913cf016e23d0d0c8020cddc0a4dbce16067189a2a79b95b4bc8df3
6
+ metadata.gz: 12426ff000383b3aaf8cfa02aac6ab5b71b903c0636ac8c9d4a2169306c2a113eb4445585fa7565c629a26a541fb0d02e0d12885a1b6e8be03bac05a266797c1
7
+ data.tar.gz: eec801904cd87bcaae6b8b58e3e86c40339587e127ba16b3f79f725ec3755376a338ceee6acd10b28c638c36ea8ce850ae7e2f524d1811b73680de18e34f2540
data/README.md CHANGED
@@ -45,20 +45,25 @@ Add configuration with specifying `tag` like:
45
45
 
46
46
  ```ruby
47
47
  Rcb.configure('fooo.com') do |config|
48
- config.max_failure_count 1
48
+ config.open_condition.max_failure_count 1
49
+ config.open_condition.window_msec 1000
49
50
  config.reset_timeout_msec 100
50
51
  end
51
52
 
52
53
  Rcb.configure('hoge.com') do |config|
53
- config.max_failure_count 3
54
+ config.open_condition.max_failure_count 3
55
+ config.open_condition.window_msec 3000
54
56
  config.reset_timeout_msec 300
55
57
  end
56
58
  ```
57
59
 
58
60
  These configurations are for both 'fooo.com' and 'hoge.com'.
59
61
 
60
- - `max_failure_count`: a threshold for execution failures.
61
- - if the breaker reaches the given `max_failure_count`, enters Open state from Close
62
+ - `open_condition`: a condition to decide whether trip to open from close
63
+ - `max_failure_count`: a threshold for execution failures.
64
+ - if the breaker reaches the given `max_failure_count`, enters Open state from Close
65
+ - `window_msec`: a window time(milliseconds) for failures
66
+ - if the breaker catched failures more than `window_msec` before, they are discarded
62
67
  - `reset_timeout_msec`: milliseconds to wait for next try
63
68
  - after the given `reset_timeout_msec`, the circuit breaker enters a Half-Open state from Open
64
69
 
data/lib/rcb/instance.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rstructural'
2
2
  require_relative './state'
3
+ require_relative './state_store'
3
4
  require_relative './configuration'
4
5
  require_relative './error'
5
6
 
@@ -8,13 +9,14 @@ module Rcb
8
9
  attr_reader :config
9
10
 
10
11
  # @param config [Rcb::Config]
11
- def initialize(config)
12
+ def initialize(config, state_store:)
12
13
  @config = config
14
+ @state_store = state_store || Rcb::StateStore::InMemory
13
15
  end
14
16
 
15
17
  def run!(&block)
16
18
  result =
17
- case States.of(config.tag)
19
+ case get_state
18
20
  in State::Close => s
19
21
  s.run(config, &block)
20
22
  in State::Open => s
@@ -25,40 +27,23 @@ module Rcb
25
27
 
26
28
  case result
27
29
  in Result::Ok[state, result]
28
- States.update(config.tag, state)
30
+ @state_store.update(config.tag, state)
29
31
  return result
30
32
  in Result::Ng[state, error]
31
- States.update(config.tag, state)
33
+ @state_store.update(config.tag, state)
32
34
  raise error
33
35
  end
34
36
  end
35
37
 
36
38
  def state
37
- States.show(config)
39
+ get_state.show_state(config)
38
40
  end
39
41
 
40
- end
41
-
42
- private
43
-
44
- class States
45
- @states = {}
42
+ private
46
43
 
47
- def self.show(config)
48
- self.of(config.tag).state(config)
49
- end
50
-
51
- def self.of(tag)
52
- @states[tag.to_sym] ||= State::Close.create
53
- end
54
-
55
- def self.update(tag, state)
56
- @states[tag] = state
57
- end
58
-
59
- def self.clear
60
- @states = {}
61
- end
44
+ def get_state
45
+ @state_store.get(config.tag) || State::Close.create
62
46
  end
47
+ end
63
48
 
64
49
  end
data/lib/rcb/state.rb CHANGED
@@ -57,7 +57,7 @@ module Rcb::State
57
57
  end
58
58
 
59
59
  interface do
60
- def state(config)
60
+ def show_state(config)
61
61
  case self
62
62
  in Open if self.half_open?(config.reset_timeout_msec)
63
63
  :half_open
@@ -0,0 +1,22 @@
1
+ require_relative './state'
2
+ require_relative './configuration'
3
+
4
+ module Rcb
5
+ module StateStore
6
+ class InMemory
7
+ @states = {}
8
+
9
+ def self.get(tag)
10
+ @states[tag.to_sym]
11
+ end
12
+
13
+ def self.update(tag, state)
14
+ @states[tag] = state
15
+ end
16
+
17
+ def self.clear
18
+ @states = {}
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/rcb.rb CHANGED
@@ -3,11 +3,11 @@ require_relative "./rcb/configuration"
3
3
 
4
4
  module Rcb
5
5
  module ClassMethods
6
- def for(tag, open_condition: nil, reset_timeout_msec: nil)
6
+ def for(tag, state_store: nil, open_condition: nil, reset_timeout_msec: nil)
7
7
  config = Rcb::Configurations.for(tag,
8
8
  open_condition: open_condition,
9
9
  reset_timeout_msec: reset_timeout_msec)
10
- Instance.new(config)
10
+ Instance.new(config, state_store: state_store)
11
11
  end
12
12
  end
13
13
 
data/rcb.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "rcb"
3
- spec.version = '0.2.0'
3
+ spec.version = '0.3.0'
4
4
  spec.authors = ["petitviolet"]
5
5
  spec.email = ["violethero0820@gmail.com"]
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rcb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - petitviolet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-25 00:00:00.000000000 Z
11
+ date: 2020-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rstructural
@@ -88,6 +88,7 @@ files:
88
88
  - lib/rcb/instance.rb
89
89
  - lib/rcb/result.rb
90
90
  - lib/rcb/state.rb
91
+ - lib/rcb/state_store.rb
91
92
  - rcb.gemspec
92
93
  homepage: https://github.com/petitviolet/rcb
93
94
  licenses: