about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2017-03-08 00:52:23 +0000
committerEric Wong <e@80x24.org>2017-03-08 04:57:43 +0000
commit8ce88a3756b110e5e3001f640ebd53a5b11d8c65 (patch)
treeaa7a0eef9bf9315406e323829f3f6fb110254b3a /lib
parent77b9ec2aa017cabe9babbbcba4f0cf5cea5f7aca (diff)
downloadunicorn-8ce88a3756b110e5e3001f640ebd53a5b11d8c65.tar.gz
We can force kgio_tryaccept to return an internal class
for TCP objects by subclassing Kgio::TCPServer.

This avoids breakage in any unfortunate projects which depend on
our undocumented internal APIs, such as gctools
<https://github.com/tmm1/gctools>
Diffstat (limited to 'lib')
-rw-r--r--lib/unicorn/http_request.rb10
-rw-r--r--lib/unicorn/http_server.rb6
-rw-r--r--lib/unicorn/oob_gc.rb4
-rw-r--r--lib/unicorn/socket_helper.rb16
4 files changed, 24 insertions, 12 deletions
diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb
index 9acde50..68bde16 100644
--- a/lib/unicorn/http_request.rb
+++ b/lib/unicorn/http_request.rb
@@ -61,7 +61,7 @@ class Unicorn::HttpParser
   # returns an environment hash suitable for Rack if successful
   # This does minimal exception trapping and it is up to the caller
   # to handle any socket errors (e.g. user aborted upload).
-  def read(socket, listener)
+  def read(socket)
     clear
     e = env
 
@@ -82,7 +82,7 @@ class Unicorn::HttpParser
       false until add_parse(socket.kgio_read!(16384))
     end
 
-    check_client_connection(socket, listener) if @@check_client_connection
+    check_client_connection(socket) if @@check_client_connection
 
     e['rack.input'] = 0 == content_length ?
                       NULL_IO : @@input_class.new(socket, self)
@@ -105,8 +105,8 @@ class Unicorn::HttpParser
   end
 
   if defined?(Raindrops::TCP_Info)
-    def check_client_connection(socket, listener) # :nodoc:
-      if Kgio::TCPServer === listener
+    def check_client_connection(socket) # :nodoc:
+      if Unicorn::TCPClient === socket
         @@tcp_info ||= Raindrops::TCP_Info.new(socket)
         @@tcp_info.get!(socket)
         raise Errno::EPIPE, "client closed connection".freeze,
@@ -127,7 +127,7 @@ class Unicorn::HttpParser
       end
     end
   else
-    def check_client_connection(socket, listener) # :nodoc:
+    def check_client_connection(socket) # :nodoc:
       write_http_header(socket)
     end
   end
diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index 2aa1072..c2086cb 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -558,8 +558,8 @@ class Unicorn::HttpServer
 
   # once a client is accepted, it is processed in its entirety here
   # in 3 easy steps: read request, call app, write app response
-  def process_client(client, listener)
-    status, headers, body = @app.call(env = @request.read(client, listener))
+  def process_client(client)
+    status, headers, body = @app.call(env = @request.read(client))
 
     begin
       return if @request.hijacked?
@@ -655,7 +655,7 @@ class Unicorn::HttpServer
         # Unicorn::Worker#kgio_tryaccept is not like accept(2) at all,
         # but that will return false
         if client = sock.kgio_tryaccept
-          process_client(client, sock)
+          process_client(client)
           nr += 1
           worker.tick = time_now.to_i
         end
diff --git a/lib/unicorn/oob_gc.rb b/lib/unicorn/oob_gc.rb
index 74a1d51..5572e59 100644
--- a/lib/unicorn/oob_gc.rb
+++ b/lib/unicorn/oob_gc.rb
@@ -67,8 +67,8 @@ module Unicorn::OobGC
 
   #:stopdoc:
   PATH_INFO = "PATH_INFO"
-  def process_client(client, listener)
-    super(client, listener) # Unicorn::HttpServer#process_client
+  def process_client(client)
+    super(client) # Unicorn::HttpServer#process_client
     if OOBGC_PATH =~ OOBGC_ENV[PATH_INFO] && ((@@nr -= 1) <= 0)
       @@nr = OOBGC_INTERVAL
       OOBGC_ENV.clear
diff --git a/lib/unicorn/socket_helper.rb b/lib/unicorn/socket_helper.rb
index df8315e..5371413 100644
--- a/lib/unicorn/socket_helper.rb
+++ b/lib/unicorn/socket_helper.rb
@@ -3,6 +3,18 @@
 require 'socket'
 
 module Unicorn
+
+  # Instead of using a generic Kgio::Socket for everything,
+  # tag TCP sockets so we can use TCP_INFO under Linux without
+  # incurring extra syscalls for Unix domain sockets.
+  # TODO: remove these when we remove kgio
+  TCPClient = Class.new(Kgio::Socket) # :nodoc:
+  class TCPSrv < Kgio::TCPServer # :nodoc:
+    def kgio_tryaccept # :nodoc:
+      super(TCPClient)
+    end
+  end
+
   module SocketHelper
 
     # internal interface
@@ -148,7 +160,7 @@ module Unicorn
       end
       sock.bind(Socket.pack_sockaddr_in(port, addr))
       sock.autoclose = false
-      Kgio::TCPServer.for_fd(sock.fileno)
+      TCPSrv.for_fd(sock.fileno)
     end
 
     # returns rfc2732-style (e.g. "[::1]:666") addresses for IPv6
@@ -185,7 +197,7 @@ module Unicorn
     def server_cast(sock)
       begin
         Socket.unpack_sockaddr_in(sock.getsockname)
-        Kgio::TCPServer.for_fd(sock.fileno)
+        TCPSrv.for_fd(sock.fileno)
       rescue ArgumentError
         Kgio::UNIXServer.for_fd(sock.fileno)
       end