circuit-hiatus 0.4.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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +26 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hiatus.gemspec +31 -0
- data/lib/hiatus/circuit_breaker.rb +84 -0
- data/lib/hiatus/circuits/count_circuit_breaker.rb +10 -0
- data/lib/hiatus/circuits/percentage_circuit_breaker.rb +10 -0
- data/lib/hiatus/count_threshold.rb +23 -0
- data/lib/hiatus/extras/file_count_threshold.rb +35 -0
- data/lib/hiatus/mixin.rb +37 -0
- data/lib/hiatus/percentage_threshold.rb +30 -0
- data/lib/hiatus/version.rb +3 -0
- data/lib/hiatus.rb +11 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0ae4ee84b9cb2ac232e1413ba93e6bc2cbdf94b3cb4663adf08201e1f7fd56be
|
4
|
+
data.tar.gz: a5ddb3f5c6d8c3b5468c02a49733fbd585cd7f240fdbd4b4e5baba376e9750fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9411ffa270d0f7dab195cbac02cfbadf4bc9d50056701ae9e31a75884879d444a3e1dbab0393a271c6253ead4847111cc87038244b74e59495180c56b3b50b34
|
7
|
+
data.tar.gz: 1d3822b0977b2b987f232cb68b1c0e1af6433bc7d73818003273d70e0a5fa130fa6d0a10f7bc7230f518753b41022529a4335af93e4e6fa199941752d104ee2a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
rvm:
|
4
|
+
- 2.7.1
|
5
|
+
before_install: gem install bundler -v 2.1.4
|
6
|
+
after_script:
|
7
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
8
|
+
before_script:
|
9
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
|
10
|
+
> ./cc-test-reporter
|
11
|
+
- chmod +x ./cc-test-reporter
|
12
|
+
- "./cc-test-reporter before-build"
|
13
|
+
script:
|
14
|
+
- bundle install
|
15
|
+
- bundle exec rspec
|
16
|
+
deploy:
|
17
|
+
provider: rubygems
|
18
|
+
api_key:
|
19
|
+
secure: ewdh0Teo5jGrIOmi8oMzHB4pUNaoHwS5tcJTIHzTlSXEqDkzRmf9QlVkRwQ3Fk6LWNw6zmipTPTQUvths6z0/qArg8Ga1aqf8NWSWNc7W+SiHKNl8bdKkcP623QSwjNq1icH8dK29/I4y+sociit+IrS+CbRMticiteLufHsJG+FrWtUOTdxBLseJF74kP7LlGBmPa7skCdxSxToJ0scs0CnP7fgeZj3J0gJ4XFtg2xrw70EWsWOQYM2Gp5i/2n8tvR/EgALu+UcVibQlduYt/wffEhkb7K3HYOrBJ0Zzzy8VUIwW/xNrGDfILkM96nGhVgjC/Z/xT+c7zmPDa0Kq9itRtEbt+BXfrF0AHevFdOoPIqSKc5BEnt5/jMnqf3hdkYAiwneq3AMkrsbybC9SNDXNTNyyWtiiyYyls4f5MHz4aGdjWVd+SMKRFhZxZwTNgRyPD8xi1q7Fdz4Gloz6Ih4IWYEe0FYgxD76SV8xZtK3j1SddVpacsGFCDKv4Xq28SKYyu0t9IL8ROEFqYR9QjXNazNwXwpqjTYLSKomKt9974Z6tJ/foUrC8b8IPUl+rjo0iFr90frFJD89vMnkuKoAN3vPfZAL/V68VsCf7rxZ72UMN5enkfk37AvsJW1a7Xfds7jNGwYIjsbkk1NimTZuQc4Ra5SFCZE/qjvj1M=
|
20
|
+
gem: circuit-hiatus
|
21
|
+
on:
|
22
|
+
tags: true
|
23
|
+
repo: cesartalves/hiatus
|
24
|
+
env:
|
25
|
+
global:
|
26
|
+
secure: ICtEJMZcF9xb/jql10P9Uo0/JFgXl81NMOOP/fjAso3Oeo/EHxYpZHTRe6nc6QGREFATO+2l7cceObroANZL3WaGOPaSosRfB/sU3UWImXa47j22b/aRIWAx94tiD7InbGDYwk7KoUTZyfRXyV3rxTp/QrL84kivJmvrkB7UUzGGyAiqiJqsseIgXHxFufBoKIdE3ArAviQIrUkqBk4TtqZMCiDbAqcdk/4FNa2goSP2VsI3CzNpi4/hQ3Y9xvYJojbukTLeSbDKhMDSmjkWwpTn/2wvwjkdYZz5k8aGSY0+yR5L1wrRXEvjJHQGgfRwiPLQ6qbKzJsSMDysNUss2OoUocycIy/ElPseCohUroXh9ZxZZHtfOnw5rvKsGcSgKvIoiFPQ/Qh52gUXNPm40axIpsTR+ckRjpvtNA165ghLNoHvu1F8cXc+HjwmLtOdz46TZSAgEejpYKguWjNgq2b44SaOFlba7Mpq7EEu9DfdPu3F5wj7spugXIoFOL0r/JE1EeBM0ovSE0il4Ox3DfEPIIzvnxC8k3kB4CG5H9LVjjyfiXGcNvBr1aqaU5Ofmd01Cdo3EGoHN5iV8x6W8UCSJlI8RKeTcLu47FUfeGwEdwdZJ7U7K4YM7kxeV4cpSyAhDxF3OnSWH17cTxM809MW5zAuLsn+CbQkm+2L1Q4=
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at cesartalvez@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
circuit-hiatus (0.4.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.3)
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
docile (1.3.2)
|
12
|
+
httparty (0.18.1)
|
13
|
+
mime-types (~> 3.0)
|
14
|
+
multi_xml (>= 0.5.2)
|
15
|
+
method_source (1.0.0)
|
16
|
+
mime-types (3.3.1)
|
17
|
+
mime-types-data (~> 3.2015)
|
18
|
+
mime-types-data (3.2020.0512)
|
19
|
+
multi_xml (0.6.0)
|
20
|
+
pry (0.13.1)
|
21
|
+
coderay (~> 1.1)
|
22
|
+
method_source (~> 1.0)
|
23
|
+
rake (12.3.3)
|
24
|
+
rspec (3.9.0)
|
25
|
+
rspec-core (~> 3.9.0)
|
26
|
+
rspec-expectations (~> 3.9.0)
|
27
|
+
rspec-mocks (~> 3.9.0)
|
28
|
+
rspec-core (3.9.2)
|
29
|
+
rspec-support (~> 3.9.3)
|
30
|
+
rspec-expectations (3.9.2)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.9.0)
|
33
|
+
rspec-mocks (3.9.1)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.9.0)
|
36
|
+
rspec-support (3.9.3)
|
37
|
+
simplecov (0.18.5)
|
38
|
+
docile (~> 1.1)
|
39
|
+
simplecov-html (~> 0.11)
|
40
|
+
simplecov-html (0.12.2)
|
41
|
+
timecop (0.9.1)
|
42
|
+
|
43
|
+
PLATFORMS
|
44
|
+
ruby
|
45
|
+
|
46
|
+
DEPENDENCIES
|
47
|
+
circuit-hiatus!
|
48
|
+
httparty
|
49
|
+
pry
|
50
|
+
rake (~> 12.0)
|
51
|
+
rspec (~> 3.0)
|
52
|
+
simplecov
|
53
|
+
timecop
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 cesartalvez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Hiatus
|
2
|
+
|
3
|
+
* *noun:*
|
4
|
+
+ *a pause or gap in a sequence, series, or process.*
|
5
|
+
+ *A temporary gap, pause, break*
|
6
|
+
|
7
|
+
Dead simple circuit breaker pattern implementation.
|
8
|
+
|
9
|
+
Currently this is an unambitious project for future reference and talks. Thought the pattern was interesting and wanted to give it a shot :)
|
10
|
+
|
11
|
+
--
|
12
|
+
|
13
|
+
## Introduction
|
14
|
+
|
15
|
+
This pattern helps applications which deal with calls to remote services. When they're unavailable, it prevents repeated calls which may cause cascading failure.
|
16
|
+
|
17
|
+
Here is a great article about it: https://martinfowler.com/bliki/CircuitBreaker.html
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add `gem 'hiatus', git: https://github.com/cesartalves/hiatus` to your Gemfile
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Once threshold is reached, will raise a `Hiatus:CircuitBrokenError` till `half_open_interval` passes. Then, it will half-open: a failed call trips the circuit again, a successful call closes it.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
threshold = Hiatus::CountThreshold.new 5 # will be broken on the 5th failed attempt
|
29
|
+
|
30
|
+
circuit_breaker = Hiatus::CircuitBreaker.new threshold: threshold, half_open_interval: 60 # after 60 seconds, circuit is apt to make calls again
|
31
|
+
|
32
|
+
5.times do
|
33
|
+
circuit_breaker.run do
|
34
|
+
begin
|
35
|
+
# Http call raising error
|
36
|
+
rescue => e
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
circuit_breaker.run do
|
42
|
+
# any call
|
43
|
+
end # => Hiatus:CircuitBrokenError
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
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
|
+
|
49
|
+
```ruby
|
50
|
+
class Service
|
51
|
+
|
52
|
+
include Hiatus::Mixin
|
53
|
+
|
54
|
+
# this gives you great flexiblity on which circuit to use
|
55
|
+
# you can even have your classes sharing the same circuit breaker :)
|
56
|
+
circuit_factory -> { Hiatus::Circuits::CountCircuitBreaker.new threshold: 3 }
|
57
|
+
|
58
|
+
circuit_protected def call
|
59
|
+
raise 'Error'
|
60
|
+
end
|
61
|
+
|
62
|
+
# Check `spec/hiatus_mixin_spec.rb` for the full example
|
63
|
+
|
64
|
+
```
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
TODO: fill
|
69
|
+
|
70
|
+
After checking out the repo, run `bin/setup` 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.
|
71
|
+
|
72
|
+
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).
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
TODO: fill
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/circuit_breaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/circuit_breaker/blob/master/CODE_OF_CONDUCT.md).
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
TODO: fill
|
83
|
+
|
84
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
85
|
+
|
86
|
+
## Code of Conduct
|
87
|
+
|
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/[USERNAME]/circuit_breaker/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hiatus"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/hiatus.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/hiatus/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "circuit-hiatus"
|
5
|
+
spec.version = Hiatus::VERSION
|
6
|
+
spec.authors = ["cesartalvez"]
|
7
|
+
spec.email = ["cesartalvez@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{ Write a short summary, because RubyGems requires one.}
|
10
|
+
spec.description = %q{ Write a longer description or delete this line.}
|
11
|
+
spec.homepage = "https://github.com/cesartalves/hiatus"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
# spec.metadata["source_code_uri"] = " Put your gem's public repo URL here."
|
17
|
+
# spec.metadata["changelog_uri"] = " Put your gem's CHANGELOG.md URL here."
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
spec.add_development_dependency 'httparty'
|
28
|
+
spec.add_development_dependency 'pry'
|
29
|
+
spec.add_development_dependency 'timecop'
|
30
|
+
spec.add_development_dependency 'simplecov'
|
31
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Hiatus
|
2
|
+
class CircuitBrokenError < StandardError; end
|
3
|
+
|
4
|
+
class CircuitBreaker
|
5
|
+
|
6
|
+
DEFAUTS = {
|
7
|
+
threshold: 5,
|
8
|
+
half_open_interval: 5
|
9
|
+
}
|
10
|
+
|
11
|
+
DEFAULT_THRESHOLD = CountThreshold.new DEFAUTS[:threshold]
|
12
|
+
|
13
|
+
attr_reader :state
|
14
|
+
|
15
|
+
def initialize(threshold: nil, half_open_interval: nil)
|
16
|
+
@threshold = threshold || DEFAULT_THRESHOLD
|
17
|
+
@half_open_interval = half_open_interval || DEFAUTS[:half_open_interval]
|
18
|
+
|
19
|
+
@state = :closed
|
20
|
+
end
|
21
|
+
|
22
|
+
def run &block
|
23
|
+
|
24
|
+
raise CircuitBrokenError if open? && !reached_retrial_threshold?
|
25
|
+
|
26
|
+
begin
|
27
|
+
call_with_circuit_state_changes &block
|
28
|
+
rescue => e
|
29
|
+
increment_failure_count_and_trip_if_threshold_exceeded
|
30
|
+
raise $!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def call_with_circuit_state_changes
|
35
|
+
@threshold.touch
|
36
|
+
yield if block_given?
|
37
|
+
@threshold.reset if half_open?
|
38
|
+
close
|
39
|
+
end
|
40
|
+
|
41
|
+
def increment_failure_count_and_trip_if_threshold_exceeded
|
42
|
+
increment_failure_count
|
43
|
+
trip_if_threshold_exceeded
|
44
|
+
end
|
45
|
+
|
46
|
+
def increment_failure_count; @threshold.increment; end
|
47
|
+
|
48
|
+
def trip_if_threshold_exceeded
|
49
|
+
@state = :open if @threshold.reached?
|
50
|
+
|
51
|
+
refresh_last_failure_timestamp
|
52
|
+
end
|
53
|
+
|
54
|
+
def open?
|
55
|
+
state == :open
|
56
|
+
end
|
57
|
+
|
58
|
+
def closed?
|
59
|
+
state == :closed
|
60
|
+
end
|
61
|
+
|
62
|
+
def close
|
63
|
+
@state = :closed
|
64
|
+
last_failure_timestamp = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
def half_open?
|
68
|
+
open? && reached_retrial_threshold?
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
def refresh_last_failure_timestamp
|
74
|
+
@last_failure_timestamp = Time.now
|
75
|
+
end
|
76
|
+
|
77
|
+
def reached_retrial_threshold?
|
78
|
+
last_failure_timestamp && Time.now - last_failure_timestamp > @half_open_interval
|
79
|
+
end
|
80
|
+
|
81
|
+
attr_accessor :last_failure_timestamp
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Hiatus
|
2
|
+
module Circuits
|
3
|
+
class CountCircuitBreaker < Hiatus::CircuitBreaker
|
4
|
+
def initialize(threshold: nil, half_open_interval: nil)
|
5
|
+
threshold = Hiatus::CountThreshold.new threshold
|
6
|
+
super threshold: threshold, half_open_interval: half_open_interval
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Hiatus
|
2
|
+
module Circuits
|
3
|
+
class PercentageCircuitBreaker < Hiatus::CircuitBreaker
|
4
|
+
def initialize(threshold: nil, half_open_interval: nil)
|
5
|
+
threshold = Hiatus::PercentageThreshold.new threshold
|
6
|
+
super threshold: threshold, half_open_interval: half_open_interval
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Hiatus
|
2
|
+
class CountThreshold
|
3
|
+
|
4
|
+
def initialize(threshold = 5)
|
5
|
+
@threshold = threshold
|
6
|
+
@failure_count = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def increment
|
10
|
+
@failure_count += 1
|
11
|
+
end
|
12
|
+
|
13
|
+
def reached?
|
14
|
+
@failure_count >= @threshold
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset
|
18
|
+
@failure_count = 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def touch; end;
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Hiatus
|
2
|
+
# an example of threshold that could be stored somewhere
|
3
|
+
# file's really simple to implement.
|
4
|
+
class FileCountThreshold < CountThreshold
|
5
|
+
def initialize(file_path, threshold = 5)
|
6
|
+
@file_path = file_path
|
7
|
+
super threshold
|
8
|
+
end
|
9
|
+
|
10
|
+
def increment
|
11
|
+
super
|
12
|
+
serialize
|
13
|
+
end
|
14
|
+
|
15
|
+
def reached?
|
16
|
+
failure_count, threshold = *deserialize
|
17
|
+
failure_count >= threshold
|
18
|
+
end
|
19
|
+
|
20
|
+
def reset
|
21
|
+
super
|
22
|
+
serialize
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def deserialize
|
28
|
+
JSON.parse IO.binread(@file_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
def serialize
|
32
|
+
IO.binwrite @file_path, JSON.dump([@failure_count, @threshold])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/hiatus/mixin.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Hiatus
|
2
|
+
module Mixin
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def circuit_factory(callable)
|
11
|
+
@@circuit_factory = callable
|
12
|
+
end
|
13
|
+
|
14
|
+
def circuit_break(method)
|
15
|
+
unbounded_method_to_decorate = instance_method method
|
16
|
+
|
17
|
+
define_method :circuit_breaker do
|
18
|
+
@_circuit_breaker_
|
19
|
+
end
|
20
|
+
|
21
|
+
private :circuit_breaker
|
22
|
+
|
23
|
+
define_method method do |*args, &block|
|
24
|
+
@_circuit_breaker_ ||= @@circuit_factory[]
|
25
|
+
|
26
|
+
@_circuit_breaker_.run do
|
27
|
+
unbounded_method_to_decorate.bind(self)[*args, &block]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
alias_method :circuit_protected, :circuit_break
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Hiatus
|
2
|
+
class PercentageThreshold
|
3
|
+
|
4
|
+
DEFAULT_PERCENTAGE = 0.5
|
5
|
+
|
6
|
+
def initialize(failure_percentage = DEFAULT_PERCENTAGE)
|
7
|
+
@failure_percentage = failure_percentage
|
8
|
+
@failure_count = 0.0
|
9
|
+
@total_calls = 0.0
|
10
|
+
end
|
11
|
+
|
12
|
+
def increment
|
13
|
+
@failure_count += 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def touch
|
17
|
+
@total_calls += 1
|
18
|
+
end
|
19
|
+
|
20
|
+
def reset
|
21
|
+
@failure_count = 0
|
22
|
+
@total_calls = 0
|
23
|
+
end
|
24
|
+
|
25
|
+
def reached?
|
26
|
+
@failure_count / @total_calls > @failure_percentage
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/hiatus.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "hiatus/version"
|
2
|
+
|
3
|
+
require 'hiatus/count_threshold'
|
4
|
+
require 'hiatus/percentage_threshold'
|
5
|
+
|
6
|
+
require 'hiatus/circuit_breaker'
|
7
|
+
|
8
|
+
require 'hiatus/circuits/count_circuit_breaker'
|
9
|
+
require 'hiatus/circuits/percentage_circuit_breaker'
|
10
|
+
|
11
|
+
require 'hiatus/mixin'
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: circuit-hiatus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cesartalvez
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: timecop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: " Write a longer description or delete this line."
|
70
|
+
email:
|
71
|
+
- cesartalvez@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- hiatus.gemspec
|
88
|
+
- lib/hiatus.rb
|
89
|
+
- lib/hiatus/circuit_breaker.rb
|
90
|
+
- lib/hiatus/circuits/count_circuit_breaker.rb
|
91
|
+
- lib/hiatus/circuits/percentage_circuit_breaker.rb
|
92
|
+
- lib/hiatus/count_threshold.rb
|
93
|
+
- lib/hiatus/extras/file_count_threshold.rb
|
94
|
+
- lib/hiatus/mixin.rb
|
95
|
+
- lib/hiatus/percentage_threshold.rb
|
96
|
+
- lib/hiatus/version.rb
|
97
|
+
homepage: https://github.com/cesartalves/hiatus
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata:
|
101
|
+
homepage_uri: https://github.com/cesartalves/hiatus
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 2.3.0
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubygems_version: 3.1.2
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Write a short summary, because RubyGems requires one.
|
121
|
+
test_files: []
|