Class | PhusionPassenger::AbstractRequestHandler
In: | lib/phusion_passenger/abstract_request_handler.rb
Parent: | Object

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.

### Methods

cleanup main_loop main_loop_running? new soft_shutdown start_main_loop_thread

### Included Modules

DebugLogging Utils

### Constants

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 | = | 500
MAX_HEADER_SIZE | = | 128 * 1024
OBJECT_SPACE_SUPPORTS_LIVE_OBJECTS | = | ObjectSpace.respond_to?(:live_objects)
OBJECT_SPACE_SUPPORTS_ALLOCATED_OBJECTS | = | ObjectSpace.respond_to?(:allocated_objects)
OBJECT_SPACE_SUPPORTS_COUNT_OBJECTS | = | ObjectSpace.respond_to?(:count_objects)
GC_SUPPORTS_TIME | = | GC.respond_to?(:time)
GC_SUPPORTS_CLEAR_STATS | = | GC.respond_to?(:clear_stats)

### Attributes

connect_password | [RW] | A password with which clients must authenticate. Default is unauthenticated.
iterations | [R] | The number of times the main loop has iterated so far. Mostly useful for unit test assertions.
memory_limit | [RW] | 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.
processed_requests | [R] | Number of requests processed so far. This includes requests that raised exceptions.

server_sockets

[R]

A hash containing all server sockets that this request handler listens on. The hash is in the form of:

      {
         name1 => [socket_address1, socket_type1, socket1],
         name2 => [socket_address2, socket_type2, socket2],
         ...
      }

`name` is a Symbol. `socket_addressx` is the address of the socket, `socket_typex` is the socket‘s type (either ‘unix’ or ‘tcp’) and `socketx` is the actual socket IO objec. There‘s guaranteed to be at least one server socket, namely one with the name +:main+.

soft_termination_linger_time | [RW] | If a soft termination signal was received, then the main loop will quit the given amount of seconds after the last time a connection was accepted. Defaults to 3 seconds.

### Public Class methods

new(owner_pipe, options = {})

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.
- detach_key
- connect_password
- pool_account_username
- pool_account_password_base64

### Public Instance methods

cleanup()

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.

main_loop()

Enter the request handler‘s main loop.

main_loop_running?()

Check whether the main loop‘s currently running.

soft_shutdown()

Remove this request handler from the application pool so that no new connections will come in. Then make the main loop quit a few seconds after the last time a connection came in. This all is to ensure that no connections come in while we‘re shutting down.

May only be called while the main loop is running. May be called from any thread.

start_main_loop_thread()

Start the main loop in a new thread. This thread will be stopped by cleanup.

[Validate]