berater 0.10.1 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e691d2d3cdc8f002e0e222c24d2e6f9a52ccb1dac7edaa92ea232ecce17fb77e
4
- data.tar.gz: 5d302fff5fd063f34a94b1aae1b9b2e118b18efbe2315b4591405346f1dbc70c
3
+ metadata.gz: 5d3ef54656151bf378670caca07686db842a007e87e51d14195cc3f2b8d34d34
4
+ data.tar.gz: 22c6b65158ba926e78cd013c54913024c617eb5c2932297f7e4e424a72f66a14
5
5
  SHA512:
6
- metadata.gz: 88b8e91557729601336b5235fbcc9710c79aa2f68ff393a09b5c55a1c994cef7371ed4a310c8038b07bdfc5f35d6b367ad7e1fc886c3f7c8579130808af5386b
7
- data.tar.gz: 2600478d9761b14abbc2ef6c1a38b2e5b5e7476a40c6955f110746d4a90e31baabbda8d6571dcaf7d4e457d0cc19f120e84cba8915ded01b2a680111806d4400
6
+ metadata.gz: 2743af38d4daf9cd5d18a06368834545add59fb6d192fdf17c7f503ab059961177c444e5b09efde6a8ed6739b4cca04d83e7e88f8d904d0bd3e2b1bc94220cfb
7
+ data.tar.gz: a6750a44bcd05a1d0b25ff770d3c1d0f6fd3fd331cfebebbecdebd4ef06bc4f5a34d350cc36414cce7b212e7e63f8bb1d10cc4d25788ae50e1b973dfd280be4e
@@ -7,12 +7,12 @@ module Berater
7
7
  options[:redis] || Berater.redis
8
8
  end
9
9
 
10
- def limit(capacity: nil, cost: 1, &block)
11
- capacity ||= @capacity
12
- lock = nil
10
+ def limit(**opts, &block)
11
+ opts[:capacity] ||= @capacity
12
+ opts[:cost] ||= 1
13
13
 
14
- Berater.middleware.call(self, capacity: capacity, cost: cost) do |limiter, **opts|
15
- lock = limiter.inner_limit(**opts)
14
+ lock = Berater.middleware.call(self, **opts) do |limiter, **opts|
15
+ limiter.inner_limit(**opts)
16
16
  end
17
17
 
18
18
  if block_given?
@@ -27,10 +27,24 @@ module Berater
27
27
  end
28
28
 
29
29
  protected def inner_limit(capacity:, cost:)
30
+ if capacity.is_a?(String)
31
+ # try casting
32
+ begin
33
+ capacity = Float(capacity)
34
+ rescue ArgumentError; end
35
+ end
36
+
30
37
  unless capacity.is_a?(Numeric) && capacity >= 0
31
38
  raise ArgumentError, "invalid capacity: #{capacity}"
32
39
  end
33
40
 
41
+ if cost.is_a?(String)
42
+ # try casting
43
+ begin
44
+ cost = Float(cost)
45
+ rescue ArgumentError; end
46
+ end
47
+
34
48
  unless cost.is_a?(Numeric) && cost >= 0 && cost < Float::INFINITY
35
49
  raise ArgumentError, "invalid cost: #{cost}"
36
50
  end
@@ -76,6 +90,13 @@ module Berater
76
90
  end
77
91
 
78
92
  def capacity=(capacity)
93
+ if capacity.is_a?(String)
94
+ # try casting
95
+ begin
96
+ capacity = Float(capacity)
97
+ rescue TypeError, ArgumentError; end
98
+ end
99
+
79
100
  unless capacity.is_a?(Numeric)
80
101
  raise ArgumentError, "expected Numeric, found #{capacity.class}"
81
102
  end
