Permalink
Browse files

Utilitize TCP_CORK on linux

Inform the TCP stack via TCP_CORK where a HTTP response begins and ends.
This keeps keepalive fast and allows it to fill out TCP packets to be as
big as possible.
  • Loading branch information...
1 parent 281c20e commit 6da831aec3eb4bd237008a30a539e0885475dc8a @evanphx evanphx committed Dec 1, 2011
Showing with 23 additions and 0 deletions.
  1. +23 −0 lib/puma/server.rb
View
@@ -61,6 +61,25 @@ def initialize(app, events=Events::DEFAULT)
}
end
+ if RUBY_PLATFORM =~ /linux/
+ # 6 == Socket::IPPROTO_TCP
+ # 3 == TCP_CORK
+ # 1/0 == turn on/off
+ def cork_socket(socket)
+ socket.setsockopt(6, 3, 1)
+ end
+
+ def uncork_socket(socket)
+ socket.setsockopt(6, 3, 0)
+ end
+ else
+ def cork_socket(socket)
+ end
+
+ def uncork_socket(socket)
+ end
+ end
+
def add_tcp_listener(host, port, optimize_for_latency=true)
s = TCPServer.new(host, port)
if optimize_for_latency
@@ -273,6 +292,8 @@ def handle_request(env, client, body, cl)
content_length = res_body[0].size
end
+ cork_socket client
+
client.write http_version
client.write status.to_s
client.write " "
@@ -336,6 +357,8 @@ def handle_request(env, client, body, cl)
end
ensure
+ uncork_socket client
+
body.close
res_body.close if res_body.respond_to? :close

0 comments on commit 6da831a

Please sign in to comment.