about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-07-03 08:50:25 +0000
committerEric Wong <e@80x24.org>2015-07-03 10:05:11 +0000
commit602784e227c3d792c492ffd45df81182eb0fa9dc (patch)
tree739e113a8e60d8a91ac2c2f744cadf8ced280d19
parentd3f28e0fa44318f9af4409fc9b865d5094ccc1ee (diff)
downloadyahns-602784e227c3d792c492ffd45df81182eb0fa9dc.tar.gz
TCP socket options are now set when inheriting existing sockets from
a parent process.  I'm fairly certain all the TCP setsockopt knobs
we use are idempotent and harmless to change.

If anything, the only directive I'd be uncomfortable changing is
shortening the listen(2) (aka :backlog) size, but we've always
changed that anyways since it also applies to UNIX sockets.

Note: removing a configuration knob in a yahns config file can not
reset the value to the OS-provided default setting.  Inherited
sockets must use a new setting to override existing ones.
(or the socket needs to be closed and re-created in the process
 launcher before yahns inherits it).

Based on unicorn commit 1db9a8243d42cc86d5ca4901bceb305061d0d212

Noticed-by: Christos Trochalakis <yatiohi@ideopolis.gr>
  <20150626114129.GA25883@luke.ws.skroutz.gr>
-rw-r--r--lib/yahns/server.rb3
-rw-r--r--test/test_server.rb20
2 files changed, 22 insertions, 1 deletions
diff --git a/lib/yahns/server.rb b/lib/yahns/server.rb
index 3b9addc..b6663e1 100644
--- a/lib/yahns/server.rb
+++ b/lib/yahns/server.rb
@@ -315,9 +315,10 @@ class Yahns::Server # :nodoc:
     inherited = ENV['YAHNS_FD'].to_s.split(',').map! do |fd|
       io = Socket.for_fd(fd.to_i)
       opts = sock_opts(io)
+      io = server_cast(io, opts)
       set_server_sockopt(io, opts)
       @logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
-      server_cast(io, opts)
+      io
     end
 
     @listeners.replace(inherited)
diff --git a/test/test_server.rb b/test/test_server.rb
index 0410f65..50e739a 100644
--- a/test/test_server.rb
+++ b/test/test_server.rb
@@ -845,4 +845,24 @@ class TestServer < Testcase
   ensure
     quit_wait(pid)
   end
+
+  def test_inherit_tcp_nodelay_set
+    err = @err
+    cfg = Yahns::Config.new
+    host, port = @srv.addr[3], @srv.addr[1]
+    @srv.setsockopt(:IPPROTO_TCP, :TCP_NODELAY, 0)
+    assert_equal 0, @srv.getsockopt(:IPPROTO_TCP, :TCP_NODELAY).int
+    cfg.instance_eval do
+      ru = lambda { |_| [ 200, { 'Content-Length' => '2' } , [ 'HI' ] ] }
+      GTL.synchronize { app(:rack, ru) { listen "#{host}:#{port}" } }
+      logger(Logger.new(err.path))
+    end
+    pid = mkserver(cfg, @srv) { ENV["YAHNS_FD"] = "#{@srv.fileno}" }
+    run_client(host, port) { |res| assert_equal "HI", res.body }
+
+    # TCP socket option is shared at file level, not FD level:
+    assert_equal 1, @srv.getsockopt(:IPPROTO_TCP, :TCP_NODELAY).int
+  ensure
+    quit_wait(pid)
+  end
 end