unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [ANN] unicorn 5.0.0.pre2 - another prerelease!
  @ 2015-07-06 21:41  6% ` Eric Wong
  0 siblings, 0 replies; 4+ results
From: Eric Wong @ 2015-07-06 21:41 UTC (permalink / raw)
  To: unicorn-public

There is a minor TCP socket options are now applied to inherited
sockets, and we have native support for inheriting sockets from
systemd (by emulating the sd_listen_fds(3) function).

Dynamic changes in the application to Rack::Utils::HTTP_STATUS
codes is now supported, so you can use your own custom status
lines.

Ruby 2.2 and later is now favored for performance.
Optimizations by using constants which made sense in earlier
versions of Ruby are gone: so users of old Ruby versions
will see performance regressions.  Ruby 2.2 users should
see the same or better performance, and we have less code
as a result.

* doc: update some invalid URLs
* apply TCP socket options on inherited sockets
* reflect changes in Rack::Utils::HTTP_STATUS_CODES
* reduce constants and optimize for Ruby 2.2
* http_response: reduce size of multi-line header path
* emulate sd_listen_fds for systemd support
* test/unit/test_response.rb: compatibility with older test-unit

This also includes all changes in unicorn 5.0.0.pre1:

http://bogomips.org/unicorn-public/m/20150615225652.GA16164@dcvr.yhbt.net.html

-- 
EW

^ permalink raw reply	[relevance 6%]

* [PATCH v2] emulate sd_listen_fds for systemd support
  2015-06-30  8:47  7%           ` Christos Trochalakis
@ 2015-07-05  0:27  5%             ` Eric Wong
  0 siblings, 0 replies; 4+ results
From: Eric Wong @ 2015-07-05  0:27 UTC (permalink / raw)
  To: Christos Trochalakis
  Cc: Dmitry Smirnov, Hleb Valoshka, unicorn, unicorn-public

systemd socket emulation shares FDs across execve, just like
the built-in SIGUSR2 upgrade process in unicorn.  Thus it is
easy to support inheriting sockets from systemd.

Tested-by: Christos Trochalakis <yatiohi@ideopolis.gr>
---
  Christos Trochalakis <yatiohi@ideopolis.gr> wrote:
  > On Sat, Jun 27, 2015 at 04:01:36AM +0000, Eric Wong wrote:
  > >Unfortunately, testing this is tricky because FD=3
  > >(SD_LISTEN_FDS_START) tends to be grabbed by (MRI) Ruby 1.9.3
  > >and onwards for the internal self-pipe.

  OK, that was a bogus note.
  I forgot exec() in Ruby 1.9+ supports redirects natively.
  Test included in updated patch.

  > I have also tested it and works as expected. Also, hardcoding
  > SD_LISTEN_FDS_START seems like the best option. I 'd like to see that
  > applied to master.

  Thanks.  This will be in 5.0.0.pre2

 lib/unicorn/http_server.rb | 16 +++++++++++++---
 test/exec/test_exec.rb     | 24 ++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index 297b9f9..0f97516 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -766,12 +766,22 @@ class Unicorn::HttpServer
   def inherit_listeners!
     # inherit sockets from parents, they need to be plain Socket objects
     # before they become Kgio::UNIXServer or Kgio::TCPServer
-    inherited = ENV['UNICORN_FD'].to_s.split(',').map do |fd|
-      io = Socket.for_fd(fd.to_i)
+    inherited = ENV['UNICORN_FD'].to_s.split(',')
+
+    # emulate sd_listen_fds() for systemd
+    sd_pid, sd_fds = ENV.values_at('LISTEN_PID', 'LISTEN_FDS')
+    if sd_pid && sd_pid.to_i == $$
+      # 3 = SD_LISTEN_FDS_START
+      inherited.concat((3...(3 + sd_fds.to_i)).map { |fd| Socket.for_fd(fd) })
+    end
+    # to ease debugging, we will not unset LISTEN_PID and LISTEN_FDS
+
+    inherited.map! do |fd|
+      io = String === fd ? Socket.for_fd(fd.to_i) : fd
       io.autoclose = false
       io = server_cast(io)
       set_server_sockopt(io, listener_opts[sock_name(io)])
-      logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
+      logger.info "inherited addr=#{sock_name(io)} fd=#{io.fileno}"
       io
     end
 
diff --git a/test/exec/test_exec.rb b/test/exec/test_exec.rb
index 6deb96b..af8de26 100644
--- a/test/exec/test_exec.rb
+++ b/test/exec/test_exec.rb
@@ -96,6 +96,30 @@ run lambda { |env|
     end
   end
 
+  def test_sd_listen_fds_emulation
+    File.open("config.ru", "wb") { |fp| fp.write(HI) }
+    sock = TCPServer.new(@addr, @port)
+    sock.setsockopt(:SOL_SOCKET, :SO_KEEPALIVE, 0)
+
+    pid = xfork do
+      redirect_test_io do
+        # pretend to be systemd
+        ENV['LISTEN_PID'] = "#$$"
+        ENV['LISTEN_FDS'] = '1'
+
+        # 3 = SD_LISTEN_FDS_START
+        exec($unicorn_bin, "-l", "#@addr:#@port", 3 => sock)
+      end
+    end
+    res = hit(["http://#{@addr}:#{@port}/"])
+    assert_equal [ "HI\n"], res
+    assert_shutdown(pid)
+    assert_equal 1, sock.getsockopt(:SOL_SOCKET, :SO_KEEPALIVE).int,
+                 "unicorn should always set SO_KEEPALIVE on inherited sockets"
+  ensure
+    sock.close if sock
+  end
+
   def test_working_directory_rel_path_config_file
     other = Tempfile.new('unicorn.wd')
     File.unlink(other.path)
-- 
EW

^ permalink raw reply related	[relevance 5%]

* Re: [RFC] emulate sd_listen_fds for systemd support
  2015-06-27  4:01  6%         ` [RFC] emulate sd_listen_fds for systemd support Eric Wong