@@ -0,0 +1,43 @@
1
+ require 'set'
2
+
3
+ module Berater
4
+ module Middleware
5
+ class FailOpen
6
+ ERRORS = Set[
7
+ Redis::BaseConnectionError,
8
+ ]
9
+
10
+ def initialize(errors: nil, on_fail: nil)
11
+ @errors = errors || ERRORS
12
+ @on_fail = on_fail
13
+ end
14
+
15
+ def call(*, **opts)
16
+ yield.tap do |lock|
17
+ return lock unless lock&.is_a?(Berater::Lock)
18
+
19
+ # wrap lock.release so it fails open
20
+
21
+ # save reference to original function
22
+ release_fn = lock.method(:release)
23
+
24
+ # make bound variables accessible to block
25
+ errors = @errors
26
+ on_fail = @on_fail
27
+
28
+ lock.define_singleton_method(:release) do
29
+ release_fn.call
30
+ rescue *errors => e
31
+ on_fail&.call(e)
32
+ false
33
+ end
34
+ end
35
+ rescue *@errors => e
36
+ @on_fail&.call(e)
37
+
38
+ # fail open by faking a lock
39
+ Berater::Lock.new(opts[:capacity], -1)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ module Berater
2
+ module Middleware
3
+ class LoadShedder
4
+ PRIORITY_RANGE = 1..5
5
+
6
+ def initialize(default_priority: nil)
7
+ @default_priority = default_priority
8
+ end
9
+
10
+ def call(*args, **opts)
11
+ if priority = opts.delete(:priority) || @default_priority
12
+ opts[:capacity] = adjust_capacity(opts[:capacity], priority)
13
+ end
14
+
15
+ yield *args, **opts
16
+ end
17
+
18
+ protected
19
+
20
+ def adjust_capacity(capacity, priority)
21
+ if priority.is_a?(String)
22
+ # try casting
23
+ priority = Float(priority) rescue nil
24
+ end
25
+
26
+ unless PRIORITY_RANGE.include?(priority)
27
+ return capacity
28
+ end
29
+
30
+ # priority 1 stays at 100%, 2 scales down to 90%, 5 to 60%
31
+ factor = 1 - (priority - 1) * 0.1
32
+ (capacity * factor).floor
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,94 @@
1
+ module Berater
2
+ module Middleware
3
+ class Statsd
4
+ def initialize(client, tags: {})
5
+ @client = client
6
+ @tags = tags
7
+ end
8
+
9
+ def call(limiter, **opts)
10
+ duration = -Process.clock_gettime(Process::CLOCK_MONOTONIC)
11
+ lock = yield
12
+ rescue Exception => error
13
+ # note exception and propagate
14
+ raise
15
+ ensure
16
+ duration += Process.clock_gettime(Process::CLOCK_MONOTONIC)
17
+ duration = (duration * 1_000).round(2) # milliseconds
18
+
19
+ tags = build_tags(limiter, opts)
20
+
21
+ @client.timing(
22
+ 'berater.limiter.limit',
23
+ duration,
24
+ tags: tags,
25
+ )
26
+
27
+ @client.gauge(
28
+ 'berater.limiter.capacity',
29
+ limiter.capacity,
30
+ tags: tags,
31
+ )
32
+
33
+ if lock
34
+ @client.increment(
35
+ 'berater.lock.acquired',
36
+ tags: tags,
37
+ )
38
+
39
+ if lock.contention > 0 # not a failsafe lock
40
+ @client.gauge(
41
+ 'berater.lock.capacity',
42
+ lock.capacity,
43
+ tags: tags,
44
+ )
45
+ @client.gauge(
46
+ 'berater.limiter.contention',
47
+ lock.contention,
48
+ tags: tags,
49
+ )
50
+ end
51
+ end
52
+
53
+ if error
54
+ if error.is_a?(Berater::Overloaded)
55
+ @client.increment(
56
+ 'berater.limiter.overloaded',
57
+ tags: tags,
58
+ )
59
+
60
+ # overloaded, so contention >= capacity
61
+ @client.gauge(
62
+ 'berater.limiter.contention',
63
+ limiter.capacity,
64
+ tags: tags,
65
+ )
66
+ else
67
+ @client.increment(
68
+ 'berater.limiter.error',
69
+ tags: tags.merge(type: error.class.to_s.gsub('::', '_'))
70
+ )
71
+ end
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def build_tags(limiter, opts)
78
+ tags = {
79
+ key: limiter.key,
80
+ limiter: limiter.class.to_s.split(':')[-1],
81
+ }
82
+
83
+ # append custom tags
84
+ if @tags.respond_to?(:call)
85
+ tags.merge!(@tags.call(limiter, **opts) || {})
86
+ else
87
+ tags.merge!(@tags)
88
+ end
89
+
90
+ tags.merge!(opts.fetch(:tags, {}))
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,7 @@
1
+ module Berater
2
+ module Middleware
3
+ autoload 'FailOpen', 'berater/middleware/fail_open'
4
+ autoload 'LoadShedder', 'berater/middleware/load_shedder'
5
+ autoload 'Statsd', 'berater/middleware/statsd'
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Berater
2
- VERSION = "0.10.1"
2
+ VERSION = "0.12.1"
3
3
  end
