cb2 0.0.4 → 1.0.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 +4 -4
- data/Gemfile.lock +3 -1
- data/Gemfile.redis4 +5 -0
- data/Gemfile.redis4.lock +46 -0
- data/Gemfile.redis5 +5 -0
- data/Gemfile.redis5.lock +50 -0
- data/README.md +73 -0
- metadata +22 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3323bac7ddd5764b21f46a29100e035c5fefc75454bc593e8e8a73409cb9f50e
|
4
|
+
data.tar.gz: b3f3353141dcdad1f71eec514869c2379232d041c7afb501283ba6c50b2c0cab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 829d3b1044257eefe478c72ce18f993f3b9620dc029711efdc274a07773130485b41f284a7cd1c01c19895578874475f3c4ecfa43003fdf2d5affa9a2b377388
|
7
|
+
data.tar.gz: ed44bd343dc9d248710ca1232f3bb212bc629c03949585af7be07d86d6a1e1f535a0f3add1f0cc7b0297df6ded3da4ee5ab66956c0d8e5e6a7cda6391032f98c
|
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@ PATH
|
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
4
|
cb2 (0.0.4)
|
5
|
-
redis (
|
5
|
+
redis (>= 4)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
@@ -10,6 +10,7 @@ GEM
|
|
10
10
|
connection_pool (2.4.1)
|
11
11
|
diff-lcs (1.5.0)
|
12
12
|
minitest (5.18.1)
|
13
|
+
mutex_m (0.3.0)
|
13
14
|
rake (13.0.6)
|
14
15
|
redis (5.2.0)
|
15
16
|
redis-client (>= 0.22.0)
|
@@ -37,6 +38,7 @@ PLATFORMS
|
|
37
38
|
DEPENDENCIES
|
38
39
|
cb2!
|
39
40
|
minitest (> 0)
|
41
|
+
mutex_m (> 0)
|
40
42
|
rake (> 0)
|
41
43
|
rr (~> 1.1)
|
42
44
|
rspec (~> 3.1)
|
data/Gemfile.redis4
ADDED
data/Gemfile.redis4.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cb2 (0.0.4)
|
5
|
+
redis (>= 4)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.6.2)
|
11
|
+
minitest (5.25.5)
|
12
|
+
mutex_m (0.3.0)
|
13
|
+
rake (13.2.1)
|
14
|
+
redis (4.8.1)
|
15
|
+
rr (1.2.1)
|
16
|
+
rspec (3.13.1)
|
17
|
+
rspec-core (~> 3.13.0)
|
18
|
+
rspec-expectations (~> 3.13.0)
|
19
|
+
rspec-mocks (~> 3.13.0)
|
20
|
+
rspec-core (3.13.4)
|
21
|
+
rspec-support (~> 3.13.0)
|
22
|
+
rspec-expectations (3.13.5)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.13.0)
|
25
|
+
rspec-mocks (3.13.5)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.13.0)
|
28
|
+
rspec-support (3.13.4)
|
29
|
+
timecop (0.9.10)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
arm64-darwin-24
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
cb2!
|
37
|
+
minitest (> 0)
|
38
|
+
mutex_m (> 0)
|
39
|
+
rake (> 0)
|
40
|
+
redis (~> 4.0)
|
41
|
+
rr (~> 1.1)
|
42
|
+
rspec (~> 3.1)
|
43
|
+
timecop (> 0)
|
44
|
+
|
45
|
+
BUNDLED WITH
|
46
|
+
2.5.23
|
data/Gemfile.redis5
ADDED
data/Gemfile.redis5.lock
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cb2 (0.0.4)
|
5
|
+
redis (>= 4)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
connection_pool (2.5.3)
|
11
|
+
diff-lcs (1.6.2)
|
12
|
+
minitest (5.25.5)
|
13
|
+
mutex_m (0.3.0)
|
14
|
+
rake (13.2.1)
|
15
|
+
redis (5.4.0)
|
16
|
+
redis-client (>= 0.22.0)
|
17
|
+
redis-client (0.24.0)
|
18
|
+
connection_pool
|
19
|
+
rr (1.2.1)
|
20
|
+
rspec (3.13.1)
|
21
|
+
rspec-core (~> 3.13.0)
|
22
|
+
rspec-expectations (~> 3.13.0)
|
23
|
+
rspec-mocks (~> 3.13.0)
|
24
|
+
rspec-core (3.13.4)
|
25
|
+
rspec-support (~> 3.13.0)
|
26
|
+
rspec-expectations (3.13.5)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.13.0)
|
29
|
+
rspec-mocks (3.13.5)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.13.0)
|
32
|
+
rspec-support (3.13.4)
|
33
|
+
timecop (0.9.10)
|
34
|
+
|
35
|
+
PLATFORMS
|
36
|
+
arm64-darwin-24
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
cb2!
|
41
|
+
minitest (> 0)
|
42
|
+
mutex_m (> 0)
|
43
|
+
rake (> 0)
|
44
|
+
redis (~> 5.0)
|
45
|
+
rr (~> 1.1)
|
46
|
+
rspec (~> 3.1)
|
47
|
+
timecop (> 0)
|
48
|
+
|
49
|
+
BUNDLED WITH
|
50
|
+
2.5.23
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# CB2
|
2
|
+
|
3
|
+
[](https://rubygems.org/gems/cb2)
|
4
|
+
[](https://github.com/prognostikos/cb2/actions)
|
5
|
+
|
6
|
+
Implementation of the [circuit breaker pattern](http://martinfowler.com/bliki/CircuitBreaker.html) in Ruby, backed by Redis.
|
7
|
+
|
8
|
+
Setup circuit breakers wrapping external service calls, be it HTTP, TCP, etc. When a service becomes unavailable the circuit breaker will open and quickly refuse any additional requests to it. After a specific window the breaker closes again, allowing calls to go through.
|
9
|
+
|
10
|
+
This pattern makes your application more resilient to third party failures, because it won't exhaust resources trying to make calls to an unresponsive service. This is particularly relevant to apps running on servers susceptible to request queuing, like Unicorn or Puma. It can also help the services you depend on to recover faster, by reducing the load on them.
|
11
|
+
|
12
|
+
CB2 tracks errors over a rolling window of time (size configurable), and opens after the error rate hits a certain percentage. The circuit stays open (rejecting calls) for another specified period, and then from there it goes to the half-open state: in which a succesful request will make it close again, but a failure will put it immediatelly back at the open state. [Martin Fowler has a nice diagram to further explain these states](http://martinfowler.com/bliki/CircuitBreaker.html).
|
13
|
+
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Instantiate a circuit breaker:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
breaker = CB2::Breaker.new(
|
21
|
+
service: "aws" # identify each circuit breaker individually
|
22
|
+
duration: 60, # keep track of errors over a 1 min window
|
23
|
+
threshold: 5, # open the circuit breaker when error rate is at 5%
|
24
|
+
reenable_after: 600, # keep it open for 10 mins
|
25
|
+
redis: Redis.new) # redis connection it should use to keep state
|
26
|
+
```
|
27
|
+
|
28
|
+
Then wrap service calls with it:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
breaker.run do
|
32
|
+
some_api_request()
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
The breaker will open when that block raises enough exceptions to trigger the threshold. Handle these exceptions to react accordingly:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
begin
|
40
|
+
breaker.run do
|
41
|
+
some_api_request()
|
42
|
+
end
|
43
|
+
rescue CB2::BreakerOpen
|
44
|
+
alternate_response() # fallback to cached data, or raise a user-friendly exception
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
### Circuit breaker stub
|
49
|
+
|
50
|
+
CB2 can also run as a stub. Use it to aid testing, simulations and gradual rollouts:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
breaker = CB2::Breaker.new(
|
54
|
+
strategy: "stub",
|
55
|
+
allow: true) # set it to false to always block requests
|
56
|
+
```
|
57
|
+
|
58
|
+
## See also
|
59
|
+
|
60
|
+
This might be the only percent-based breaker in Ruby so far. For count-based breakers (eg: open after X exceptions) you can also check [breaker_box](https://github.com/sittercity/breaker_box) and [circuit_breaker](https://github.com/wsargent/circuit_breaker).
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
Running the tests locally requires a running `redis` service listening on port `6379`. If the service is not running locally, use a `container`:
|
65
|
+
|
66
|
+
```
|
67
|
+
docker run -p 6379:6379 -d redis
|
68
|
+
bundle exec rspec
|
69
|
+
```
|
70
|
+
|
71
|
+
## Meta
|
72
|
+
|
73
|
+
Created by Pedro Belo.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cb2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Belo
|
@@ -9,15 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2025-05-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "~>"
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '5'
|
21
18
|
- - ">="
|
22
19
|
- !ruby/object:Gem::Version
|
23
20
|
version: '4'
|
@@ -25,9 +22,6 @@ dependencies:
|
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
|
-
- - "~>"
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: '5'
|
31
25
|
- - ">="
|
32
26
|
- !ruby/object:Gem::Version
|
33
27
|
version: '4'
|
@@ -101,6 +95,20 @@ dependencies:
|
|
101
95
|
- - ">"
|
102
96
|
- !ruby/object:Gem::Version
|
103
97
|
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: mutex_m
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">"
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
104
112
|
description: Implementation of the circuit breaker pattern in Ruby
|
105
113
|
email: matt@prognostikos.com
|
106
114
|
executables: []
|
@@ -109,6 +117,11 @@ extra_rdoc_files: []
|
|
109
117
|
files:
|
110
118
|
- Gemfile
|
111
119
|
- Gemfile.lock
|
120
|
+
- Gemfile.redis4
|
121
|
+
- Gemfile.redis4.lock
|
122
|
+
- Gemfile.redis5
|
123
|
+
- Gemfile.redis5.lock
|
124
|
+
- README.md
|
112
125
|
- lib/cb2.rb
|
113
126
|
- lib/cb2/breaker.rb
|
114
127
|
- lib/cb2/error.rb
|
@@ -134,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
147
|
- !ruby/object:Gem::Version
|
135
148
|
version: '0'
|
136
149
|
requirements: []
|
137
|
-
rubygems_version: 3.5.
|
150
|
+
rubygems_version: 3.5.22
|
138
151
|
signing_key:
|
139
152
|
specification_version: 4
|
140
153
|
summary: Circuit breaker
|