@ 2015-06-30  8:47  7%           ` Christos Trochalakis
  2015-07-05  0:27  5%             ` [PATCH v2] " Eric Wong
  0 siblings, 1 reply; 4+ results
From: Christos Trochalakis @ 2015-06-30  8:47 UTC (permalink / raw)
  To: Eric Wong; +Cc: Dmitry Smirnov, Hleb Valoshka, unicorn, unicorn-public

On Sat, Jun 27, 2015 at 04:01:36AM +0000, Eric Wong wrote:
>Unfortunately, testing this is tricky because FD=3
>(SD_LISTEN_FDS_START) tends to be grabbed by (MRI) Ruby 1.9.3
>and onwards for the internal self-pipe.
>
>So for now, I've manually tested this with a systemd installation
>
>Disclaimer: this is also my first experience using systemd
>

I have also tested it and works as expected. Also, hardcoding
SD_LISTEN_FDS_START seems like the best option. I 'd like to see that
applied to master.


^ permalink raw reply	[relevance 7%]

* [RFC] emulate sd_listen_fds for systemd support
  @ 2015-06-27  4:01  6%         ` Eric Wong
  2015-06-30  8:47  7%           ` Christos Trochalakis
  0 siblings, 1 reply; 4+ results
From: Eric Wong @ 2015-06-27  4:01 UTC (permalink / raw)
  To: Christos Trochalakis
  Cc: Dmitry Smirnov, Hleb Valoshka, unicorn, unicorn-public

Unfortunately, testing this is tricky because FD=3
(SD_LISTEN_FDS_START) tends to be grabbed by (MRI) Ruby 1.9.3
and onwards for the internal self-pipe.

So for now, I've manually tested this with a systemd installation

Disclaimer: this is also my first experience using systemd
---
  Eric Wong <e@80x24.org> wrote:
  > OK, I'll probably add LISTEN_FDS and LISTEN_PID support to unicorn
  > directly so the wrapper is unnecessary.

 lib/unicorn/http_server.rb | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index 297b9f9..0f97516 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -766,12 +766,22 @@ def redirect_io(io, path)
   def inherit_listeners!
     # inherit sockets from parents, they need to be plain Socket objects
     # before they become Kgio::UNIXServer or Kgio::TCPServer
-    inherited = ENV['UNICORN_FD'].to_s.split(',').map do |fd|
-      io = Socket.for_fd(fd.to_i)
+    inherited = ENV['UNICORN_FD'].to_s.split(',')
+
+    # emulate sd_listen_fds() for systemd
+    sd_pid, sd_fds = ENV.values_at('LISTEN_PID', 'LISTEN_FDS')
+    if sd_pid && sd_pid.to_i == $$
+      # 3 = SD_LISTEN_FDS_START
+      inherited.concat((3...(3 + sd_fds.to_i)).map { |fd| Socket.for_fd(fd) })
+    end
+    # to ease debugging, we will not unset LISTEN_PID and LISTEN_FDS
+
+    inherited.map! do |fd|
+      io = String === fd ? Socket.for_fd(fd.to_i) : fd
       io.autoclose = false
       io = server_cast(io)
       set_server_sockopt(io, listener_opts[sock_name(io)])
-      logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
+      logger.info "inherited addr=#{sock_name(io)} fd=#{io.fileno}"
       io
     end
 
-- 
EW

^ permalink raw reply related	[relevance 6%]

Results 1-4 of 4 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2015-06-15 22:56     [ANN] unicorn 5.0.0.pre1 - incompatible changes! Eric Wong
2015-07-06 21:41  6% ` [ANN] unicorn 5.0.0.pre2 - another prerelease! Eric Wong
     [not found]     <3137844.nsIDTWmvl4@debstor>
     [not found]     ` <20150625083118.GA22140@luke.ws.skroutz.gr>
2015-06-25 23:26       ` [DRE-maint] unicorn: native systemd service Eric Wong
2015-06-26 11:41         ` Christos Trochalakis
2015-06-27  3:05           ` Eric Wong
2015-06-27  4:01  6%         ` [RFC] emulate sd_listen_fds for systemd support Eric Wong
2015-06-30  8:47  7%           ` Christos Trochalakis
2015-07-05  0:27  5%             ` [PATCH v2] " Eric Wong

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.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).