data/lib/berater.rb CHANGED
@@ -2,6 +2,7 @@ require 'berater/limiter'
2
2
  require 'berater/limiter_set'
3
3
  require 'berater/lock'
4
4
  require 'berater/lua_script'
5
+ require 'berater/middleware'
5
6
  require 'berater/utils'
6
7
  require 'berater/version'
7
8
  require 'meddleware'
@@ -51,8 +52,8 @@ module Berater
51
52
  end
52
53
 
53
54
  def expunge
54
- redis.scan_each(match: "#{self.name}*") do |key|
55
- redis.del key
55
+ redis.scan_each(match: "#{name}*") do |key|
56
+ redis.del(key)
56
57
  end
57
58
  end
58
59
 
@@ -24,6 +24,7 @@ describe Berater::ConcurrencyLimiter do
24
24
  it { expect_capacity(0) }
25
25
  it { expect_capacity(1) }
26
26
  it { expect_capacity(1.5) }
27
+ it { expect_capacity('1.5') }
27
28
  it { expect_capacity(10_000) }
28
29
 
29
30
  context 'with erroneous values' do
@@ -34,7 +35,7 @@ describe Berater::ConcurrencyLimiter do
34
35
  end
35
36
 
36
37
  it { expect_bad_capacity(-1) }
37
- it { expect_bad_capacity('1') }
38
+ it { expect_bad_capacity('abc') }
38
39
  it { expect_bad_capacity(:one) }
39
40
  it { expect_bad_capacity(Float::INFINITY) }
40
41
  end
data/spec/limiter_spec.rb CHANGED
@@ -14,6 +14,34 @@ describe Berater::Limiter do
14
14
  end
15
15
  end
16
16
 
17
+ describe '#capacity=' do
18
+ subject do
19
+ Berater::RateLimiter.new(:key, capacity, :second).capacity
20
+ end
21
+
22
+ context 'when capacity is numeric' do
23
+ let(:capacity) { 3.5 }
24
+
25
+ it { is_expected.to be capacity }
26
+ end
27
+
28
+ context 'when capacity is a stringified numeric' do
29
+ let(:capacity) { '3.5' }
30
+
31
+ it 'casts the value' do
32
+ is_expected.to be capacity.to_f
33
+ end
34
+ end
35
+
36
+ context 'when capacity is a bogus value' do
37
+ let(:capacity) { :abc }
38
+
39
+ it 'raises' do
40
+ expect { subject }.to raise_error(ArgumentError)
41
+ end
42
+ end
43
+ end
44
+
17
45
  describe '#limit' do
18
46
  subject { Berater::Unlimiter.new }
19
47
 
@@ -29,6 +57,12 @@ describe Berater::Limiter do
29
57
  subject.limit(capacity: 'abc')
30
58
  }.to raise_error(ArgumentError)
31
59
  end
60
+
61
+ it 'handles stringified numerics gracefully' do
62
+ is_expected.to receive(:acquire_lock).with(3.5, anything)
63
+
64
+ subject.limit(capacity: '3.5')
65
+ end
32
66
  end
33
67
 
34
68
  context 'with a cost parameter' do
@@ -51,6 +85,12 @@ describe Berater::Limiter do
51
85
  subject.limit(cost: Float::INFINITY)
52
86
  }.to raise_error(ArgumentError)
53
87
  end
88
+
89
+ it 'handles stringified numerics gracefully' do
90
+ is_expected.to receive(:acquire_lock).with(anything, 2.5)
91
+
92
+ subject.limit(cost: '2.5')
93
+ end
54
94
  end
55
95
 
56
96
  context 'when Berater.redis is nil' do
