Scroll to navigation

Perlbal::Manual::Internals(3pm) User Contributed Perl Documentation Perlbal::Manual::Internals(3pm)

NAME

Perlbal::Manual::Internals - Perlbal's architecture at a glance

VERSION

Perlbal 1.78.

DESCRIPTION

Connections come in from wherever and get to the TCPListener. It uses Service objects to determine what kind of Client* to spawn. The Client classes then handle crafting the response for the user.

                            {{ INTERNET }}
                                  |
                                  v
              [Service]<===>[TCPListener]
                          ___/    |    \___
                         v        v        v
             [ClientManage]  [ClientHTTP] [ClientProxy]
                                                ^
                                                |
                                                v
                                          [BackendHTTP]

Perlbal decides what backend to send a request to randomly (only presently supported method). If that service has idle backend connections available, configured by "backend_persist" and "connect_ahead", it will reuse those connections and greatly reduce latency. See more detail in Perlbal::Manual::LoadBalancer.

Perlbal also specializes in "spoonfeeding" data to slow clients. This allows backends to continue serving requests while Perlbal transfers responses back as fast as the client can read.

Classes

The following is a brief introduction/overview to the main Perlbal's classes:

Perlbal::Socket

Descends from Danga::Socket.

Adds on to the base class to provide some functionality specifically useful for creating HTTP sockets.

Fields

Headers as they're being read.
The final Perlbal::HTTPHeaders object inbound.
Response headers outbound (Perlbal::HTTPHeaders object).
Creation time.
Last time noted alive.
General purpose state; used by descendants.
If on, die and do no further requests.
Arrayref of scalarref read from client.
Bytes sitting in read_buf.
Total bytes read from client, ever.
If true, the next header parsing will ignore a leading \r\n.
If defined, contains the observed IP string of the peer we're serving. This is intended for holding the value of the X-Forwarded-For and using it to govern ACLs.

Perlbal::TCPListener

Descends from Perlbal::Socket.

Very lightweight and fast connection accept class. Takes incoming connections as fast as possible and passes them off, instantiating one of the various Client* classes to handle it.

Fields

Perlbal::Service.
Scalar IP port of where this service is listening for new connections.
The SSL Options.

    use Data::Dumper;
    warn Dumper( $tcp_listener->{'sslopts'} );
    

The above lines would print something like the following:

    $VAR1 = {
              'ssl' => {
                         'SSL_cipher_list' => '...',
                         'SSL_cert_file' => '...',
                         'SSL_key_file' => ',,,',
                         'SSL_ca_path' => '...',
                         'SSL_verify_mode' => '...'
                       }
            };
    
Boolean value stating whether the installation of Perlbal supports IPv6 (which basically boils down to Danga::Socket v1.6.1 and IO::Socket::INET6 being available).

Perlbal::BackendHTTP

Descends from Perlbal::Socket.

This class handles connections to the backend web nodes for getting data back to the user. This class is used by other classes such as Perlbal::ClientProxy to send a request to an internal node.

Fields

Perlbal::ClientProxy connection, or undef.
Perlbal::Service.
Perlbal::Pool; whatever pool we spawned from.
IP scalar.
Port scalar.
"$ip:$port".
Object; must implement reporter interface.
Has been accepted by a webserver and we know for sure we're not just talking to the TCP stack.
If true, we're waiting for an OPTIONS * response to determine when we have attention.
Time this connection will be disconnected, if it's kept-alive and backend told us; otherwise "undef" for unknown.
Length of document being transferred. Only applies when the backend server sends a content-length header.
Bytes remaining to be read. Only applies when the backend server sends a content-length header.
Number of requests this backend's been used for.
Int; counts what generation we were spawned in.
Boolean. If on, we're doing a buffered upload transmit.
Extra storage; plugins can use it if they want.

Perlbal::HTTPHeaders

Header management. Parses headers (request and response) and stores data for further user. Also manages validation of the request line so that it conforms to HTTP specifications.

Fields

href; lowercase header -> comma-sep list of values.
Href; lowercase header -> provided case.
Aref; order headers were received (canonical order).
Scalar; request method (if GET request).
Scalar; request URI (if GET request).
"res" or "req".
HTTP response status code.
Status text that for response code.
Version (string) "1.1".
Version (number: major*1000+minor): "1.1" => 1001.
First line of HTTP response (if response).
First line of HTTP request (if request).

