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 <yati...@ideopolis.gr>
  <20150626114129.ga25...@luke.ws.skroutz.gr>
---
 lib/yahns/server.rb |  3 ++-
 test/test_server.rb | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

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
-- 
EW


Reply via email to