The request handler‘s job is to process incoming HTTP requests using the currently loaded Ruby on Rails application. HTTP requests are forwarded to the request handler by the web server. HTTP responses generated by the RoR application are forwarded to the web server, which, in turn, sends the response back to the HTTP client.
Design decisions
Some design decisions are made because we want to decrease system administrator maintenance overhead. These decisions are documented in this section.
Abstract namespace Unix sockets
RequestHandler listens on a Unix socket for incoming requests. If possible, RequestHandler will try to create a Unix socket on the _abstract namespace_, instead of on the filesystem. If the RoR application crashes (segfault), or if it gets killed by SIGKILL, or if the system loses power, then there will be no stale socket files left on the filesystem. Unfortunately, abstract namespace Unix sockets are only supported by Linux. On systems that do not support abstract namespace Unix sockets, RequestHandler will automatically fallback to using regular Unix socket files.
It is possible to force RequestHandler to use regular Unix socket files by setting the environment variable PASSENGER_NO_ABSTRACT_NAMESPACE_SOCKETS to 1.
Owner pipes
Because only the web server communicates directly with a request handler, we want the request handler to exit if the web server has also exited. This is implemented by using a so-called _owner pipe_. The writable part of the pipe will be owned by the web server. RequestHandler will continuously check whether the other side of the pipe has been closed. If so, then it knows that the web server has exited, and so the request handler will exit as well. This works even if the web server gets killed by SIGKILL.
Request format
Incoming "HTTP requests" are not true HTTP requests, i.e. their binary representation do not conform to RFC 2616. Instead, the request format is based on CGI, and is similar to that of SCGI.
The format consists of 3 parts:
- A 32-bit big-endian integer, containing the size of the transformed headers.
- The transformed HTTP headers.
- The verbatim (untransformed) HTTP request body.
HTTP headers are transformed to a format that satisfies the following grammar:
headers ::= header* header ::= name NUL value NUL name ::= notnull+ value ::= notnull+ notnull ::= "\x01" | "\x02" | "\x02" | ... | "\xFF" NUL = "\x00"
The web server transforms the HTTP request to the aforementioned format, and sends it to the request handler.
HARD_TERMINATION_SIGNAL | = | "SIGTERM" |
Signal which will cause the Rails application to exit immediately. | ||
SOFT_TERMINATION_SIGNAL | = | "SIGUSR1" |
Signal which will cause the Rails application to exit as soon as it‘s done processing a request. | ||
BACKLOG_SIZE | = | 50 |
MAX_HEADER_SIZE | = | 128 * 1024 |
NINJA_PATCHING_LOCK | = | Mutex.new |
PASSENGER_VERSION | = | $1 |
[R] | socket_name |
The name of the socket on which the request handler accepts new connections. This is either a
Unix socket filename, or the name for an abstract namespace Unix socket.
If socket_name refers to an abstract namespace Unix socket, then the name does not contain a leading null byte. See also using_abstract_namespace? |
Create a new RequestHandler with the given owner pipe. owner_pipe must be the readable part of a pipe IO object.
[ show source ]
# File lib/passenger/request_handler.rb, line 119 119: def initialize(owner_pipe) 120: if ENV['PASSENGER_NO_ABSTRACT_NAMESPACE_SOCKETS'].blank? 121: @using_abstract_namespace = create_unix_socket_on_abstract_namespace 122: else 123: @using_abstract_namespace = false 124: end 125: if !@using_abstract_namespace 126: create_unix_socket_on_filesystem 127: end 128: @owner_pipe = owner_pipe 129: @previous_signal_handlers = {} 130: 131: NINJA_PATCHING_LOCK.synchronize do 132: if !@@ninja_patched_action_controller && defined?(::ActionController::Base) \ 133: && ::ActionController::Base.private_method_defined?(:perform_action) 134: @@ninja_patched_action_controller = true 135: ::ActionController::Base.class_eval do 136: alias passenger_orig_perform_action perform_action 137: 138: def perform_action(*whatever) 139: headers["X-Powered-By"] = "Phusion Passenger (mod_rails) #{PASSENGER_VERSION}" 140: passenger_orig_perform_action(*whatever) 141: end 142: end 143: end 144: end 145: end
Clean up temporary stuff created by the request handler. This method should be called after the main loop has exited.
[ show source ]
# File lib/passenger/request_handler.rb, line 149 149: def cleanup 150: @socket.close rescue nil 151: @owner_pipe.close rescue nil 152: if !using_abstract_namespace? 153: File.unlink(@socket_name) rescue nil 154: end 155: end
Enter the request handler‘s main loop.
[ show source ]
# File lib/passenger/request_handler.rb, line 163 163: def main_loop 164: reset_signal_handlers 165: begin 166: done = false 167: while !done 168: client = accept_connection 169: if client.nil? 170: break 171: end 172: trap SOFT_TERMINATION_SIGNAL do 173: done = true 174: end 175: process_request(client) 176: trap SOFT_TERMINATION_SIGNAL, DEFAULT 177: end 178: rescue EOFError 179: # Exit main loop. 180: rescue Interrupt 181: # Exit main loop. 182: rescue SignalException => signal 183: if signal.message != HARD_TERMINATION_SIGNAL && 184: signal.message != SOFT_TERMINATION_SIGNAL 185: raise 186: end 187: ensure 188: revert_signal_handlers 189: end 190: end
[ show source ]
# File lib/passenger/request_handler.rb, line 138 138: def perform_action(*whatever) 139: headers["X-Powered-By"] = "Phusion Passenger (mod_rails) #{PASSENGER_VERSION}" 140: passenger_orig_perform_action(*whatever) 141: end
Returns whether socket_name refers to an abstract namespace Unix socket.
[ show source ]
# File lib/passenger/request_handler.rb, line 158 158: def using_abstract_namespace? 159: return @using_abstract_namespace 160: end