yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH] apply TCP socket options on inherited sockets
Date: Wed, 15 Jul 2015 18:50:20 +0000	[thread overview]
Message-ID: <1436986220-775-1-git-send-email-e@80x24.org> (raw)

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>
---
 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	other threads:[~2015-07-15 18:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/yahns/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1436986220-775-1-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=yahns-public@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/yahns.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).