berater 0.14.0 → 0.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/berater/limiter.rb +10 -13
- data/lib/berater/middleware/fail_open.rb +5 -2
- data/lib/berater/middleware/trace.rb +5 -3
- data/lib/berater/rspec/matchers.rb +2 -2
- data/lib/berater/version.rb +1 -1
- data/spec/concurrency_limiter_spec.rb +4 -4
- data/spec/middleware/trace_spec.rb +10 -0
- data/spec/mutex_spec.rb +35 -0
- data/spec/rate_limiter_spec.rb +4 -4
- data/spec/static_limiter_spec.rb +3 -3
- metadata +6 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcfe29f7b98bcd132e05ba7cadbc05a128da9f4737eaa383e330bfd4d72832fb
|
4
|
+
data.tar.gz: 7750d96b8df130de0e9dd11c81f69b021543567c6e7f63087839dc17bbaf309c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d17e3d67ebdc5644348011b1d03b5850bceeeae4533d25a6a67f1cdadf8071312c0a397ae15560f01ad7ef123f68dfd7a19c55040ff5af5b7c17883f0c33f265
|
7
|
+
data.tar.gz: 95d37d05e057c5b536ce724308a91f1be09211a3e3687dced98618f5836d9d992b4dfb51f00b074a65dd36e9664344b032703f02c9883600db348b411b2f18e2
|
data/lib/berater/limiter.rb
CHANGED
@@ -52,22 +52,24 @@ module Berater
|
|
52
52
|
|
53
53
|
acquire_lock(capacity: capacity, cost: cost, **opts)
|
54
54
|
rescue NoMethodError => e
|
55
|
-
|
55
|
+
if e.message.include?("undefined method `evalsha' for")
|
56
|
+
# repackage error so it's easier to understand
|
57
|
+
raise RuntimeError, "invalid redis connection: #{redis}"
|
58
|
+
end
|
56
59
|
|
57
|
-
|
58
|
-
raise RuntimeError, "invalid redis connection: #{redis}"
|
60
|
+
raise
|
59
61
|
end
|
60
62
|
|
61
63
|
def utilization
|
62
64
|
lock = limit(cost: 0)
|
63
65
|
|
64
66
|
if lock.capacity == 0
|
65
|
-
|
67
|
+
100.0
|
66
68
|
else
|
67
|
-
lock.contention.to_f / lock.capacity
|
69
|
+
lock.contention.to_f / lock.capacity * 100
|
68
70
|
end
|
69
71
|
rescue Berater::Overloaded
|
70
|
-
|
72
|
+
100.0
|
71
73
|
end
|
72
74
|
|
73
75
|
def ==(other)
|
@@ -111,7 +113,7 @@ module Berater
|
|
111
113
|
@capacity = capacity
|
112
114
|
end
|
113
115
|
|
114
|
-
def acquire_lock(
|
116
|
+
def acquire_lock(**options)
|
115
117
|
raise NotImplementedError
|
116
118
|
end
|
117
119
|
|
@@ -124,12 +126,7 @@ module Berater
|
|
124
126
|
# can only call via subclass
|
125
127
|
raise NoMethodError if self == Berater::Limiter
|
126
128
|
|
127
|
-
|
128
|
-
# avoid ruby 2 problems with empty hashes
|
129
|
-
super(*args)
|
130
|
-
else
|
131
|
-
super
|
132
|
-
end
|
129
|
+
super
|
133
130
|
end
|
134
131
|
|
135
132
|
def cache_key(key)
|
@@ -3,9 +3,12 @@ require 'set'
|
|
3
3
|
module Berater
|
4
4
|
module Middleware
|
5
5
|
class FailOpen
|
6
|
-
ERRORS = Set[
|
6
|
+
ERRORS = Set.new [
|
7
7
|
Redis::BaseConnectionError,
|
8
|
-
|
8
|
+
SystemCallError, # openssl
|
9
|
+
Timeout::Error, # connection pool
|
10
|
+
(OpenSSL::OpenSSLError if defined?(OpenSSL)),
|
11
|
+
].compact
|
9
12
|
|
10
13
|
def initialize(errors: nil, on_fail: nil)
|
11
14
|
@errors = errors || ERRORS
|
@@ -6,10 +6,12 @@ module Berater
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def call(limiter, **)
|
9
|
-
|
9
|
+
return yield unless tracer
|
10
|
+
|
11
|
+
tracer.trace('Berater') do |span|
|
10
12
|
begin
|
11
13
|
lock = yield
|
12
|
-
rescue
|
14
|
+
rescue StandardError => error
|
13
15
|
# capture exception for reporting, then propagate
|
14
16
|
raise
|
15
17
|
ensure
|
@@ -32,7 +34,7 @@ module Berater
|
|
32
34
|
private
|
33
35
|
|
34
36
|
def tracer
|
35
|
-
@tracer || (defined?(Datadog
|
37
|
+
@tracer || (defined?(Datadog) && Datadog::Tracing)
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
@@ -14,7 +14,7 @@ module Berater
|
|
14
14
|
if res.is_a? Berater::Limiter
|
15
15
|
# eg. expect { Berater.new(...) }.to be_overloaded
|
16
16
|
@limiter = res
|
17
|
-
@limiter.utilization >=
|
17
|
+
@limiter.utilization >= 100
|
18
18
|
else
|
19
19
|
# eg. expect { Berater(...) }.to be_overloaded
|
20
20
|
# eg. expect { limiter.limit }.to be_overloaded
|
@@ -23,7 +23,7 @@ module Berater
|
|
23
23
|
when Berater::Limiter
|
24
24
|
# eg. expect(Berater.new(...)).to be_overloaded
|
25
25
|
@limiter = obj
|
26
|
-
@limiter.utilization >=
|
26
|
+
@limiter.utilization >= 100
|
27
27
|
end
|
28
28
|
rescue Berater::Overloaded
|
29
29
|
true
|
data/lib/berater/version.rb
CHANGED
@@ -255,19 +255,19 @@ describe Berater::ConcurrencyLimiter do
|
|
255
255
|
let(:limiter) { described_class.new(:key, 10, timeout: 30) }
|
256
256
|
|
257
257
|
it 'works' do
|
258
|
-
expect(limiter.utilization).to
|
258
|
+
expect(limiter.utilization).to eq 0
|
259
259
|
|
260
260
|
2.times { limiter.limit }
|
261
|
-
expect(limiter.utilization).to
|
261
|
+
expect(limiter.utilization).to eq 20
|
262
262
|
|
263
263
|
Timecop.freeze(15)
|
264
264
|
|
265
265
|
8.times { limiter.limit }
|
266
|
-
expect(limiter.utilization).to
|
266
|
+
expect(limiter.utilization).to eq 100
|
267
267
|
|
268
268
|
Timecop.freeze(15)
|
269
269
|
|
270
|
-
expect(limiter.utilization).to
|
270
|
+
expect(limiter.utilization).to eq 80
|
271
271
|
end
|
272
272
|
end
|
273
273
|
|
@@ -65,6 +65,16 @@ describe Berater::Middleware::Trace do
|
|
65
65
|
}.to raise_error(IOError)
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
69
|
+
context 'when tracer is not defined' do
|
70
|
+
before do
|
71
|
+
allow(instance).to receive(:tracer).and_return(nil)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'still yields' do
|
75
|
+
expect {|b| instance.call(limiter, &b) }.to yield_control
|
76
|
+
end
|
77
|
+
end
|
68
78
|
end
|
69
79
|
|
70
80
|
context 'when used as middleware' do
|
data/spec/mutex_spec.rb
CHANGED
@@ -107,4 +107,39 @@ describe Berater::Mutex do
|
|
107
107
|
it { expect(klass.new).not_to respond_to(:synchronize) }
|
108
108
|
it { expect(klass.new).not_to respond_to(:mutex_options) }
|
109
109
|
end
|
110
|
+
|
111
|
+
describe 'when used in a counter' do
|
112
|
+
subject(:counter) { klass.new }
|
113
|
+
|
114
|
+
let(:klass) do
|
115
|
+
class Counter
|
116
|
+
include Berater::Mutex
|
117
|
+
|
118
|
+
@@count = 0
|
119
|
+
@@counts = {}
|
120
|
+
|
121
|
+
def incr
|
122
|
+
synchronize { @@count += 1 }
|
123
|
+
end
|
124
|
+
|
125
|
+
def incr_key(key)
|
126
|
+
synchronize(key) do
|
127
|
+
@@counts[key] ||= 0
|
128
|
+
@@counts[key] += 1
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
Counter
|
133
|
+
end
|
134
|
+
|
135
|
+
it { expect(counter.incr).to eq 1 }
|
136
|
+
it { expect(counter.incr_key(:a)).to eq 1 }
|
137
|
+
|
138
|
+
it 'separates keys' do
|
139
|
+
res = 3.times.map { counter.incr_key(:a) }
|
140
|
+
expect(res).to eq [ 1, 2, 3 ]
|
141
|
+
|
142
|
+
expect(counter.incr_key(:b)).to eq 1
|
143
|
+
end
|
144
|
+
end
|
110
145
|
end
|
data/spec/rate_limiter_spec.rb
CHANGED
@@ -237,17 +237,17 @@ describe Berater::RateLimiter do
|
|
237
237
|
let(:limiter) { described_class.new(:key, 10, :minute) }
|
238
238
|
|
239
239
|
it do
|
240
|
-
expect(limiter.utilization).to
|
240
|
+
expect(limiter.utilization).to eq 0
|
241
241
|
|
242
242
|
2.times { limiter.limit }
|
243
|
-
expect(limiter.utilization).to
|
243
|
+
expect(limiter.utilization).to eq 20
|
244
244
|
|
245
245
|
8.times { limiter.limit }
|
246
|
-
expect(limiter.utilization).to
|
246
|
+
expect(limiter.utilization).to eq 100
|
247
247
|
|
248
248
|
Timecop.freeze(30)
|
249
249
|
|
250
|
-
expect(limiter.utilization).to
|
250
|
+
expect(limiter.utilization).to eq 50
|
251
251
|
end
|
252
252
|
end
|
253
253
|
|
data/spec/static_limiter_spec.rb
CHANGED
@@ -60,13 +60,13 @@ describe Berater::StaticLimiter do
|
|
60
60
|
let(:limiter) { described_class.new(:key, 10) }
|
61
61
|
|
62
62
|
it do
|
63
|
-
expect(limiter.utilization).to
|
63
|
+
expect(limiter.utilization).to eq 0
|
64
64
|
|
65
65
|
2.times { limiter.limit }
|
66
|
-
expect(limiter.utilization).to
|
66
|
+
expect(limiter.utilization).to eq 20
|
67
67
|
|
68
68
|
8.times { limiter.limit }
|
69
|
-
expect(limiter.utilization).to
|
69
|
+
expect(limiter.utilization).to eq 100
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: berater
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.15.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Pepper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: meddleware
|
@@ -66,34 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: codecov
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
70
|
name: ddtrace
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
86
72
|
requirements:
|
87
73
|
- - ">="
|
88
74
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
75
|
+
version: '1'
|
90
76
|
type: :development
|
91
77
|
prerelease: false
|
92
78
|
version_requirements: !ruby/object:Gem::Requirement
|
93
79
|
requirements:
|
94
80
|
- - ">="
|
95
81
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
82
|
+
version: '1'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: dogstatsd-ruby
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,20 +94,6 @@ dependencies:
|
|
108
94
|
- - ">="
|
109
95
|
- !ruby/object:Gem::Version
|
110
96
|
version: '4.3'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: rake
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
requirements:
|
115
|
-
- - ">="
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
requirements:
|
122
|
-
- - ">="
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
version: '0'
|
125
97
|
- !ruby/object:Gem::Dependency
|
126
98
|
name: rspec
|
127
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -225,14 +197,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
225
197
|
requirements:
|
226
198
|
- - ">="
|
227
199
|
- !ruby/object:Gem::Version
|
228
|
-
version: '
|
200
|
+
version: '3'
|
229
201
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
230
202
|
requirements:
|
231
203
|
- - ">="
|
232
204
|
- !ruby/object:Gem::Version
|
233
205
|
version: '0'
|
234
206
|
requirements: []
|
235
|
-
rubygems_version: 3.
|
207
|
+
rubygems_version: 3.2.33
|
236
208
|
signing_key:
|
237
209
|
specification_version: 4
|
238
210
|
summary: Berater
|