Skip to content

Commit

Permalink
Send headers and body in one write if possible
Browse files Browse the repository at this point in the history
This change tries to avoid a pathological case with Nagle's algorithm where two
small writes that would fit in a single TCP packet are performed in a row
(headers and body).

Instead, it tries to perform a single write if possible.
  • Loading branch information
tarcieri committed Dec 19, 2015
1 parent bd12470 commit 34cbf44
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/http/request/writer.rb
Expand Up @@ -36,8 +36,9 @@ def add_headers

# Stream the request to a socket
def stream
send_request_header
send_request_body
add_headers
add_body_type_headers
send_request
end

# Send headers needed to connect through proxy
Expand All @@ -64,23 +65,24 @@ def join_headers
@request_header.join(CRLF) + (CRLF) * 2
end

def send_request_header
add_headers
add_body_type_headers
def send_request
headers = join_headers

write(join_headers)
end
case @body
when NilClass
write(headers)
when String
write(headers << @body)
when Enumerable
write(headers)

def send_request_body
if @body.is_a?(String)
write(@body)
elsif @body.is_a?(Enumerable)
@body.each do |chunk|
write(chunk.bytesize.to_s(16) << CRLF)
write(chunk << CRLF)
end

write(CHUNKED_END)
else fail TypeError, "invalid body type: #{@body.class}"
end
end

Expand Down

0 comments on commit 34cbf44

Please sign in to comment.