Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse TCP connections #149

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Reuse TCP connections
  • Loading branch information
rohitpaulk committed Jan 10, 2018
commit 94179b8ae9b7686de462418b6a4f8c590d8902cf
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