@@ -0,0 +1,196 @@
1
+ describe Berater::Middleware::FailOpen do
2
+ let(:limiter) { Berater::Unlimiter.new }
3
+ let(:lock) { limiter.limit }
4
+ let(:error) { Redis::TimeoutError }
5
+
6
+ describe '.call' do
7
+ let(:instance) { described_class.new(errors: errors, on_fail: on_fail) }
8
+ let(:errors) { nil }
9
+ let(:on_fail) { nil }
10
+
11
+ it 'returns the blocks value' do
12
+ expect(instance.call { lock }).to be lock
13
+ end
14
+
15
+ context 'when there is an error during lock acquisition' do
16
+ subject { instance.call { raise error } }
17
+
18
+ it 'still returns a lock' do
19
+ expect(subject).to be_a Berater::Lock
20
+ end
21
+
22
+ it 'creates a new, fake lock' do
23
+ expect(Berater::Lock).to receive(:new)
24
+ subject
25
+ end
26
+
27
+ it 'returns a lock that is releasable' do
28
+ expect(subject.release).to be true
29
+ end
30
+
31
+ context 'when an on_fail handler is defined' do
32
+ let(:on_fail) { double(Proc) }
33
+
34
+ it 'calls the handler' do
35
+ expect(on_fail).to receive(:call).with(error)
36
+ subject
37
+ end
38
+ end
39
+
40
+ context 'when the error is an IOError' do
41
+ let(:error) { IOError }
42
+
43
+ it 'would normally not catch the error' do
44
+ expect { subject }.to raise_error(error)
45
+ end
46
+
47
+ context 'and errors option is set' do
48
+ let(:errors) { [ error ] }
49
+
50
+ it 'catches the error' do
51
+ expect { subject }.not_to raise_error
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ context 'when there is an error during lock release' do
58
+ subject { instance.call { lock }.release }
59
+
60
+ before do
61
+ expect(lock).to receive(:release).and_raise(error)
62
+ end
63
+
64
+ it 'handles the exception' do
65
+ expect { subject }.not_to raise_error
66
+ end
67
+
68
+ it 'returns false since lock was not released' do
69
+ is_expected.to be false
70
+ end
71
+
72
+ context 'when an on_fail handler is defined' do
73
+ let(:on_fail) { double(Proc) }
74
+
75
+ it 'calls the handler' do
76
+ expect(on_fail).to receive(:call).with(Exception)
77
+ subject
78
+ end
79
+ end
80
+
81
+ context 'when the error is an IOError' do
82
+ let(:error) { IOError }
83
+
84
+ it 'would normally not catch the error' do
85
+ expect { subject }.to raise_error(error)
86
+ end
87
+
88
+ context 'and errors option is set' do
89
+ let(:errors) { [ error ] }
90
+
91
+ it 'catches the error' do
92
+ expect { subject }.not_to raise_error
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ context 'when there is no lock' do
99
+ it 'does not crash' do
100
+ instance.call {}
101
+ end
102
+ end
103
+
104
+ context 'when the lock is not a lock' do
105
+ it 'does not crash' do
106
+ instance.call { :foo }
107
+ end
108
+ end
109
+ end
110
+
111
+ context 'when there is an error during lock acquisition' do
112
+ before do
113
+ expect(limiter).to receive(:acquire_lock).and_raise(error)
114
+ end
115
+
116
+ it 'raises an exception for the caller' do
117
+ expect { limiter.limit }.to raise_error(error)
118
+ end
119
+
120
+ context 'when FailOpen middleware is enabled' do
121
+ before do
122
+ Berater.middleware.use described_class
123
+ end
124
+
125
+ it 'fails open' do
126
+ expect(limiter.limit).to be_a Berater::Lock
127
+ end
128
+
129
+ it 'returns the intended result' do
130
+ expect(limiter.limit { 123 }).to be 123
131
+ end
132
+ end
133
+
134
+ context 'when FailOpen middleware is enabled with callback' do
135
+ before do
136
+ Berater.middleware.use described_class, on_fail: on_fail
137
+ end
138
+ let(:on_fail) { double(Proc) }
139
+
140
+ it 'calls the callback' do
141
+ expect(on_fail).to receive(:call).with(Exception)
142
+ limiter.limit
143
+ end
144
+ end
145
+ end
146
+
147
+ context 'when there is an error during lock release' do
148
+ before do
149
+ allow(limiter).to receive(:acquire_lock).and_return(lock)
150
+ allow(lock).to receive(:release).and_raise(error)
151
+ end
152
+
153
+ it 'acquires a lock' do
154
+ expect(limiter.limit).to be_a Berater::Lock
155
+ expect(limiter.limit).to be lock
156
+ end
157
+
158
+ it 'raises an exception when lock is released' do
159
+ expect {
160
+ limiter.limit.release
161
+ }.to raise_error(error)
162
+ end
163
+
164
+ it 'raises an exception when lock is auto released' do
165
+ expect {
166
+ limiter.limit {}
167
+ }.to raise_error(error)
168
+ end
169
+
170
+ context 'when FailOpen middleware is enabled' do
171
+ before do
172
+ Berater.middleware.use described_class
173
+ end
174
+
175
+ it 'fails open' do
176
+ expect { limiter.limit.release }.not_to raise_error
177
+ end
178
+
179
+ it 'returns the intended result' do
180
+ expect(limiter.limit { 123 }).to be 123
181
+ end
182
+ end
183
+
184
+ context 'when FailOpen middleware is enabled with callback' do
185
+ before do
186
+ Berater.middleware.use described_class, on_fail: on_fail
187
+ end
188
+ let(:on_fail) { double(Proc) }
189
+
190
+ it 'calls the callback' do
191
+ expect(on_fail).to receive(:call).with(Exception)
192
+ limiter.limit {}
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,150 @@
1
+ describe Berater::Middleware::LoadShedder do
2
+ describe '#call' do
3
+ subject { described_class.new }
4
+
5
+ it 'yields' do
6
+ expect {|b| subject.call(&b) }.to yield_control
7
+ end
8
+
9
+ it 'passes through capacity and cost options' do
10
+ opts = {
11
+ capacity: 1,
12
+ cost: 2,
13
+ }
14
+
15
+ subject.call(**opts) do |**passed_opts|
16
+ expect(passed_opts).to eq(opts)
17
+ end
18
+ end
19
+
20
+ it 'strips out priority from options' do
21
+ opts = {
22
+ capacity: 1,
23
+ priority: 3,
24
+ }
25
+
26
+ subject.call(**opts) do |**passed_opts|
27
+ expect(passed_opts.keys).not_to include(:priority)
28
+ end
29
+ end
30
+
31
+ it 'keeps full capacity for priority 1' do
32
+ subject.call(capacity: 100, priority: 1) do |capacity:|
33
+ expect(capacity).to eq 100
34
+ end
35
+ end
36
+
37
+ it 'adjusts the capactiy according to priority' do
38
+ subject.call(capacity: 100, priority: 2) do |capacity:|
39
+ expect(capacity).to be < 100
40
+ end
41
+
42
+ subject.call(capacity: 100, priority: 5) do |capacity:|
43
+ expect(capacity).to eq 60
44
+ end
45
+ end
46
+
47
+ it 'works with a fractional priority' do
48
+ subject.call(capacity: 100, priority: 1.5) do |capacity:|
49
+ expect(capacity).to be < 100
50
+ end
51
+ end
52
+
53
+ context 'with a default priority' do
54
+ subject { described_class.new(default_priority: 5) }
55
+
56
+ it 'keeps full capacity for priority 1' do
57
+ subject.call(capacity: 100, priority: 1) do |capacity:|
58
+ expect(capacity).to eq 100
59
+ end
60
+ end
61
+
62
+ it 'uses the default priority' do
63
+ subject.call(capacity: 100) do |capacity:|
64
+ expect(capacity).to eq 60
65
+ end
66
+ end
67
+ end
68
+
69
+ context 'with a stringified priority' do
70
+ it 'casts the value' do
71
+ subject.call(capacity: 100, priority: '5') do |capacity:|
72
+ expect(capacity).to eq 60
73
+ end
74
+ end
75
+ end
76
+
77
+ context 'with a bogus priority value' do
78
+ it 'ignores the priority option' do
79
+ subject.call(capacity: 100, priority: nil) do |capacity:|
80
+ expect(capacity).to eq 100
81
+ end
82
+
83
+ subject.call(capacity: 100, priority: -1) do |capacity:|
84
+ expect(capacity).to eq 100
85
+ end
86
+
87
+ subject.call(capacity: 100, priority: 0) do |capacity:|
88
+ expect(capacity).to eq 100
89
+ end
90
+
91
+ subject.call(capacity: 100, priority: 50) do |capacity:|
92
+ expect(capacity).to eq 100
93
+ end
94
+
95
+ subject.call(capacity: 100, priority: 'abc') do |capacity:|
96
+ expect(capacity).to eq 100
97
+ end
98
+
99
+ subject.call(capacity: 100, priority: :abc) do |capacity:|
100
+ expect(capacity).to eq 100
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ context 'with a limiter' do
107
+ before do
108
+ Berater.middleware.use Berater::Middleware::LoadShedder
109
+ end
110
+
111
+ shared_examples 'limiter load shedding' do |limiter|
112
+ it 'passes through the capactiy properly' do
113
+ expect(limiter).to receive(:inner_limit).with(
114
+ hash_including(capacity: 100)
115
+ ).and_call_original
116
+
117
+ limiter.limit
118
+ end
119
+
120
+ it 'scales the capactiy with priority' do
121
+ expect(limiter).to receive(:inner_limit).with(
122
+ hash_including(capacity: 60)
123
+ ).and_call_original
124
+
125
+ limiter.limit(priority: 5)
126
+ end
127
+
128
+ it 'overloads properly' do
129
+ 60.times { limiter.limit(priority: 5) }
130
+
131
+ expect {
132
+ limiter.limit(priority: 5)
133
+ }.to be_overloaded
134
+
135
+ expect {
136
+ limiter.limit(priority: 4)
137
+ }.not_to be_overloaded
138
+
139
+ 39.times { limiter.limit(priority: 1) }
140
+
141
+ expect {
142
+ limiter.limit(priority: 1)
143
+ }.to be_overloaded
144
+ end
145
+ end
146
+
147
+ include_examples 'limiter load shedding', Berater::ConcurrencyLimiter.new(:key, 100)
148
+ include_examples 'limiter load shedding', Berater::RateLimiter.new(:key, 100, :second)
149
+ end
150
+ end
@@ -0,0 +1,232 @@
1
+ require 'datadog/statsd'
2
+
3
+ describe Berater::Middleware::Statsd do
4
+ let(:client) { double(Datadog::Statsd) }
5
+
6
+ before do
7
+ allow(client).to receive(:gauge)
8
+ allow(client).to receive(:increment)
9
+ allow(client).to receive(:timing)
10
+ end
11
+
12
+ describe '#call' do
13
+ subject { described_class.new(client, **client_opts).call(limiter, **opts, &block) }
14
+
15
+ let(:client_opts) { {} }
16
+ let(:limiter) { double(Berater::Limiter, key: :key, capacity: 5) }
17
+ let(:lock) { double(Berater::Lock, capacity: 4, contention: 2) }
18
+ let(:opts) { { capacity: lock.capacity, cost: 1 } }
19
+ let(:block) { lambda { lock } }
20
+
21
+ after { subject }
22
+
23
+ it 'returns a lock' do
24
+ expect(subject).to be lock
25
+ end
26
+
27
+ it 'tracks the call' do
28
+ expect(client).to receive(:timing).with(
29
+ 'berater.limiter.limit',
30
+ Float,
31
+ tags: {
32
+ key: limiter.key,
33
+ limiter: String,
34
+ },
35
+ )
36
+ end
37
+
38
+ it 'tracks limiter capacity' do
39
+ expect(client).to receive(:gauge).with(
40
+ 'berater.limiter.capacity',
41
+ limiter.capacity,
42
+ Hash
43
+ )
44
+ end
45
+
46
+ it 'tracks lock acquisition' do
47
+ expect(client).to receive(:increment).with(
48
+ 'berater.lock.acquired',
49
+ Hash
50
+ )
51
+ end
52
+
53
+ context 'when custom tags are passed in' do
54
+ let(:opts) { { tags: { abc: 123 } } }
55
+
56
+ it 'incorporates the tags' do
57
+ expect(client).to receive(:timing) do |*, tags:|
58
+ expect(tags).to include(opts[:tags])
59
+ end
60
+ end
61
+ end
62
+
63
+ context 'with global tags' do
64
+ let(:client_opts) { { tags: { abc: 123 } }}
65
+
66
+ it 'incorporates the tags' do
67
+ expect(client).to receive(:timing) do |*, tags:|
68
+ expect(tags).to include(client_opts[:tags])
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'with global tag callback' do
74
+ let(:client_opts) { { tags: callback }}
75
+ let(:callback) { double(Proc) }
76
+
77
+ it 'calls the callback' do
78
+ expect(callback).to receive(:call).with(limiter, **opts)
79
+ end
80
+
81
+ it 'incorporates the tags' do
82
+ expect(callback).to receive(:call).and_return({ abc: 123 })
83
+
84
+ expect(client).to receive(:timing) do |*, tags:|
85
+ expect(tags).to include(abc: 123)
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ context 'with a limiter' do
92
+ before do
93
+ Berater.middleware.use described_class, client
94
+ end
95
+
96
+ let(:limiter) { Berater::ConcurrencyLimiter.new(:key, 3) }
97
+
98
+ it 'tracks calls to limit' do
99
+ expect(client).to receive(:timing) do |*, tags:|
100
+ expect(tags[:limiter]).to eq 'ConcurrencyLimiter'
101
+ end
102
+
103
+ expect(client).to receive(:gauge).with(
104
+ 'berater.limiter.capacity',
105
+ limiter.capacity,
106
+ Hash,
107
+ )
108
+
109
+ expect(client).to receive(:gauge).with(
110
+ 'berater.limiter.contention',
111
+ 1,
112
+ Hash,
113
+ )
114
+
115
+ limiter.limit
116
+ end
117
+
118
+ it 'tracks each call' do
119
+ expect(client).to receive(:gauge).with(
120
+ 'berater.limiter.contention',
121
+ 1,
122
+ Hash,
123
+ )
124
+
125
+ expect(client).to receive(:gauge).with(
126
+ 'berater.limiter.contention',
127
+ 2,
128
+ Hash,
129
+ )
130
+
131
+ 2.times { limiter.limit }
132
+ end
133
+
134
+ context 'when an exception is raised' do
135
+ before do
136
+ expect(limiter).to receive(:redis).and_raise(error)
137
+ end
138
+
139
+ let(:error) { Redis::TimeoutError }
140
+
141
+ it 'tracks limiter exceptions' do
142
+ expect(client).to receive(:increment).with(
143
+ 'berater.limiter.error',
144
+ tags: hash_including(type: 'Redis_TimeoutError'),
145
+ )
146
+
147
+ expect { limiter.limit }.to raise_error(error)
148
+ end
149
+
150
+ context 'with FailOpen middleware inserted after' do
151
+ before do
152
+ Berater.middleware.use Berater::Middleware::FailOpen
153
+ end
154
+
155
+ it 'does not track the exception' do
156
+ expect(client).not_to receive(:increment).with(
157
+ 'berater.limiter.error',
158
+ )
159
+
160
+ limiter.limit
161
+ end
162
+
163
+ it 'does not track lock-based stats' do
164
+ expect(client).not_to receive(:gauge).with(
165
+ 'berater.lock.capacity',
166
+ )
167
+
168
+ expect(client).not_to receive(:gauge).with(
169
+ 'berater.limiter.contention',
170
+ )
171
+
172
+ limiter.limit
173
+ end
174
+ end
175
+
176
+ context 'with FailOpen middleware inserted before' do
177
+ before do
178
+ Berater.middleware.prepend Berater::Middleware::FailOpen
179
+ end
180
+
181
+ it 'tracks the exception' do
182
+ expect(client).to receive(:increment).with(
183
+ 'berater.limiter.error',
184
+ anything,
185
+ )
186
+
187
+ limiter.limit
188
+ end
189
+
190
+ it 'does not track lock-based stats' do
191
+ expect(client).not_to receive(:gauge).with(
192
+ 'berater.lock.capacity',
193
+ )
194
+
195
+ expect(client).not_to receive(:gauge).with(
196
+ 'berater.limiter.contention',
197
+ )
198
+
199
+ limiter.limit
200
+ end
201
+ end
202
+ end
203
+
204
+ context 'when the limiter is overloaded' do
205
+ before { limiter.capacity.times { limiter.limit } }
206
+
207
+ after do
208
+ expect { limiter.limit }.to be_overloaded
209
+ end
210
+
211
+ it 'tracks the overloaded count' do
212
+ expect(client).to receive(:increment).with(
213
+ 'berater.limiter.overloaded',
214
+ Hash
215
+ )
216
+ end
217
+
218
+ it 'tracks contention' do
219
+ expect(client).not_to receive(:gauge).with(
220
+ 'berater.limiter.contention',
221
+ limiter.capacity,
222
+ )
223
+ end
224
+
225
+ it 'does not track the exception' do
226
+ expect(client).not_to receive(:increment).with(
227
+ 'berater.limiter.error',
228
+ )
229
+ end
230
+ end
231
+ end
232
+ end
@@ -105,6 +105,16 @@ describe 'Berater.middleware' do
105
105
  expect(middleware).to receive(:call)
