cb2 0.0.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7745238479e3b771cd4250afd72b38628690b89c
4
- data.tar.gz: 4a52b01231637cbfce2e2ab95f5a1ef05137a525
2
+ SHA256:
3
+ metadata.gz: 3323bac7ddd5764b21f46a29100e035c5fefc75454bc593e8e8a73409cb9f50e
4
+ data.tar.gz: b3f3353141dcdad1f71eec514869c2379232d041c7afb501283ba6c50b2c0cab
5
5
  SHA512:
6
- metadata.gz: 050ca6b08571141c02da98e85a064f76039955e180e14ddae4b071dfbb18fe6f3da4077e705f9809e9576e9553dd0b971c6241368736dfbaba93f3ec6412ccea
7
- data.tar.gz: 8b21fa54a558a8cb7caf67f6c22c37d05aa208505913bcfcfeb98cedbfc1732dd64044cc315478166410e37667af23bdcbab8e04d25b79731511e9aa3c67d8c4
6
+ metadata.gz: 829d3b1044257eefe478c72ce18f993f3b9620dc029711efdc274a07773130485b41f284a7cd1c01c19895578874475f3c4ecfa43003fdf2d5affa9a2b377388
7
+ data.tar.gz: ed44bd343dc9d248710ca1232f3bb212bc629c03949585af7be07d86d6a1e1f535a0f3add1f0cc7b0297df6ded3da4ee5ab66956c0d8e5e6a7cda6391032f98c
data/Gemfile.lock CHANGED
@@ -1,36 +1,48 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cb2 (0.0.2)
5
- redis (~> 3.1)
4
+ cb2 (0.0.4)
5
+ redis (>= 4)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- diff-lcs (1.2.5)
11
- rake (10.3.2)
12
- redis (3.1.0)
10
+ connection_pool (2.4.1)
11
+ diff-lcs (1.5.0)
12
+ minitest (5.18.1)
13
+ mutex_m (0.3.0)
14
+ rake (13.0.6)
15
+ redis (5.2.0)
16
+ redis-client (>= 0.22.0)
17
+ redis-client (0.22.2)
18
+ connection_pool
13
19
  rr (1.1.2)
14
- rspec (3.1.0)
15
- rspec-core (~> 3.1.0)
16
- rspec-expectations (~> 3.1.0)
17
- rspec-mocks (~> 3.1.0)
18
- rspec-core (3.1.7)
19
- rspec-support (~> 3.1.0)
20
- rspec-expectations (3.1.2)
20
+ rspec (3.12.0)
21
+ rspec-core (~> 3.12.0)
22
+ rspec-expectations (~> 3.12.0)
23
+ rspec-mocks (~> 3.12.0)
24
+ rspec-core (3.12.2)
25
+ rspec-support (~> 3.12.0)
26
+ rspec-expectations (3.12.3)
21
27
  diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.1.0)
23
- rspec-mocks (3.1.3)
24
- rspec-support (~> 3.1.0)
25
- rspec-support (3.1.2)
26
- timecop (0.7.1)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-mocks (3.12.6)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-support (3.12.1)
33
+ timecop (0.9.6)
27
34
 
28
35
  PLATFORMS
29
36
  ruby
30
37
 
31
38
  DEPENDENCIES
32
39
  cb2!
40
+ minitest (> 0)
41
+ mutex_m (> 0)
33
42
  rake (> 0)
34
43
  rr (~> 1.1)
35
44
  rspec (~> 3.1)
