Skip to content

Commit ffbe680

Browse files
committed
Merge branch 'master' into limit-batch-size
2 parents c76f94a + 204d5a2 commit ffbe680

6 files changed

Lines changed: 28 additions & 13 deletions

File tree

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Metrics/AbcSize:
1818
# Offense count: 1
1919
# Configuration parameters: CountComments.
2020
Metrics/ClassLength:
21-
Max: 223
21+
Max: 229
2222

2323
# Offense count: 1
2424
Metrics/CyclomaticComplexity:

lib/segment/analytics/client.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
require 'thread'
22
require 'time'
3+
4+
require 'segment/analytics/defaults'
5+
require 'segment/analytics/logging'
36
require 'segment/analytics/utils'
47
require 'segment/analytics/worker'
5-
require 'segment/analytics/defaults'
68

79
module Segment
810
class Analytics
911
class Client
1012
include Segment::Analytics::Utils
13+
include Segment::Analytics::Logging
1114

1215
# public: Creates a new client
1316
#
@@ -314,7 +317,12 @@ def enqueue(action)
314317

315318
true
316319
else
317-
false # Queue is full
320+
logger.warn(
321+
'Queue is full, dropping events. The :max_queue_size ' \
322+
'configuration parameter can be increased to prevent this from ' \
323+
'happening.'
324+
)
325+
false
318326
end
319327
end
320328

lib/segment/analytics/request.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ def send_request(write_key, batch)
112112

113113
[200, '{}']
114114
else
115+
# If `start` is not called, Ruby adds a 'Connection: close' header to
116+
# all requests, preventing us from reusing a connection for multiple
117+
# HTTP requests
118+
@http.start unless @http.started?
119+
115120
response = @http.request(request, payload)
116121
[response.code.to_i, response.body]
117122
end

lib/segment/analytics/worker.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def initialize(queue, write_key, options = {})
2929
batch_size = options[:batch_size] || Defaults::MessageBatch::MAX_SIZE
3030
@batch = MessageBatch.new(batch_size)
3131
@lock = Mutex.new
32+
@request = Request.new
3233
end
3334

3435
# public: Continuously runs the loop to check for new events
@@ -43,7 +44,7 @@ def run
4344
end
4445
end
4546

46-
res = Request.new.post @write_key, @batch
47+
res = @request.post(@write_key, @batch)
4748
@on_error.call(res.status, res.error) unless res.status == 200
4849

4950
@lock.synchronize { @batch.clear }

spec/segment/analytics/client_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ class Analytics
275275
end
276276
end
277277

278+
it 'returns false if queue is full' do
279+
client.instance_variable_set(:@max_queue_size, 1)
280+
281+
[:track, :screen, :page, :group, :identify, :alias].each do |s|
282+
expect(client.send(s, data)).to eq(true)
283+
expect(client.send(s, data)).to eq(false) # Queue is full
284+
queue.pop(true)
285+
end
286+
end
287+
278288
it 'converts message id to string' do
279289
[:track, :screen, :page, :group, :identify, :alias].each do |s|
280290
client.send(s, data)

spec/segment/analytics/message_batch_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ class Analytics
1919
subject << message
2020
expect(subject.length).to eq(0)
2121
end
22-
23-
it 'rejects messages that exceed the maximum allowed size' do
24-
max_bytes = Defaults::Message::MAX_BYTES
25-
hash = { 'a' => 'b' * max_bytes }
26-
message = Message.new(hash)
27-
28-
subject << message
29-
expect(subject.length).to eq(0)
30-
end
3122
end
3223

3324
describe '#full?' do

0 commit comments

Comments
 (0)