106
106
  expect(limiter.limit).to be nil
107
107
  end
108
+
109
+ it 'can intercept the lock' do
110
+ expect(middleware).to receive(:call) do |&block|
111
+ lock = block.call
112
+ expect(lock).to be_a Berater::Lock
113
+ expect(lock.capacity).to eq limiter.capacity
114
+ end
115
+
116
+ limiter.limit
117
+ end
108
118
  end
109
119
  end
110
120
  end
@@ -19,12 +19,13 @@ describe Berater::RateLimiter do
19
19
  describe '#capacity' do
20
20
  def expect_capacity(capacity)
21
21
  limiter = described_class.new(:key, capacity, :second)
22
- expect(limiter.capacity).to eq capacity
22
+ expect(limiter.capacity).to eq capacity.to_f
23
23
  end
24
24
 
25
25
  it { expect_capacity(0) }
26
26
  it { expect_capacity(1) }
27
27
  it { expect_capacity(1.5) }
28
+ it { expect_capacity('1.5') }
28
29
  it { expect_capacity(100) }
29
30
 
30
31
  context 'with erroneous values' do
@@ -35,7 +36,7 @@ describe Berater::RateLimiter do
35
36
  end
36
37
 
37
38
  it { expect_bad_capacity(-1) }
38
- it { expect_bad_capacity('1') }
39
+ it { expect_bad_capacity('abc') }
39
40
  it { expect_bad_capacity(:one) }
