berater 0.14.0 → 0.15.0

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: 3780da104c79084b2c3b348363a5f763c42d9d936dc218e68df90cdbbc223cfe
4
- data.tar.gz: a3b7e7af7b3a764f97c013cb642eccf6d71ad825df459d32722a4e312ca2cdd8
3
+ metadata.gz: 81f3c362a1360d2b3f3b4a2829b374dd38f0f9de6809737efe7d9bb44ffbd88c
4
+ data.tar.gz: cbad555e49e5ac86d63f99486139e6df13b9da4289832b2d8cf5034bd7140018
5
5
  SHA512:
6
- metadata.gz: 364a70d42009b4c80b7235cbbc736f8c074f309abfa529f494f2fd01959e9b5ffb2bbd08ad3a39b12b0565abb8435bf86cd59b774055c4fbd845ba7d2f0c798b
7
- data.tar.gz: 92271042126ccc8b70a3a95f6fa530a8f36dceddaf7cc8de3ef9eb1ac861fcd9f7d10e066d27ac4d7bb807d5f43741f78aaf4b9b43a970bc159a5b2a3142f73a
6
+ metadata.gz: ed26b29a67a30db075ec18ab64698b6ae76dbe7d4d8aee64c290dc449a6867566ea3c8155eb6ec6c5fe440208c19345bfc02c30adcddb4356b591f784678df99
7
+ data.tar.gz: 56143daeb1069e5d1162bea25c9892f146009168f8ef42c23cdc78774e6148602ef40364423c94e5248d2cff99793e6e5ef6a676e918ec4cf23d2686a35b5927
@@ -62,12 +62,12 @@ module Berater
62
62
  lock = limit(cost: 0)
63
63
 
64
64
  if lock.capacity == 0
65
- 1.0
65
+ 100.0
66
66
  else
67
- lock.contention.to_f / lock.capacity
67
+ lock.contention.to_f / lock.capacity * 100
68
68
  end
69
69
  rescue Berater::Overloaded
70
- 1.0
70
+ 100.0
71
71
  end
72
72
 
73
73
  def ==(other)
@@ -124,12 +124,7 @@ module Berater
124
124
  # can only call via subclass
125
125
  raise NoMethodError if self == Berater::Limiter
126
126
 
127
- if RUBY_VERSION < '3' && kwargs.empty?
128
- # avoid ruby 2 problems with empty hashes
129
- super(*args)
130
- else
131
- super
132
- end
127
+ super
133
128
  end
134
129
 
135
130
  def cache_key(key)
@@ -6,7 +6,9 @@ module Berater
6
6
  end
7
7
 
8
8
  def call(limiter, **)
9
- tracer&.trace('Berater') do |span|
9
+ return yield unless tracer
10
+
11
+ tracer.trace('Berater') do |span|
10
12
  begin
11
13
  lock = yield
12
14
  rescue Exception => error
@@ -32,7 +34,7 @@ module Berater
32
34
  private
33
35
 
34
36
  def tracer
35
- @tracer || (defined?(Datadog::Tracing) && Datadog::Tracing) || (defined?(Datadog) && Datadog.tracer)
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 >= 1
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 >= 1
26
+ @limiter.utilization >= 100
27
27
  end
28
28
  rescue Berater::Overloaded
29
29
  true
@@ -1,3 +1,3 @@
1
1
  module Berater
2
- VERSION = "0.14.0"
2
+ VERSION = "0.15.0"
3
3
  end
@@ -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 be 0.0
258
+ expect(limiter.utilization).to eq 0
259
259
 
260
260
  2.times { limiter.limit }
261
- expect(limiter.utilization).to be 0.2
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 be 1.0
266
+ expect(limiter.utilization).to eq 100
267
267
 
268
268
  Timecop.freeze(15)
269
269
 
270
- expect(limiter.utilization).to be 0.8
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
@@ -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 be 0.0
240
+ expect(limiter.utilization).to eq 0
241
241
 
242
242
  2.times { limiter.limit }
243
- expect(limiter.utilization).to be 0.2
243
+ expect(limiter.utilization).to eq 20
244
244
 
245
245
  8.times { limiter.limit }
246
- expect(limiter.utilization).to be 1.0
246
+ expect(limiter.utilization).to eq 100
247
247
 
248
248
  Timecop.freeze(30)
249
249
 
250
- expect(limiter.utilization).to be 0.5
250
+ expect(limiter.utilization).to eq 50
251
251
  end
252
252
  end
253
253
 
@@ -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 be 0.0
63
+ expect(limiter.utilization).to eq 0
64
64
 
65
65
  2.times { limiter.limit }
66
- expect(limiter.utilization).to be 0.2
66
+ expect(limiter.utilization).to eq 20
67
67
 
68
68
  8.times { limiter.limit }
69
- expect(limiter.utilization).to be 1.0
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.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-11 00:00:00.000000000 Z
11
+ date: 2023-10-13 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: '0'
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: '0'
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,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
225
197
  requirements:
226
198
  - - ">="
227
199
  - !ruby/object:Gem::Version
228
- version: '0'
200
+ version: '3'
229
201
  required_rubygems_version: !ruby/object:Gem::Requirement
230
202
  requirements:
231
203
  - - ">="