The request handler is the layer which connects Apache with the underlying application‘s request dispatcher (i.e. either Rails‘s Dispatcher class or Rack). 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.
AbstractRequestHandler is an abstract base class for easing the implementation of request handlers for Rails and Rack.
Design decisions
Some design decisions are made because we want to decrease system administrator maintenance overhead. These decisions are documented in this section.
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 passed to the web server* via a Unix socket, and the web server will own that part of the pipe, while AbstractRequestHandler owns the readable part of the pipe. AbstractRequestHandler 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.
- It might also be passed to the ApplicationPoolServerExecutable, if the web server‘s using ApplicationPoolServer instead of StandardApplicationPool.
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 | = | 100 |
MAX_HEADER_SIZE | = | 128 * 1024 |
PASSENGER_HEADER | = | determine_passenger_header |
[R] | iterations | The number of times the main loop has iterated so far. Mostly useful for unit test assertions. |
[RW] | memory_limit |
Specifies the maximum allowed memory usage, in MB. If after having
processed a request AbstractRequestHandler detects that
memory usage has risen above this limit, then it will gracefully exit (that
is, exit after having processed all pending requests).
A value of 0 (the default) indicates that there‘s no limit. |
[R] | processed_requests | Number of requests processed so far. This includes requests that raised exceptions. |
[R] | socket_name |
The name of the socket on which the request handler accepts new connections. At this
moment, this value is always the filename of a Unix domain socket.
See also #socket_type. |
[R] | socket_type | The type of socket that #socket_name refers to. At the moment, the value is always ‘unix’, which indicates a Unix domain socket. |
Create a new RequestHandler with the given owner pipe. owner_pipe must be the readable part of a pipe IO object.
Additionally, the following options may be given:
- memory_limit: Used to set the memory_limit attribute.
[ show source ]
# File lib/phusion_passenger/abstract_request_handler.rb, line 138 138: def initialize(owner_pipe, options = {}) 139: if should_use_unix_sockets? 140: create_unix_socket_on_filesystem 141: else 142: create_tcp_socket 143: end 144: @socket.close_on_exec! 145: @owner_pipe = owner_pipe 146: @previous_signal_handlers = {} 147: @main_loop_thread_lock = Mutex.new 148: @main_loop_thread_cond = ConditionVariable.new 149: @memory_limit = options["memory_limit"] || 0 150: @iterations = 0 151: @processed_requests = 0 152: end
Clean up temporary stuff created by the request handler.
If the main loop was started by #main_loop, then this method may only be called after the main loop has exited.
If the main loop was started by #start_main_loop_thread, then this method may be called at any time, and it will stop the main loop thread.
[ show source ]
# File lib/phusion_passenger/abstract_request_handler.rb, line 161 161: def cleanup 162: if @main_loop_thread 163: @main_loop_thread.raise(Interrupt.new("Cleaning up")) 164: @main_loop_thread.join 165: end 166: @socket.close rescue nil 167: @owner_pipe.close rescue nil 168: File.unlink(@socket_name) rescue nil 169: end
Enter the request handler‘s main loop.
[ show source ]
# File lib/phusion_passenger/abstract_request_handler.rb, line 177 177: def main_loop 178: reset_signal_handlers 179: begin 180: @graceful_termination_pipe = IO.pipe 181: @graceful_termination_pipe[0].close_on_exec! 182: @graceful_termination_pipe[1].close_on_exec! 183: 184: @main_loop_thread_lock.synchronize do 185: @main_loop_running = true 186: @main_loop_thread_cond.broadcast 187: end 188: 189: install_useful_signal_handlers 190: 191: while true 192: @iterations += 1 193: client = accept_connection 194: if client.nil? 195: break 196: end 197: begin 198: headers, input = parse_request(client) 199: if headers 200: if headers[REQUEST_METHOD] == PING 201: process_ping(headers, input, client) 202: else 203: process_request(headers, input, client) 204: end 205: end 206: rescue IOError, SocketError, SystemCallError => e 207: print_exception("Passenger RequestHandler", e) 208: ensure 209: # 'input' is the same as 'client' so we don't 210: # need to close that. 211: client.close rescue nil 212: end 213: @processed_requests += 1 214: end 215: rescue EOFError 216: # Exit main loop. 217: rescue Interrupt 218: # Exit main loop. 219: rescue SignalException => signal 220: if signal.message != HARD_TERMINATION_SIGNAL && 221: signal.message != SOFT_TERMINATION_SIGNAL 222: raise 223: end 224: ensure 225: @graceful_termination_pipe[0].close rescue nil 226: @graceful_termination_pipe[1].close rescue nil 227: revert_signal_handlers 228: @main_loop_thread_lock.synchronize do 229: @main_loop_running = false 230: @main_loop_thread_cond.broadcast 231: end 232: end 233: end
Check whether the main loop‘s currently running.
[ show source ]
# File lib/phusion_passenger/abstract_request_handler.rb, line 172 172: def main_loop_running? 173: return @main_loop_running 174: end
[ show source ]
# File lib/phusion_passenger/abstract_request_handler.rb, line 236 236: def start_main_loop_thread 237: @main_loop_thread = Thread.new do 238: main_loop 239: end 240: @main_loop_thread_lock.synchronize do 241: while !@main_loop_running 242: @main_loop_thread_cond.wait(@main_loop_thread_lock) 243: end 244: end 245: end