36
- timecop (~> 0.7)
45
+ timecop (> 0)
46
+
47
+ BUNDLED WITH
48
+ 2.3.26
data/Gemfile.redis4 ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "redis", "~> 4.0"
@@ -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
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "redis", "~> 5.0"
@@ -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
+ [![Gem version](http://img.shields.io/gem/v/cb2.svg)](https://rubygems.org/gems/cb2)
4
+ [![Build Status](https://github.com/prognostikos/cb2/actions/workflows/verify.yml/badge.svg)](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.
data/lib/cb2/breaker.rb CHANGED
@@ -8,7 +8,7 @@ class CB2::Breaker
8
8
 
9
9
  def run
10
10
  if open?
11
- raise CB2::BreakerOpen
11
+ raise CB2::BreakerOpen.new("#{service} breaker open")
12
12
  end
13
13
 
14
14
  begin
@@ -6,7 +6,7 @@ class CB2::RollingWindow
6
6
  @duration = options.fetch(:duration)
7
7
  @threshold = options.fetch(:threshold)
8
8
  @reenable_after = options.fetch(:reenable_after)
9
- @redis = options[:redis] || Redis.current
9
+ @redis = options[:redis] || Redis.new
10
10
  end
11
11
 
12
12
  def open?
@@ -54,16 +54,17 @@ class CB2::RollingWindow
54
54
  protected
55
55
 
56
56
  def increment_rolling_window(key)
57
- t = Time.now.to_i
58
- pipeline = redis.pipelined do
57
+ t = Time.now.to_i
58
+ result = redis.pipelined do |pipeline|
59
59
  # keep the sorted set clean
60
- redis.zremrangebyscore(key, "-inf", t - duration)
60
+ pipeline.zremrangebyscore(key, "-inf", t - duration)
61
61
  # add as a random uuid because sorted sets won't take duplicate items:
62
- redis.zadd(key, t, SecureRandom.uuid)
62
+ pipeline.zadd(key, t, SecureRandom.uuid)
63
63
  # just count how many errors are left in the set
64
- redis.zcard(key)
64
+ pipeline.zcard(key)
65
65
  end
66
- return pipeline.last # return the count
66
+
67
+ result.last # return the count
67
68
  end
68
69
 
69
70
  def should_open?(error_count)
data/lib/cb2.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "redis"
2
+ require "securerandom"
2
3
 
3
4
  module CB2
4
5
  end
metadata CHANGED
@@ -1,29 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cb2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Belo
8
- autorequire:
8
+ - Matt Rohrer
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-06-11 00:00:00.000000000 Z
12
+ date: 2025-05-29 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: redis
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - "~>"
18
+ - - ">="
18
19
  - !ruby/object:Gem::Version
19
- version: '3.1'
20
+ version: '4'
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
- - - "~>"
25
+ - - ">="
25
26
  - !ruby/object:Gem::Version
26
- version: '3.1'
27
+ version: '4'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: rake
29
30
  requirement: !ruby/object:Gem::Requirement
@@ -66,38 +67,72 @@ dependencies:
66
67
  - - "~>"
67
68
  - !ruby/object:Gem::Version
68
69
  version: '3.1'
70
+ - !ruby/object:Gem::Dependency
71
+ name: minitest
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">"
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">"
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
69
84
  - !ruby/object:Gem::Dependency
70
85
  name: timecop
71
86
  requirement: !ruby/object:Gem::Requirement
72
87
  requirements:
73
- - - "~>"
88
+ - - ">"
74
89
  - !ruby/object:Gem::Version
75
- version: '0.7'
90
+ version: '0'
76
91
  type: :development
77
92
  prerelease: false
78
93
  version_requirements: !ruby/object:Gem::Requirement
79
94
  requirements:
80
- - - "~>"
95
+ - - ">"
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: mutex_m
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">"
81
103
  - !ruby/object:Gem::Version
82
- version: '0.7'
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'
83
112
  description: Implementation of the circuit breaker pattern in Ruby
84
- email: pedrobelo@gmail.com
113
+ email: matt@prognostikos.com
85
114
  executables: []
86
115
  extensions: []
87
116
  extra_rdoc_files: []
88
117
  files:
89
118
  - Gemfile
90
119
  - Gemfile.lock
120
+ - Gemfile.redis4
121
+ - Gemfile.redis4.lock
122
+ - Gemfile.redis5
123
+ - Gemfile.redis5.lock
124
+ - README.md
91
125
  - lib/cb2.rb
92
126
  - lib/cb2/breaker.rb
93
127
  - lib/cb2/error.rb
94
128
  - lib/cb2/strategies/percentage.rb
95
129
  - lib/cb2/strategies/rolling_window.rb
96
130
  - lib/cb2/strategies/stub.rb
97
- homepage: http://github.com/pedro/cb2
98
- licenses: []
131
+ homepage: http://github.com/prognostikos/cb2
132
+ licenses:
133
+ - MIT
99
134
  metadata: {}
100
- post_install_message:
135
+ post_install_message:
101
136
  rdoc_options: []
102
137
  require_paths:
103
138
  - lib
@@ -112,10 +147,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
147
  - !ruby/object:Gem::Version
113
148
  version: '0'
114
149
  requirements: []
115
- rubyforge_project:
116
- rubygems_version: 2.4.3
117
- signing_key:
150
+ rubygems_version: 3.5.22
151
+ signing_key:
118
152
  specification_version: 4
119
153
  summary: Circuit breaker
120
154
  test_files: []
121
- has_rdoc: