summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-06-30 19:16:43 -0700
committerEric Wong <normalperson@yhbt.net>2009-07-08 14:49:52 -0700
commitf63f79f24ea3c6419b2664cbda5f2cbb41225bbe (patch)
tree6cb7ec609a27dfbdaed1b527603b9fe13422f923
parent98e77adbdcdd96d24c98fd098d9a046f152d6535 (diff)
Now that we support tunnelling arbitrary protocols over HTTP as
well as "100 Continue" responses, TCP_NODELAY actually becomes
useful to us.  TCP_NODELAY is actually reasonably portable
nowadays; even.

While we're adding non-portable options, TCP_CORK/TCP_NOPUSH can
be enabled, too.  Unlike some other servers, these can't be
disabled explicitly/intelligently to force a flush, however.
However, these may still improve performance with "normal" HTTP
applications (Mongrel has always had TCP_CORK enabled in Linux).

While we're adding OS-specific features, we might as well
support TCP_DEFER_ACCEPT in Linux and FreeBSD the "httpready"
accept filter to prevent abuse.

These options can all be enabled on a per-listener basis.
(cherry picked from commit 563d03f649ef31d2aec3505cbbed1e015493b8fc)
-rw-r--r--lib/unicorn/configurator.rb24
-rw-r--r--lib/unicorn/socket_helper.rb50
2 files changed, 72 insertions, 2 deletions
diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb
index a432f64..860962a 100644
--- a/lib/unicorn/configurator.rb
+++ b/lib/unicorn/configurator.rb
@@ -207,7 +207,24 @@ module Unicorn
     # specified.
     #
     # Defaults: operating system defaults
-    def listen(address, opt = { :backlog => 1024 })
+    #
+    # +tcp_nodelay+: disables Nagle's algorithm on TCP sockets
+    #
+    # This has no effect on UNIX sockets.
+    #
+    # Default: operating system defaults (usually Nagle's algorithm enabled)
+    #
+    # +tcp_nopush+: enables TCP_CORK in Linux or TCP_NOPUSH in FreeBSD
+    #
+    # This will prevent partial TCP frames from being sent out.
+    # Enabling +tcp_nopush+ is generally not needed or recommended as
+    # controlling +tcp_nodelay+ already provides sufficient latency
+    # reduction whereas Unicorn does not know when the best times are
+    # for flushing corked sockets.
+    #
+    # This has no effect on UNIX sockets.
+    #
+    def listen(address, opt = {})
       address = expand_addr(address)
       if String === address
         Hash === @set[:listener_opts] or
@@ -217,6 +234,11 @@ module Unicorn
           Integer === value or
             raise ArgumentError, "not an integer: #{key}=#{value.inspect}"
         end
+        [ :tcp_nodelay, :tcp_nopush ].each do |key|
+          (value = opt[key]).nil? and next
+          TrueClass === value || FalseClass === value or
+            raise ArgumentError, "not boolean: #{key}=#{value.inspect}"
+        end
         @set[:listener_opts][address].merge!(opt)
       end
 
diff --git a/lib/unicorn/socket_helper.rb b/lib/unicorn/socket_helper.rb
index 850ad85..f8e3725 100644
--- a/lib/unicorn/socket_helper.rb
+++ b/lib/unicorn/socket_helper.rb
@@ -4,8 +4,56 @@ module Unicorn
   module SocketHelper
     include Socket::Constants
 
+    # configure platform-specific options (only tested on Linux 2.6 so far)
+    case RUBY_PLATFORM
+    when /linux/
+      # from /usr/include/linux/tcp.h
+      TCP_DEFER_ACCEPT = 9 unless defined?(TCP_DEFER_ACCEPT)
+      TCP_CORK = 3 unless defined?(TCP_CORK)
+    when /freebsd(([1-4]\..{1,2})|5\.[0-4])/
+      # Do nothing for httpready, just closing a bug when freebsd <= 5.4
+      TCP_NOPUSH = 4 unless defined?(TCP_NOPUSH)
+    when /freebsd/
+      TCP_NOPUSH = 4 unless defined?(TCP_NOPUSH)
+      # Use the HTTP accept filter if available.
+      # The struct made by pack() is defined in /usr/include/sys/socket.h
+      # as accept_filter_arg
+      # We won't be seupportin the "dataready" filter unlike nginx
+      # since we only support HTTP and no other protocols
+      unless `/sbin/sysctl -nq net.inet.accf.http`.empty?
+        HTTPREADY = ['httpready', nil].pack('a16a240').freeze
+      end
+    end
+
+    def set_tcp_sockopt(sock, opt)
+
+      # highly portable, but off by default because we don't do keepalive
+      if defined?(TCP_NODELAY) && ! (val = opt[:tcp_nodelay]).nil?
+        sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, val ? 1 : 0) rescue nil
+      end
+
+      unless (val = opt[:tcp_nopush]).nil?
+        val = val ? 1 : 0
+        if defined?(TCP_CORK) # Linux
+          sock.setsockopt(IPPROTO_TCP, TCP_CORK, val) rescue nil
+        elsif defined?(TCP_NOPUSH) # TCP_NOPUSH is untested (FreeBSD)
+          sock.setsockopt(IPPROTO_TCP, TCP_NOPUSH, val) rescue nil
+        end
+      end
+
+      # No good reason to ever have deferred accepts off
+      if defined?(TCP_DEFER_ACCEPT)
+        sock.setsockopt(SOL_TCP, TCP_DEFER_ACCEPT, 1) rescue nil
+      elsif defined?(SO_ACCEPTFILTER) && defined?(HTTPREADY)
+        sock.setsockopt(SOL_SOCKET, SO_ACCEPTFILTER, HTTPREADY) rescue nil
+      end
+    end
+
     def set_server_sockopt(sock, opt)
       opt ||= {}
+
+      TCPSocket === sock and set_tcp_sockopt(sock, opt)
+
       if opt[:rcvbuf] || opt[:sndbuf]
         log_buffer_sizes(sock, "before: ")
         sock.setsockopt(SOL_SOCKET, SO_RCVBUF, opt[:rcvbuf]) if opt[:rcvbuf]
@@ -25,7 +73,7 @@ module Unicorn
     # creates a new server, socket. address may be a HOST:PORT or
     # an absolute path to a UNIX socket.  address can even be a Socket
     # object in which case it is immediately returned
-    def bind_listen(address = '0.0.0.0:8080', opt = { :backlog => 1024 })
+    def bind_listen(address = '0.0.0.0:8080', opt = {})
       return address unless String === address
 
       sock = if address[0..0] == "/"