40
41
  it { expect_bad_capacity(Float::INFINITY) }
41
42
  end
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.10.1
4
+ version: 0.12.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: 2021-09-18 00:00:00.000000000 Z
11
+ date: 2021-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: meddleware
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dogstatsd-ruby
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '4.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '4.3'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rake
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,10 @@ files:
150
164
  - lib/berater/limiter_set.rb
151
165
  - lib/berater/lock.rb
152
166
  - lib/berater/lua_script.rb
167
+ - lib/berater/middleware.rb
168
+ - lib/berater/middleware/fail_open.rb
169
+ - lib/berater/middleware/load_shedder.rb
170
+ - lib/berater/middleware/statsd.rb
153
171
  - lib/berater/rate_limiter.rb
154
172
  - lib/berater/rspec.rb
155
173
  - lib/berater/rspec/matchers.rb
@@ -167,6 +185,9 @@ files:
167
185
  - spec/limiter_spec.rb
168
186
  - spec/lua_script_spec.rb
169
187
  - spec/matchers_spec.rb
188
+ - spec/middleware/fail_open_spec.rb
189
+ - spec/middleware/load_shedder_spec.rb
190
+ - spec/middleware/statsd_spec.rb
170
191
  - spec/middleware_spec.rb
171
192
  - spec/rate_limiter_spec.rb
172
193
  - spec/riddle_spec.rb
@@ -193,12 +214,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
214
  - !ruby/object:Gem::Version
194
215
  version: '0'
195
216
  requirements: []
196
- rubygems_version: 3.1.4
217
+ rubygems_version: 3.1.6
197
218
  signing_key:
198
219
  specification_version: 4
199
220
  summary: Berater
200
221
  test_files:
201
222
  - spec/rate_limiter_spec.rb
223
+ - spec/middleware/load_shedder_spec.rb
224
+ - spec/middleware/statsd_spec.rb
225
+ - spec/middleware/fail_open_spec.rb
202
226
  - spec/matchers_spec.rb
203
227
  - spec/dsl_refinement_spec.rb
204
228
  - spec/test_mode_spec.rb