Perlbal::ClientHTTPBase

Descends from Perlbal::Socket.

Provides base functionality to Perlbal::ClientHTTP and Perlbal::ClientProxy. Notably, the ability to efficiently send files to the remote user. Also handles most of the state logic for statistics and such. Is also used for services of type "selector". Perlbal::ClientHTTPBase then reads in the request headers, and asks the service to re-bless the client instance to a more specific type, for either a Perlbal::ClientProxy or Perlbal::ClientHTTP (depending on selector's mapping).

Fields

Perlbal::Service object.
URI to send instead of the one requested; this is used to instruct "_serve_request" to send an index file instead of trying to serve a directory and failing.
Extra storage; plugins can use it if they want.
Filename the backend told us to start opening.
Size of file, once we "stat()" it.
If needed, IO::Handle of fd.
How much we've sent from the file.
Subref to run after we're done sendfile'ing the current file.
Number of requests this object has performed for the user.
The original service from which we came.
Whether the socket was SSL attached (restricted operations).

Perlbal::ClientHTTP

Descends from Perlbal::ClientHTTPBase.

Very simple and lightweight class. Handles sending files to the user without much overhead. Most of the functionality is contained in the parent class, and this class doesn't implement much new stuff.

Fields

1 when we're currently waiting for an async job to return.
File handle to use for writing data.
Filename of put_fh.
File offset to write next data at.
Length of document being transferred.
Bytes remaining to be read.
Boolean/obj: if processing a chunked upload, Perlbal::ChunkedUploadState object, else undef.

Perlbal::ClientProxy

Descends from Perlbal::ClientHTTPBase.

Takes an incoming connection from a user and connects to a backend node ("Perlbal::BackendHTTP") and relays the request. The backend can then either tell the proxy to reproxy and load a file from disk, or return a file directly, or just return a status message.

Fields

Perlbal::BackendHTTP object (or "undef" if disconnected).
True if we've requested a backend for this request.
Number of times we've tried to reconnect to backend.
Boolean; 1 if we are or were in the high priority queue.
Boolean; 1 if we are or were in the low priority queue.
Arrayref; URIs to reproxy to, in order.
Int: size of response we expect to get back for reproxy.
Arrayref; the host info and URI we're reproxying right now.
Int: amount of data we're still waiting for.
Bool: whether we've already sent a response to the user or not.
Int: time that we last received a request.
If defined, we are doing a transparent reproxy-URI and the headers we get back aren't necessarily the ones we want. Instead, get most headers from the provided "res" headers object here.
Bool; if we're buffering some/all of a request to memory/disk.
Bool; if on, we currently have an "aio_write" out.
Hi-res time when we started getting data to upload.
Buffered upload filehandle object.
String; buffered upload filename.
String; if defined, the reason we're buffering to disk.
Int; buffered output position.
Boolean: if backend has shut off its reads because we're too slow.
Boolean: if we shut off reads while we know data is yet to be read from client.
Bool/obj: if processing a chunked upload, Perlbal::ChunkedUploadState object, else undef.
Integer: request's body length, either as-declared, or calculated after chunked upload is complete.
Unixtime we last sent a UDP upload packet. For perlbal sending out UDP packets related to upload status (for xmlhttprequest upload bar).
Client's self-generated upload session. For perlbal sending out UDP packets related to upload status (for xmlhttprequest upload bar).
Number of times we've retried this request so far after getting 500 errors.

Perlbal::ClientManage

Descends from Perlbal::Socket.

Simple interface that provides a way for users to use the management interface of Perlbal. You can connect to the management port (as defined in the config file) with a web browser or regular telnet (see Perlbal::Manual::Management for more information on this).

Fields

Perlbal::Service.
Read buffer.
Boolean stating whether the request is HTTP.
Perlbal::CommandContext.

Perlbal::Service

A service is a particular item that Perlbal is doing. Services can have a role which defines how they behave. Each service can also have a bunch of parameters set to further adjust its behavior. By itself, the Service class handles maintaining pools of backend connections and managing statistics about itself.

Fields

Name of the service.
Role type ("web_server", "reverse_proxy", etc).
Boolean; whether we're enabled or not (enabled = listening).
Perlbal::Pool that we're using to allocate nodes if we're in proxy mode.
Perlbal::TCPListener object, when enabled.
Perlbal::Cache object, when enabled.

End-user tunables

"IP:port" of where we're listening for new connections.
Document root for "web_server" role.
Boolean; directory indexing (for "web_server" role). Not async.
Arrayref of filenames to try for index files.
Boolean; if user can request concatenated files.
Boolean; whether PUT is supported.
Max size in bytes of a put file.
Max size in bytes of a chunked request (to be written to disk first).
Number of directories required to exist at beginning of URIs in put.
Boolean; whether DELETE is supported.
Cookie name to check if the client's requests should be considered high priority.

See also "high_priority_cookie_contents".

Aforementioned cookie value must contain this substring.
Max number of persistent backends to hold onto while no clients.
Boolean; persistent connections for clients.
Boolean; persistent connections for backends.
Boolean; get attention of backend before giving it clients (using OPTIONS).
Path to check with the OPTIONS request (default is "*").
Max requests to send per kept-alive backend (default 0 = unlimited).
Number of spare backends to connect to in advance all the time.
How much data a Perlbal::ClientProxy object should buffer from a backend.
Same as above but for backends that are reproxying for us.
Number of outstanding standard priority connections to activate pressure relief at.
Int, 0-100; % chance to take a standard priority request when we're in pressure relief mode.
Array of Net::Netmask objects containing netmasks for trusted upstreams.
Boolean; if true, always trust upstreams.
Boolean; if true, do not modify "X-Forwarded-For", "X-Host", or "X-Forwarded-Host" headers.
Boolean; if true, advertise that server will reproxy files and/or URLs.
Maximum number of reproxy results to be cached. (0 is disabled and default).
Bytes for "SO_SNDBUF".
Path to server process (executable).
Keep-alive timeout in seconds for clients (default is 30).
Idle timeout outside of keep-alive time (default is 30).

Internal state

Arrayref of clients waiting for backendhttp connections.
Arrayref of high-priority clients waiting for backendhttp connections.
Arrayref of low-priority clients waiting for backendhttp connections.
Number of clients waiting for backends.
Map of clientproxy fd -> 1 (if they're waiting for a connection).
Hashref of "ip:port" -> $time (only one pending connect to backend at a time).
Number of outstanding backend connects.
Arrayref of backends we've already connected to, but haven't got clients.
Hashref: hookname => [ [ plugin, ref ], [ plugin, ref ], ... ].
Hashref: name => 1.
Arrayref: name, name, name...
Hashref: { plugin_name => { key_name => coderef } }.
Hashref with extra config options; name => values.
Boolean; if true, we're currently in "spawn_backends".
{ insert => [ [ header, value ], ... ], remove => [ header, header, ... ], set => [ [ header, value ], ... ] }.

Used in header management interface.

Int; generation count so we can slough off backends from old pools.
{ "ip:port" => 1 }.

If on, "spawn_backends" will ignore this "ip:port" combo.

0 if off; otherwise, number of bytes to buffer before we ask for a backend.
CODE ref, or undef, for role "selector" services.
Name of a service a selector should default to.
Boolean; enable/disable the buffered uploads to disk system.
Path to store buffered upload files.
Int; buffer uploads estimated to take longer than this.
Int; buffer uploads greater than this size (in bytes).
Int; buffer uploads uploading at less than this rate (in bytes/sec).
Comma separated list of "ip:port" of UDP upload status receivers.
Arrayref of sockaddrs (packed ip/port).
Boolean; whether this service speaks SSL to the client.
File path to key pem file.
File to path to cert pem file.
OpenSSL cipher list string.
Path to certificates directory.
Int; verification mode, see IO::Socket::SSL.
Boolean; whether we should retry requests after errors.
Comma-separated seconds (full or partial) to delay between retries.
Milliseconds of latency to add to request.
Boolean; whether to provide a "Server" header.
_stat_requests
Total requests to this service.
_stat_cache_hits
Total requests to this service that were served via the reproxy-url cache.
2022-06-28 perl v5.34.0