Skip to content

Commit

Permalink
Reuse TCP connections
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Jan 10, 2018
1 parent 779cc77 commit 94179b8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/segment/analytics/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def send_request(write_key, batch)

[200, '{}']
else
# If `start` is not called, Ruby adds a 'Connection: close' header to
# all requests, preventing us from reusing a connection for multiple
# HTTP requests
@http.start unless @http.started?

response = @http.request(request, payload)
[response.code.to_i, response.body]
end
Expand Down
4 changes: 2 additions & 2 deletions lib/segment/analytics/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def initialize(queue, write_key, options = {})
@on_error = options[:on_error] || proc { |status, error| }
@batch = []
@lock = Mutex.new
@request = Request.new
end

# public: Continuously runs the loop to check for new events
Expand All @@ -42,8 +43,7 @@ def run
end
end

res = Request.new.post @write_key, @batch

res = @request.post(@write_key, @batch)
@on_error.call(res.status, res.error) unless res.status == 200

@lock.synchronize { @batch.clear }
Expand Down

0 comments on commit 94179b8

Please # to comment.