about summary refs log tree commit homepage
path: root/lib/unicorn/socket_helper.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-10-25 19:27:05 +0000
committerEric Wong <normalperson@yhbt.net>2013-10-25 20:00:37 +0000
commit7c125886b5862bf20711bae22e6697ad46141434 (patch)
tree17e08b48ff591ab43a369067a3c23c6e0dbcb3c3 /lib/unicorn/socket_helper.rb
parent1dc099228ee0f59c13385a3e7346a2cb37d85153 (diff)
downloadunicorn-7c125886b5862bf20711bae22e6697ad46141434.tar.gz
This allows users to start an independent instance of unicorn on
a the same port as a running unicorn (as long as both instances
use :reuseport).

ref: https://lwn.net/Articles/542629/
Diffstat (limited to 'lib/unicorn/socket_helper.rb')
-rw-r--r--lib/unicorn/socket_helper.rb30
1 files changed, 22 insertions, 8 deletions
diff --git a/lib/unicorn/socket_helper.rb b/lib/unicorn/socket_helper.rb
index 18b0be7..2701d58 100644
--- a/lib/unicorn/socket_helper.rb
+++ b/lib/unicorn/socket_helper.rb
@@ -41,6 +41,15 @@ module Unicorn
 
       # do not send out partial frames (Linux)
       TCP_CORK = 3 unless defined?(TCP_CORK)
+
+      # Linux got SO_REUSEPORT in 3.9, BSDs have had it for ages
+      unless defined?(SO_REUSEPORT)
+        if RUBY_PLATFORM =~ /(?:alpha|mips|parisc|sparc)/
+          SO_REUSEPORT = 0x0200 # untested
+        else
+          SO_REUSEPORT = 15 # only tested on x86_64 and i686
+        end
+      end
     when /freebsd/
       # do not send out partial frames (FreeBSD)
       TCP_NOPUSH = 4 unless defined?(TCP_NOPUSH)
@@ -142,9 +151,9 @@ module Unicorn
           File.umask(old_umask)
         end
       elsif /\A\[([a-fA-F0-9:]+)\]:(\d+)\z/ =~ address
-        new_ipv6_server($1, $2.to_i, opt)
+        new_tcp_server($1, $2.to_i, opt.merge(:ipv6=>true))
       elsif /\A(\d+\.\d+\.\d+\.\d+):(\d+)\z/ =~ address
-        Kgio::TCPServer.new($1, $2.to_i)
+        new_tcp_server($1, $2.to_i, opt)
       else
         raise ArgumentError, "Don't know how to bind: #{address}"
       end
@@ -152,13 +161,18 @@ module Unicorn
       sock
     end
 
-    def new_ipv6_server(addr, port, opt)
-      opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port)
-      defined?(IPV6_V6ONLY) or
-        abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS"
-      sock = Socket.new(AF_INET6, SOCK_STREAM, 0)
-      sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0)
+    def new_tcp_server(addr, port, opt)
+      # n.b. we set FD_CLOEXEC in the workers
+      sock = Socket.new(opt[:ipv6] ? AF_INET6 : AF_INET, SOCK_STREAM, 0)
+      if opt.key?(:ipv6only)
+        defined?(IPV6_V6ONLY) or
+          abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS"
+        sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0)
+      end
       sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
+      if defined?(SO_REUSEPORT) && opt[:reuseport]
+        sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
+      end
       sock.bind(Socket.pack_sockaddr_in(port, addr))
       IO_PURGATORY << sock
       Kgio::TCPServer.for_fd(sock.fileno)