switch_gear 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/examples/example.rb +27 -0
- data/examples/example_redis.rb +32 -0
- data/lib/switch_gear/circuit_breaker/failure.rb +37 -0
- data/lib/switch_gear/circuit_breaker/memory.rb +55 -0
- data/lib/switch_gear/circuit_breaker/open_error.rb +6 -0
- data/lib/switch_gear/circuit_breaker/redis.rb +113 -0
- data/lib/switch_gear/circuit_breaker.rb +118 -0
- data/lib/switch_gear/version.rb +3 -0
- data/lib/switch_gear.rb +3 -0
- data/remote-loop.gif +0 -0
- data/remote.gif +0 -0
- data/switch_gear.gemspec +34 -0
- metadata +180 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6790aad189ffd50ac181d5e5e75fb5bf97e34d4c
|
4
|
+
data.tar.gz: 9486ddff0840167fa1e62ad3f9c63a4ff8d0d90e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 124441bce6d533ef404e3cdae2f6278182be8586bf069373e3c3ac377ede0a705fe7b408850b21245ac0338c00e4e40f6d42209eed795ad7c397971b95feb095
|
7
|
+
data.tar.gz: 75807a66c3c9b49e8e38cf083b24400a14d60b432bab458407575d0cb714a82473a028dd3ff2d9042aa178d649cad4796767abcd69f824ad80caaa2530406602
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
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 anthony.ross@validic.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 [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Anthony Ross
|
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,131 @@
|
|
1
|
+
# SwitchGear
|
2
|
+
|
3
|
+
>In an electric power system, switchgear is the combination of electrical disconnect switches, fuses or circuit breakers used to control, protect and isolate electrical equipment. Switchgears are used both to de-energize equipment to allow work to be done and to clear faults downstream. This type of equipment is directly linked to the reliability of the electricity supply.
|
4
|
+
|
5
|
+
SwitchGear is a module that will implement various failover protection layers for deploying apps at scale. The first module is a lightweight implementation of the famous [Michael Nygard](https://www.martinfowler.com/bliki/CircuitBreaker.html) circuit breaker pattern.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
This gem is in alpha and is on RubyGems.org. I'm still finalizing the API, but if you wish to help me get to it's first stable release, please do!
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'switch_gear'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### CircuitBreaker
|
24
|
+
|
25
|
+
#### In Memory
|
26
|
+
|
27
|
+
Here's an example of how you could use the breaker while making routine calls to a third party service such as Twitter:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'switch_gear/circuit_breaker'
|
31
|
+
require 'logger'
|
32
|
+
|
33
|
+
@logger = Logger.new(STDOUT)
|
34
|
+
|
35
|
+
handles = ["joe", "jane", "mary", "steve"]
|
36
|
+
|
37
|
+
def get_tweets(twitter_handle)
|
38
|
+
http_result = ["Success!", "Fail"].sample
|
39
|
+
raise RuntimeError.new("Failed to fetch tweets for #{twitter_handle}") if http_result == "Fail"
|
40
|
+
@logger.info "#{http_result} getting tweets for #{twitter_handle}"
|
41
|
+
end
|
42
|
+
|
43
|
+
breaker = SwitchGear::CircuitBreaker::Memory.new do |cb|
|
44
|
+
cb.circuit = -> (twitter_handle) { get_tweets(twitter_handle) }
|
45
|
+
cb.failure_limit = 2
|
46
|
+
cb.reset_timeout = 5
|
47
|
+
end
|
48
|
+
|
49
|
+
handles.each do |handle|
|
50
|
+
begin
|
51
|
+
breaker.call(handle)
|
52
|
+
rescue SwitchGear::CircuitBreaker::OpenError
|
53
|
+
@logger.warn "Circuit is open - unable to make calls for #{handle}"
|
54
|
+
sleep breaker.reset_timeout
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
You will see output similar to:
|
60
|
+
```
|
61
|
+
W, [2017-02-12T20:49:12.374971 #85900] WARN -- : [RuntimeError] - Failed to fetch tweets for joe
|
62
|
+
W, [2017-02-12T20:49:12.375049 #85900] WARN -- : [RuntimeError] - Failed to fetch tweets for jane
|
63
|
+
I, [2017-02-12T20:49:17.380771 #85900] INFO -- : Success! getting tweets for steve
|
64
|
+
I, [2017-02-12T20:49:17.380865 #85900] INFO -- : Circuit closed
|
65
|
+
```
|
66
|
+
|
67
|
+
Notice that we had two failures in a row for joe and jane. The circuit breaker was configured to only allow for 2 failures via the `failuire_limit` method. If another call comes in after two failures, it will raise a `SwitchGear::CircuitBreaker::OpenError` error. The only way the circuit breaker will be closed again is if the `reset_timeout` period has lapsed. In our loop we catch the `SwitchGear::CircuitBreaker::OpenError` exception and sleep (don't sleep in production - this is just an example) to allow the Circuit to close. You can see the timestamp of this log,
|
68
|
+
|
69
|
+
```
|
70
|
+
I, [2017-02-12T20:49:17.380771 #85900] INFO -- : Success! getting tweets for steve
|
71
|
+
```
|
72
|
+
is 5+ seconds after the last error which exceeds the `reset_timeout` - that's why the breaker allowed the method invocation to go get steve's tweets.
|
73
|
+
|
74
|
+
|
75
|
+
#### Redis
|
76
|
+
|
77
|
+
In an distributed environment the in memory solution of the circuit breaker creates quite a bit of unnecessary work. If you can imagine 5 servers all running their own circuit breakers, the `failure_limit` has just increased by a factor of 5. Ideally, we want server1's failures and server2's failures to be included for similar breakers. We do this by using redis where the state of the breaker and the failures are persisted. Redis is a great choice for this especially since most distributed systems have a redis instance in use.
|
78
|
+
|
79
|
+
You can visualize a few servers that were originally in a closed state moving to open upon failures as such:
|
80
|
+
|
81
|
+
![img](https://s3.postimg.org/stxckap03/ezgif_com_video_to_gif.gif)
|
82
|
+
|
83
|
+
You can set up the `CircuitBreaker` to use the redis adapter like this:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
breaker = SwitchGear::CircuitBreaker::Redis.new do |cb|
|
87
|
+
cb.circuit = -> (twitter_handle) { get_tweets(twitter_handle) }
|
88
|
+
cb.client = redis
|
89
|
+
cb.namespace = "get_tweets"
|
90
|
+
cb.failure_limit = 2
|
91
|
+
cb.reset_timeout = 5
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
You need 2 additional parameters(compared to the `Memory` adapter), they are defined as such:
|
96
|
+
|
97
|
+
- `client` - an instance of a `Redis` client. This gem does not have a hard dependency on a particular redis client but for testing I've used [redis-rb](https://github.com/redis/redis-rb). Whatever you pass in here simply has to implement a few redis commands such as `sadd`, `del`, `smembers`, `get` and `set`. The client will ensure these exist before the breaker can be instantiated.
|
98
|
+
- `namespace` - A unique name that will be used across servers to sync `state` and `failures`. I'd recommend `class_name:some_method` or whatever is special about what's being invoked in the `circuit`.
|
99
|
+
|
100
|
+
#### Roll Your Own Circuit Breaker
|
101
|
+
|
102
|
+
The goal of this project is to help you implement a circuit breaker pattern and be agnostic to the persistence layer. I did it in memory and in redis both as working implementations to make the gem usable out of the box. There are other in memory data stores that would work really well with this and so you can easily implement your own.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
class MyPreferredAdapter
|
106
|
+
include SwitchGear::CircuitBreaker
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
## Forthcoming
|
111
|
+
|
112
|
+
1. A middleware in Sidekiq using this gem
|
113
|
+
2. Better in memory support for async tasks
|
114
|
+
3. More examples
|
115
|
+
4. More documentation
|
116
|
+
|
117
|
+
|
118
|
+
## Development
|
119
|
+
|
120
|
+
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.
|
121
|
+
|
122
|
+
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).
|
123
|
+
|
124
|
+
## Contributing
|
125
|
+
|
126
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/allcentury/circuit_breaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
127
|
+
|
128
|
+
|
129
|
+
## License
|
130
|
+
|
131
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/examples/example.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'switch_gear/circuit_breaker'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
@logger = Logger.new(STDOUT)
|
5
|
+
|
6
|
+
handles = ["joe", "jane", "mary", "steve"]
|
7
|
+
|
8
|
+
def get_tweets(twitter_handle)
|
9
|
+
http_result = ["Success!", "Fail"].sample
|
10
|
+
raise RuntimeError.new("Failed to fetch tweets for #{twitter_handle}") if http_result == "Fail"
|
11
|
+
@logger.info "#{http_result} getting tweets for #{twitter_handle}"
|
12
|
+
end
|
13
|
+
|
14
|
+
breaker = SwitchGear::CircuitBreaker::Memory.new do |cb|
|
15
|
+
cb.circuit = -> (twitter_handle) { get_tweets(twitter_handle) }
|
16
|
+
cb.failure_limit = 2
|
17
|
+
cb.reset_timeout = 5
|
18
|
+
end
|
19
|
+
|
20
|
+
handles.each do |handle|
|
21
|
+
begin
|
22
|
+
breaker.call(handle)
|
23
|
+
rescue SwitchGear::CircuitBreaker::OpenError
|
24
|
+
@logger.warn "Circuit is open - unable to make calls for #{handle}"
|
25
|
+
sleep breaker.reset_timeout
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'switch_gear/circuit_breaker'
|
2
|
+
require 'logger'
|
3
|
+
require 'redis'
|
4
|
+
|
5
|
+
@logger = Logger.new(STDOUT)
|
6
|
+
|
7
|
+
handles = ["joe", "jane", "mary", "steve"]
|
8
|
+
|
9
|
+
def get_tweets(twitter_handle)
|
10
|
+
http_result = ["Success!", "Fail"].sample
|
11
|
+
raise RuntimeError.new("Failed to fetch tweets for #{twitter_handle}") if http_result == "Fail"
|
12
|
+
@logger.info "#{http_result} getting tweets for #{twitter_handle}"
|
13
|
+
end
|
14
|
+
|
15
|
+
redis = Redis.new
|
16
|
+
|
17
|
+
breaker = SwitchGear::CircuitBreaker::Redis.new do |cb|
|
18
|
+
cb.circuit = -> (twitter_handle) { get_tweets(twitter_handle) }
|
19
|
+
cb.client = redis
|
20
|
+
cb.namespace = "get_tweets"
|
21
|
+
cb.failure_limit = 2
|
22
|
+
cb.reset_timeout = 5
|
23
|
+
end
|
24
|
+
|
25
|
+
handles.each do |handle|
|
26
|
+
begin
|
27
|
+
breaker.call(handle)
|
28
|
+
rescue SwitchGear::CircuitBreaker::OpenError
|
29
|
+
@logger.warn "Circuit is open - unable to make calls for #{handle}"
|
30
|
+
sleep breaker.reset_timeout
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'json'
|
2
|
+
module SwitchGear
|
3
|
+
module CircuitBreaker
|
4
|
+
class Failure
|
5
|
+
def self.from_json(json)
|
6
|
+
failure = JSON.parse(json)
|
7
|
+
error = Object.const_get(failure["error"])
|
8
|
+
error = error.new(failure["message"])
|
9
|
+
new(error, Time.parse(failure["timestamp"]))
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(error, recorded = Time.now.utc)
|
13
|
+
@error = error
|
14
|
+
@recorded = recorded
|
15
|
+
end
|
16
|
+
|
17
|
+
def timestamp
|
18
|
+
recorded
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
"[#{error.class}] - #{error.message}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_json
|
26
|
+
JSON.generate({
|
27
|
+
error: error.class,
|
28
|
+
message: error.message,
|
29
|
+
timestamp: timestamp.to_s
|
30
|
+
})
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
attr_reader :error, :recorded
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module SwitchGear
|
2
|
+
module CircuitBreaker
|
3
|
+
class Memory
|
4
|
+
include CircuitBreaker
|
5
|
+
|
6
|
+
# The main runner, must respond to #call
|
7
|
+
# @return [Proc/Lambda] the runner
|
8
|
+
attr_accessor :circuit
|
9
|
+
# The count of failures
|
10
|
+
# @return [Integer] the amount of failures to permit. Defaults to 10 seconds
|
11
|
+
attr_accessor :failure_limit
|
12
|
+
# The amount of time in seconds before a breaker should reset if currently open. Defaults to 5
|
13
|
+
# @return [Integer]
|
14
|
+
attr_accessor :reset_timeout
|
15
|
+
# The current logger
|
16
|
+
# @return [Object] - The logger sent in at initialization. Defaults to ruby's std Logger class
|
17
|
+
attr_accessor :logger
|
18
|
+
# The current state
|
19
|
+
# @return [Symbol] should always return either :open, :closed or :half-open.
|
20
|
+
# Use the helper methods in lib/circuit_breaker.rb for more readable code.
|
21
|
+
attr_accessor :state
|
22
|
+
# The current failures
|
23
|
+
# @return [Array<CircuitBreaker::Failure>] - a list of failures serialized.
|
24
|
+
attr_accessor :failures
|
25
|
+
#
|
26
|
+
# @example create a new breaker
|
27
|
+
# breaker = SwitchGear::CircuitBreaker::Memory.new do |cb|
|
28
|
+
# cb.circuit = -> (arg) { my_method(arg) }
|
29
|
+
# cb.failure_limit = 2
|
30
|
+
# cb.reset_timeout = 5
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# @yieldparam circuit - (look to {#circuit})
|
34
|
+
# @yieldparam failure_limit - (look to {#failure_limit})
|
35
|
+
# @yieldparam reset_timeout - (look to {#reset_timeout})
|
36
|
+
# @yieldparam logger - (look to {#logger})
|
37
|
+
# @return [SwitchGear::CircuitBreaker::Memory] the object.
|
38
|
+
def initialize(&block)
|
39
|
+
yield self
|
40
|
+
@failure_limit ||= 5
|
41
|
+
@reset_timeout ||= 10
|
42
|
+
@logger ||= Logger.new(STDOUT)
|
43
|
+
@state = :closed
|
44
|
+
@failures = []
|
45
|
+
run_validations
|
46
|
+
end
|
47
|
+
|
48
|
+
# (look to {SwitchGear::CircuitBreaker::add_failure})
|
49
|
+
def add_failure(failure)
|
50
|
+
failures << failure
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module SwitchGear
|
2
|
+
module CircuitBreaker
|
3
|
+
class Redis
|
4
|
+
include CircuitBreaker
|
5
|
+
# (look to {SwitchGear::CircuitBreaker::Memory#circuit})
|
6
|
+
attr_accessor :circuit
|
7
|
+
# (look to {SwitchGear::CircuitBreaker::Memory#failure_limit})
|
8
|
+
attr_accessor :failure_limit
|
9
|
+
# (look to {SwitchGear::CircuitBreaker::Memory#reset_timeout})
|
10
|
+
attr_accessor :reset_timeout
|
11
|
+
# (look to {SwitchGear::CircuitBreaker::Memory#logger})
|
12
|
+
attr_accessor :logger
|
13
|
+
# (look to {SwitchGear::CircuitBreaker::Memory#state})
|
14
|
+
attr_accessor :state
|
15
|
+
# (look to {SwitchGear::CircuitBreaker::Memory#failures})
|
16
|
+
attr_accessor :failures
|
17
|
+
# A unique name that will be used across servers to
|
18
|
+
# sync state and failures. I'd recommend `your_class.name:your_method_name` or whatever
|
19
|
+
# is special about what's being invoked in the `circuit`. See examples/example_redis.rb
|
20
|
+
# @return [String] - namespace given
|
21
|
+
attr_accessor :namespace
|
22
|
+
|
23
|
+
# An instance of an redis client. This library does not have a
|
24
|
+
# hard dependency on a particular redis client but for testing I've used
|
25
|
+
# [redis-rb](https://github.com/redis/redis-rb). Whatever you pass in here simply has to
|
26
|
+
# implement a few redis commands such as `sadd`, `del`, `smembers`, `get` and `set`.
|
27
|
+
# The client will ensure these exist before the breaker can be instantiated.
|
28
|
+
# @return [Object] - redis client given
|
29
|
+
attr_accessor :client
|
30
|
+
|
31
|
+
# The main class to instantiate the CircuitBraker class.
|
32
|
+
#
|
33
|
+
# @example create a new breaker
|
34
|
+
# breaker = SwitchGear::CircuitBreaker::Redis.new do |cb|
|
35
|
+
# cb.circuit = -> (arg) { my_method(arg) }
|
36
|
+
# cb.failure_limit = 2
|
37
|
+
# cb.reset_timeout = 5
|
38
|
+
# cb.client = redis_client
|
39
|
+
# cb.namespace = "some_key"
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# @yieldparam circuit - (look to {#circuit})
|
43
|
+
# @yieldparam failure_limit - (look to {#failure_limit})
|
44
|
+
# @yieldparam reset_timeout - (look to {#reset_timeout})
|
45
|
+
# @yieldparam logger - (look to {#logger})
|
46
|
+
# @yieldparam client - (look to {#client})
|
47
|
+
# @yieldparam namespace - (look to {#namespace})
|
48
|
+
# @return [SwitchGear::CircuitBreaker::Redis] the object.
|
49
|
+
def initialize
|
50
|
+
yield self
|
51
|
+
@client = client
|
52
|
+
@namespace = namespace
|
53
|
+
@failure_limit ||= 5
|
54
|
+
@reset_timeout ||= 10
|
55
|
+
@logger = Logger.new(STDOUT)
|
56
|
+
run_validations
|
57
|
+
@namespace = "circuit_breaker:#{namespace}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def state
|
61
|
+
redis_state = client.get(state_namespace)
|
62
|
+
return redis_state.to_sym if redis_state
|
63
|
+
# if there is no state stored in redis, set it.
|
64
|
+
self.state = :closed
|
65
|
+
end
|
66
|
+
|
67
|
+
def state=(state)
|
68
|
+
client.set(state_namespace, state.to_s)
|
69
|
+
end
|
70
|
+
|
71
|
+
def failures
|
72
|
+
redis_fails = client.smembers(fail_namespace)
|
73
|
+
return redis_fails.map { |f| Failure.from_json(f) } if redis_fails
|
74
|
+
# if there are no failures in redis, set it to empty
|
75
|
+
self.failures = []
|
76
|
+
[]
|
77
|
+
end
|
78
|
+
|
79
|
+
def add_failure(failure)
|
80
|
+
client.sadd(fail_namespace, failure.to_json)
|
81
|
+
end
|
82
|
+
|
83
|
+
# failures= requires we replace what is currently in redis
|
84
|
+
# with the new value so we delete all entries first then add
|
85
|
+
def failures=(failures)
|
86
|
+
client.del(fail_namespace)
|
87
|
+
failures.each { |f| client.sadd(fail_namespace, f.to_json) }
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def fail_namespace
|
93
|
+
"#{namespace}:failures"
|
94
|
+
end
|
95
|
+
|
96
|
+
def state_namespace
|
97
|
+
"#{namespace}:state"
|
98
|
+
end
|
99
|
+
|
100
|
+
def run_validations
|
101
|
+
# call super to ensure module has what it needs
|
102
|
+
super
|
103
|
+
redis_commands = [:smembers, :get, :set, :sadd, :del]
|
104
|
+
if !redis_commands.all? { |c| client.respond_to?(c) }
|
105
|
+
raise NotImplementedError.new("Missing Methods. Your client must implement: #{redis_commands}")
|
106
|
+
end
|
107
|
+
if !namespace
|
108
|
+
raise NotImplementedError.new("Missing namespace")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require "switch_gear/circuit_breaker/failure"
|
2
|
+
require 'switch_gear/circuit_breaker/open_error'
|
3
|
+
require 'switch_gear/circuit_breaker/memory'
|
4
|
+
require 'switch_gear/circuit_breaker/redis'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
module SwitchGear
|
8
|
+
module CircuitBreaker
|
9
|
+
# Calls the circuit proc/lambda if the circuit is closed or half-open
|
10
|
+
#
|
11
|
+
# @param args [Array<Object>] Any number of Objects to be called with the circuit block.
|
12
|
+
# @return [Void, SwitchGear::CircuitBreaker::Open] No usable return value if successful, but will raise an error if failure_limit is reached or if the circuit is open
|
13
|
+
def call(*args)
|
14
|
+
check_reset_timeout
|
15
|
+
raise OpenError if open?
|
16
|
+
do_run(args, &circuit)
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Integer] The count of current failures
|
20
|
+
def failure_count
|
21
|
+
failures.size
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Boolean] Whether the circuit is open
|
25
|
+
def open?
|
26
|
+
state == :open
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Boolean] Whether the circuit is closed
|
30
|
+
def closed?
|
31
|
+
state == :closed
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [Boolean] Whether the circuit is half-open
|
35
|
+
def half_open?
|
36
|
+
state == :half_open
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Array<SwitchGear::CircuitBreaker::Failure>] a list of current failures
|
40
|
+
def failures
|
41
|
+
must_implement(:failures)
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param failure [Array<SwitchGear::CircuitBreaker::Failure>] a list of failures
|
45
|
+
# @return [void]
|
46
|
+
def failures=(failure)
|
47
|
+
must_implement(:failures=)
|
48
|
+
end
|
49
|
+
|
50
|
+
# @param failure [SwitchGear::CircuitBreaker::Failure] a list of failures
|
51
|
+
# @return [void]
|
52
|
+
def add_failure(failure)
|
53
|
+
must_implement(:add_failure)
|
54
|
+
end
|
55
|
+
|
56
|
+
# @return [Symbol] - either :open, :closed, :half-open
|
57
|
+
def state
|
58
|
+
must_implement(:state)
|
59
|
+
end
|
60
|
+
|
61
|
+
# @param state [Symbol] - either :open, :closed, :half-open
|
62
|
+
# @return [void]
|
63
|
+
def state=(state)
|
64
|
+
must_implement(:state=)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def must_implement(arg)
|
70
|
+
raise NotImplementedError.new("You must implement #{arg}.")
|
71
|
+
end
|
72
|
+
|
73
|
+
def do_run(args)
|
74
|
+
yield *args
|
75
|
+
reset_failures
|
76
|
+
rescue => e
|
77
|
+
handle_failure(e)
|
78
|
+
end
|
79
|
+
|
80
|
+
def check_reset_timeout
|
81
|
+
return if !open?
|
82
|
+
if reset_period_lapsed?
|
83
|
+
self.state = :half_open
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def reset_period_lapsed?
|
88
|
+
(Time.now.utc - failures.last.timestamp) > reset_timeout
|
89
|
+
end
|
90
|
+
|
91
|
+
def reset_failures
|
92
|
+
self.failures = []
|
93
|
+
self.state = :closed
|
94
|
+
logger.info "Circuit closed"
|
95
|
+
end
|
96
|
+
|
97
|
+
def handle_failure(e)
|
98
|
+
failure = Failure.new(e)
|
99
|
+
add_failure(failure)
|
100
|
+
logger.warn failure.to_s
|
101
|
+
if half_open? || failures.size >= failure_limit
|
102
|
+
self.state = :open
|
103
|
+
else
|
104
|
+
self.state = :closed
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def run_validations
|
109
|
+
logger_methods = [:debug, :info, :warn, :error]
|
110
|
+
if !logger_methods.all? { |e| logger.respond_to?(e) }
|
111
|
+
raise NotImplementedError.new("Your logger must respond to #{logger_methods}")
|
112
|
+
end
|
113
|
+
if !circuit.respond_to?(:call)
|
114
|
+
raise NotImplementedError.new("Your circuit must respond to #call")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
data/lib/switch_gear.rb
ADDED
data/remote-loop.gif
ADDED
Binary file
|
data/remote.gif
ADDED
Binary file
|
data/switch_gear.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'switch_gear/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "switch_gear"
|
8
|
+
spec.version = SwitchGear::VERSION
|
9
|
+
spec.authors = ["Anthony Ross"]
|
10
|
+
spec.email = ["anthony.s.ross@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{SwitchGear is a library containing the Circuit Breaker pattern}
|
13
|
+
spec.description = %q{SwitchGear is a library which contains the Circuit Breaker pattern. It is used to prevent constant fail-over from spotty remote systems}
|
14
|
+
spec.homepage = "https://github.com/allcentury/switch_gear"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.metadata["yard.run"] = "yri"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "pry", "~> 0.10"
|
30
|
+
spec.add_development_dependency "timecop", "~> 0.8"
|
31
|
+
spec.add_development_dependency "simplecov", "~> 0.13"
|
32
|
+
spec.add_development_dependency "yard", "~> 0.9"
|
33
|
+
spec.add_development_dependency "redis", "~> 3.3"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: switch_gear
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anthony Ross
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: timecop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.8'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.8'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.13'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.13'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: yard
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.9'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.9'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: redis
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.3'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.3'
|
125
|
+
description: SwitchGear is a library which contains the Circuit Breaker pattern. It
|
126
|
+
is used to prevent constant fail-over from spotty remote systems
|
127
|
+
email:
|
128
|
+
- anthony.s.ross@gmail.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".travis.yml"
|
136
|
+
- CODE_OF_CONDUCT.md
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- bin/console
|
142
|
+
- bin/setup
|
143
|
+
- examples/example.rb
|
144
|
+
- examples/example_redis.rb
|
145
|
+
- lib/switch_gear.rb
|
146
|
+
- lib/switch_gear/circuit_breaker.rb
|
147
|
+
- lib/switch_gear/circuit_breaker/failure.rb
|
148
|
+
- lib/switch_gear/circuit_breaker/memory.rb
|
149
|
+
- lib/switch_gear/circuit_breaker/open_error.rb
|
150
|
+
- lib/switch_gear/circuit_breaker/redis.rb
|
151
|
+
- lib/switch_gear/version.rb
|
152
|
+
- remote-loop.gif
|
153
|
+
- remote.gif
|
154
|
+
- switch_gear.gemspec
|
155
|
+
homepage: https://github.com/allcentury/switch_gear
|
156
|
+
licenses:
|
157
|
+
- MIT
|
158
|
+
metadata:
|
159
|
+
yard.run: yri
|
160
|
+
post_install_message:
|
161
|
+
rdoc_options: []
|
162
|
+
require_paths:
|
163
|
+
- lib
|
164
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
requirements: []
|
175
|
+
rubyforge_project:
|
176
|
+
rubygems_version: 2.6.8
|
177
|
+
signing_key:
|
178
|
+
specification_version: 4
|
179
|
+
summary: SwitchGear is a library containing the Circuit Breaker pattern
|
180
|
+
test_files: []
|