circuit-hiatus 0.4.1 → 0.5.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 +4 -4
- data/Gemfile.lock +7 -5
- data/README.md +90 -19
- data/{hiatus.gemspec → circuit-hiatus.gemspec} +1 -1
- data/lib/hiatus/mixin.rb +23 -9
- data/lib/hiatus/version.rb +1 -1
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 825bf2d8532d2256b192f94bf2849ff967fb707fb10dd2119e365443111c7344
|
4
|
+
data.tar.gz: d62210a847b67eb4b62afac769e661e8fb78b0c8c457db7b27f90f51478290a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebff39e24f74d3a901e452cd3bd92296c997ed1012d1a689f3ec58340b447e4ac8bb10045778342d231c4cddd52b5e17aba00bbc02fc5b75d053afdefe5f67a0
|
7
|
+
data.tar.gz: '0804b534b0bd125ea612d64148ed52a35e5a2116ee37d665e3e6a0abe9e6b053bb24c56ef839bd4ffc4f7f4aeae86bf10e25683f898388d63cc1ad96136c9ee5'
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
circuit-hiatus (0.
|
4
|
+
circuit-hiatus (0.5.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -12,6 +12,7 @@ GEM
|
|
12
12
|
httparty (0.18.1)
|
13
13
|
mime-types (~> 3.0)
|
14
14
|
multi_xml (>= 0.5.2)
|
15
|
+
json (2.3.1)
|
15
16
|
method_source (1.0.0)
|
16
17
|
mime-types (3.3.1)
|
17
18
|
mime-types-data (~> 3.2015)
|
@@ -34,10 +35,11 @@ GEM
|
|
34
35
|
diff-lcs (>= 1.2.0, < 2.0)
|
35
36
|
rspec-support (~> 3.9.0)
|
36
37
|
rspec-support (3.9.3)
|
37
|
-
simplecov (0.
|
38
|
+
simplecov (0.17.1)
|
38
39
|
docile (~> 1.1)
|
39
|
-
|
40
|
-
|
40
|
+
json (>= 1.8, < 3)
|
41
|
+
simplecov-html (~> 0.10.0)
|
42
|
+
simplecov-html (0.10.2)
|
41
43
|
timecop (0.9.1)
|
42
44
|
|
43
45
|
PLATFORMS
|
@@ -49,7 +51,7 @@ DEPENDENCIES
|
|
49
51
|
pry
|
50
52
|
rake (~> 12.0)
|
51
53
|
rspec (~> 3.0)
|
52
|
-
simplecov
|
54
|
+
simplecov (= 0.17.1)
|
53
55
|
timecop
|
54
56
|
|
55
57
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -4,6 +4,11 @@
|
|
4
4
|
+ *a pause or gap in a sequence, series, or process.*
|
5
5
|
+ *A temporary gap, pause, break*
|
6
6
|
|
7
|
+
|
8
|
+
[](https://codeclimate.com/github/cesartalves/hiatus/maintainability)
|
9
|
+
[](https://codeclimate.com/github/cesartalves/hiatus/test_coverage)
|
10
|
+
[](https://travis-ci.org/cesartalves/hiatus)
|
11
|
+
|
7
12
|
Dead simple circuit breaker pattern implementation.
|
8
13
|
|
9
14
|
Currently this is an unambitious project for future reference and talks. Thought the pattern was interesting and wanted to give it a shot :)
|
@@ -18,13 +23,16 @@ Here is a great article about it: https://martinfowler.com/bliki/CircuitBreaker.
|
|
18
23
|
|
19
24
|
## Installation
|
20
25
|
|
21
|
-
Add `gem 'hiatus'
|
26
|
+
Add `gem 'circuit-hiatus'` to your Gemfile and `bundle install`, or `gem install circuit-hiatus`
|
22
27
|
|
23
28
|
## Usage
|
24
29
|
|
25
|
-
Once threshold is reached, will raise a `Hiatus:CircuitBrokenError`
|
30
|
+
Once threshold is reached, the circuit will raise a `Hiatus:CircuitBrokenError` until `half_open_interval` passes. Then, it will half-open: a failed call trips the circuit again, a successful call closes it.
|
26
31
|
|
27
32
|
```ruby
|
33
|
+
|
34
|
+
require 'hiatus' # not circuit-hiatus!
|
35
|
+
|
28
36
|
threshold = Hiatus::CountThreshold.new 5 # will be broken on the 5th failed attempt
|
29
37
|
|
30
38
|
circuit_breaker = Hiatus::CircuitBreaker.new threshold: threshold, half_open_interval: 60 # after 60 seconds, circuit is apt to make calls again
|
@@ -47,42 +55,105 @@ end # => Hiatus:CircuitBrokenError
|
|
47
55
|
You can also include a Mixin in your class, since apparently this is what the ruby kids like. No idea why you'd like to tie your code to my little gem, but here it goes:
|
48
56
|
|
49
57
|
```ruby
|
50
|
-
|
58
|
+
require 'hiatus'
|
59
|
+
|
60
|
+
class Service
|
61
|
+
|
62
|
+
include Hiatus::Mixin
|
63
|
+
|
64
|
+
# this gives you great flexiblity on which circuit to use
|
65
|
+
# you can even have your classes sharing the same circuit breaker :)
|
66
|
+
circuit_factory -> { Hiatus::Circuits::CountCircuitBreaker.new threshold: 3 }
|
67
|
+
|
68
|
+
circuit_protected def call
|
69
|
+
raise 'Error'
|
70
|
+
end
|
71
|
+
|
72
|
+
# Check `spec/hiatus_mixin_spec.rb` for the full example
|
73
|
+
|
74
|
+
```
|
75
|
+
|
76
|
+
To configure a default factory:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
|
80
|
+
Hiatus::Mixin.config do |c|
|
81
|
+
c.default_circuit_factory = { Hiatus::Circuits::PercentageCircuitBreaker.new threshold: 0.5 }
|
82
|
+
end
|
83
|
+
|
84
|
+
```
|
85
|
+
|
86
|
+
*Available Circuit types*
|
87
|
+
|
88
|
+
- `Hiatus::Circuits::PercentageCircuitBreaker` => shorthand for circuit with threshold= `Hiatus::CountThreshold.new`. Opens once requests fails enough times
|
89
|
+
|
90
|
+
- `Hiatus::Circuits::CountCircuitBreaker` => shorthand for circuit with threshold= `Hiatus::PercentageThreshold.new`. Opens once failure percentage passes the given threshold
|
51
91
|
|
52
|
-
|
92
|
+
## Advanced configuration
|
53
93
|
|
54
|
-
|
55
|
-
# you can even have your classes sharing the same circuit breaker :)
|
56
|
-
circuit_factory -> { Hiatus::Circuits::CountCircuitBreaker.new threshold: 3 }
|
94
|
+
You can easily create new instances of Threshold, so that they can be stored anywhere (database, redis, your dog's house - anything that responds to the interface), effectively creating a circuit breaker that can be used across applications:
|
57
95
|
|
58
|
-
|
59
|
-
|
96
|
+
```ruby
|
97
|
+
|
98
|
+
class RedisThreshold < CountThreshold
|
99
|
+
def initialize(options)
|
100
|
+
|
101
|
+
def increment
|
102
|
+
super
|
103
|
+
serialize
|
60
104
|
end
|
61
105
|
|
62
|
-
|
106
|
+
def reached?
|
107
|
+
failure_count, threshold = *deserialize
|
108
|
+
failure_count >= threshold
|
109
|
+
end
|
110
|
+
|
111
|
+
def reset
|
112
|
+
super
|
113
|
+
serialize
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def deserialize
|
119
|
+
# get stuff from redis here
|
120
|
+
end
|
121
|
+
|
122
|
+
def serialize
|
123
|
+
# put stuff into redis here
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
threshold = RedisThreshold.new(url:, threshold: 15)
|
128
|
+
|
129
|
+
redis_cb = Hiatus::CircuitBreaker.new threshold: threshold, half_open_interval: 90
|
63
130
|
|
64
131
|
```
|
65
132
|
|
66
|
-
|
133
|
+
You can find an example of a Threshold stored in a file in extras/file_count_threshold.rb
|
67
134
|
|
68
|
-
|
135
|
+
## Possible improvements
|
69
136
|
|
70
|
-
|
137
|
+
- Figure out why travis badge says error even though the build is passing ¬ ¬'
|
71
138
|
|
72
|
-
|
139
|
+
- Create health-check mechanism to test connection once circuit has half-opened
|
73
140
|
|
74
141
|
## Contributing
|
75
142
|
|
76
|
-
|
143
|
+
PRs are welcome :)
|
144
|
+
- PRs must be 100% test-covered
|
145
|
+
- PRs must please my code aesthetical preferences
|
77
146
|
|
78
|
-
|
147
|
+
## Development
|
79
148
|
|
80
|
-
|
149
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
81
150
|
|
82
|
-
|
151
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
152
|
+
|
153
|
+
## License
|
83
154
|
|
84
155
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
85
156
|
|
86
157
|
## Code of Conduct
|
87
158
|
|
88
|
-
Everyone interacting in the CircuitBreaker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
159
|
+
Everyone interacting in the CircuitBreaker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cesartalves/hiatus/blob/master/CODE_OF_CONDUCT.md).
|
@@ -27,5 +27,5 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency 'httparty'
|
28
28
|
spec.add_development_dependency 'pry'
|
29
29
|
spec.add_development_dependency 'timecop'
|
30
|
-
spec.add_development_dependency 'simplecov'
|
30
|
+
spec.add_development_dependency 'simplecov', '0.17.1'
|
31
31
|
end
|
data/lib/hiatus/mixin.rb
CHANGED
@@ -1,37 +1,51 @@
|
|
1
1
|
module Hiatus
|
2
|
+
|
2
3
|
module Mixin
|
3
4
|
|
5
|
+
class << self
|
6
|
+
attr_accessor :default_circuit_factory
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.config
|
10
|
+
yield self
|
11
|
+
end
|
12
|
+
|
4
13
|
def self.included(base)
|
5
14
|
base.extend ClassMethods
|
6
15
|
end
|
7
16
|
|
8
17
|
module ClassMethods
|
9
18
|
|
19
|
+
class NoCircuitFactoryProvided < StandardError; end
|
20
|
+
|
10
21
|
def circuit_factory(callable)
|
11
|
-
|
22
|
+
@circuit_factory = callable || Hiatus::Mixin.default_circuit_factory
|
12
23
|
end
|
13
24
|
|
14
|
-
def
|
15
|
-
unbounded_method_to_decorate = instance_method method
|
25
|
+
def _circuit_factory_; @circuit_factory; end
|
16
26
|
|
17
|
-
|
18
|
-
|
19
|
-
end
|
27
|
+
def circuit_break(method)
|
28
|
+
raise NoCircuitFactoryProvided unless @circuit_factory
|
20
29
|
|
21
|
-
|
30
|
+
unbounded_method_to_decorate = instance_method method
|
22
31
|
|
23
32
|
define_method method do |*args, &block|
|
24
|
-
@_circuit_breaker_ ||=
|
33
|
+
@_circuit_breaker_ ||= self.class._circuit_factory_[]
|
25
34
|
|
26
35
|
@_circuit_breaker_.run do
|
27
36
|
unbounded_method_to_decorate.bind(self)[*args, &block]
|
28
37
|
end
|
29
38
|
end
|
39
|
+
|
40
|
+
define_method :circuit_breaker do
|
41
|
+
@_circuit_breaker_
|
42
|
+
end
|
43
|
+
|
44
|
+
private :circuit_breaker
|
30
45
|
end
|
31
46
|
|
32
47
|
alias_method :circuit_protected, :circuit_break
|
33
48
|
|
34
49
|
end
|
35
|
-
|
36
50
|
end
|
37
51
|
end
|
data/lib/hiatus/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: circuit-hiatus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- cesartalvez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: simplecov
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 0.17.1
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '='
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 0.17.1
|
69
69
|
description: " Write a longer description or delete this line."
|
70
70
|
email:
|
71
71
|
- cesartalvez@gmail.com
|
@@ -84,7 +84,7 @@ files:
|
|
84
84
|
- Rakefile
|
85
85
|
- bin/console
|
86
86
|
- bin/setup
|
87
|
-
- hiatus.gemspec
|
87
|
+
- circuit-hiatus.gemspec
|
88
88
|
- lib/hiatus.rb
|
89
89
|
- lib/hiatus/circuit_breaker.rb
|
90
90
|
- lib/hiatus/circuits/count_circuit_breaker.rb
|
@@ -114,7 +114,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
114
|
- !ruby/object:Gem::Version
|
115
115
|
version: '0'
|
116
116
|
requirements: []
|
117
|
-
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 2.7.7
|
118
119
|
signing_key:
|
119
120
|
specification_version: 4
|
120
121
|
summary: Write a short summary, because RubyGems requires one.
|