pebbles-river 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pebbles/river/rate_limiter.rb +33 -0
- data/lib/pebbles/river/version.rb +1 -1
- data/lib/pebbles/river/worker.rb +32 -28
- data/lib/pebbles/river.rb +1 -0
- data/spec/lib/worker_spec.rb +19 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5154135c4634b945ecb355540b84513c94adcb90
|
4
|
+
data.tar.gz: b589add0f5307e3ca0611fd062c473ea4ca77243
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b178f1ec13fb5916f718a3e77df7e5fe983dd40fc40bfa971e35fae2166fed6ff48e2aaf2ba5c40d4bf5177335b68182c727360565113081fd6dcc572e325152
|
7
|
+
data.tar.gz: d25e51b1f21784e80d10b8d8881fd08f4237ef6b1c0fc7c53dfe68fda0ea857a41eecb97ac339cdb1747de408e60ca1c80029cf6883d7a2d8144606d662a6bfe
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Pebbles
|
2
|
+
module River
|
3
|
+
|
4
|
+
# Simple queueless token-bucket limiter.
|
5
|
+
class RateLimiter
|
6
|
+
|
7
|
+
def initialize(max_rate, window_seconds)
|
8
|
+
@last_check = Time.now
|
9
|
+
@max_rate = @allowance = max_rate
|
10
|
+
@window_seconds = window_seconds
|
11
|
+
end
|
12
|
+
|
13
|
+
def increment
|
14
|
+
now = Time.now
|
15
|
+
time_passed = now - @last_check
|
16
|
+
@last_check = now
|
17
|
+
|
18
|
+
@allowance += time_passed * (@max_rate / @window_seconds)
|
19
|
+
if @allowance > @max_rate
|
20
|
+
@allowance = @max_rate
|
21
|
+
end
|
22
|
+
if @allowance < 1
|
23
|
+
sleep((1 - @allowance) * (@window_seconds / @max_rate))
|
24
|
+
@allowance = 0
|
25
|
+
else
|
26
|
+
@allowance -= 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/pebbles/river/worker.rb
CHANGED
@@ -49,6 +49,7 @@ module Pebbles
|
|
49
49
|
@handler = handler
|
50
50
|
@river = River.new
|
51
51
|
@next_event_time = Time.now
|
52
|
+
@rate_limiter = RateLimiter.new(1.0, 10)
|
52
53
|
end
|
53
54
|
|
54
55
|
# Runs the handler once.
|
@@ -114,14 +115,12 @@ module Pebbles
|
|
114
115
|
|
115
116
|
def process_next
|
116
117
|
with_exceptions do
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
return false
|
124
|
-
end
|
118
|
+
queue.pop(auto_ack: false, ack: true) do |raw_message|
|
119
|
+
if raw_message[:payload] != :queue_empty
|
120
|
+
process_message(raw_message)
|
121
|
+
return true
|
122
|
+
else
|
123
|
+
return false
|
125
124
|
end
|
126
125
|
end
|
127
126
|
end
|
@@ -132,7 +131,7 @@ module Pebbles
|
|
132
131
|
message = Message.new(raw_message, queue)
|
133
132
|
rescue => exception
|
134
133
|
ignore_exceptions do
|
135
|
-
queue.nack(delivery_tag:
|
134
|
+
queue.nack(delivery_tag: raw_message[:delivery_details][:delivery_tag])
|
136
135
|
end
|
137
136
|
raise exception
|
138
137
|
else
|
@@ -148,34 +147,39 @@ module Pebbles
|
|
148
147
|
end
|
149
148
|
raise exception
|
150
149
|
else
|
151
|
-
if
|
152
|
-
|
150
|
+
if @managed_acking
|
151
|
+
case result
|
152
|
+
when false
|
153
|
+
message.nack
|
154
|
+
else
|
155
|
+
message.ack
|
156
|
+
end
|
153
157
|
end
|
154
158
|
end
|
155
159
|
end
|
156
160
|
end
|
157
161
|
|
158
|
-
def with_connection_error_handling(&block)
|
159
|
-
yield
|
160
|
-
rescue *CONNECTION_EXCEPTIONS => exception
|
161
|
-
if @queue
|
162
|
-
ignore_exceptions do
|
163
|
-
@queue.close
|
164
|
-
end
|
165
|
-
@queue = nil
|
166
|
-
end
|
167
|
-
|
168
|
-
@river.disconnect
|
169
|
-
|
170
|
-
ignore_exceptions do
|
171
|
-
@on_connection_error.call(exception)
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
162
|
def with_exceptions(&block)
|
176
163
|
begin
|
177
164
|
yield
|
165
|
+
rescue *CONNECTION_EXCEPTIONS => exception
|
166
|
+
@rate_limiter.increment
|
167
|
+
|
168
|
+
if @queue
|
169
|
+
ignore_exceptions do
|
170
|
+
@queue.close
|
171
|
+
end
|
172
|
+
@queue = nil
|
173
|
+
end
|
174
|
+
|
175
|
+
@river.disconnect
|
176
|
+
|
177
|
+
ignore_exceptions do
|
178
|
+
@on_connection_error.call(exception)
|
179
|
+
end
|
178
180
|
rescue => exception
|
181
|
+
@rate_limiter.increment
|
182
|
+
|
179
183
|
ignore_exceptions do
|
180
184
|
@on_exception.call(exception)
|
181
185
|
end
|
data/lib/pebbles/river.rb
CHANGED
data/spec/lib/worker_spec.rb
CHANGED
@@ -18,6 +18,12 @@ describe Worker do
|
|
18
18
|
handler
|
19
19
|
end
|
20
20
|
|
21
|
+
let :false_handler do
|
22
|
+
handler = double('null_handler')
|
23
|
+
handler.stub(:call) { false }
|
24
|
+
handler
|
25
|
+
end
|
26
|
+
|
21
27
|
let :io_error do
|
22
28
|
IOError.new("This is not the exception you are looking for")
|
23
29
|
end
|
@@ -145,6 +151,19 @@ describe Worker do
|
|
145
151
|
end
|
146
152
|
end
|
147
153
|
|
154
|
+
context 'when handler returns false' do
|
155
|
+
it 'nacks the message' do
|
156
|
+
expect(queue).to receive(:nack).at_least(1).times
|
157
|
+
expect(queue).to_not receive(:ack)
|
158
|
+
expect(queue).to_not receive(:close)
|
159
|
+
|
160
|
+
expect(river).to receive(:connected?).with(no_args).at_least(1).times
|
161
|
+
expect(river).to_not receive(:connect)
|
162
|
+
|
163
|
+
subject.new(false_handler, queue: {name: 'foo'}).run_once
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
148
167
|
context 'when handler throws exception' do
|
149
168
|
|
150
169
|
let :on_exception_callback do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pebbles-river
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Staubo
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-05-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pebblebed
|
@@ -139,6 +139,7 @@ files:
|
|
139
139
|
- lib/pebbles/river/compatibility.rb
|
140
140
|
- lib/pebbles/river/errors.rb
|
141
141
|
- lib/pebbles/river/message.rb
|
142
|
+
- lib/pebbles/river/rate_limiter.rb
|
142
143
|
- lib/pebbles/river/river.rb
|
143
144
|
- lib/pebbles/river/routing.rb
|
144
145
|
- lib/pebbles/river/subscription.rb
|