NAME

Perlbal::Manual::Hooks - How Perlbal's hooks work

VERSION

Perlbal 1.78.

DESCRIPTION

Basically, a hook is a bit of code that is run at certain stages in the requests that Perlbal handles.There are all kinds of hooks available and they all do different things. Some are only applicable to some of the roles and others are applicable only to certain classes. Each hook is described in detail below, but first a description of the basics of a hook.

In general, you define a hook by calling the register_hook method on a Perlbal::Service object. You specify what hook you are interested in and provide a reference to a subroutine that will be called with the parameters particular to that hook.

There are three types of hooks:

Global hooks

These are hooks that are defined on a global scale. They are set like so:

Perlbal::register_global_hook('foo', sub { return 0; });

That would define a global hook named foo that would return 0 when it's called. (Return codes from hooks will be explained below)

Global hooks are useful to define management commands. See manage_command under Perlbal::Manual::Plugins for more information.

Service handler hooks

A handler hook is attached to a particular service. These hooks are called one at a time until one hook returns 1. At that point, no further hooks are called. For example:

$service->register_hook('bar', sub {
# do something
return 1;
});

When this hook runs, it would return 1, signalling to Perlbal that it had done what it needed to do and that Perlbal shouldn't call any further hooks. You can use this type of hook to create sets of plugins that all handle different types of requests, and when one hook had handled a request it wouldn't continue telling other hooks about the request.

backend_client_assigned

Happens right after a backend is assigned to a client, but before we've talked to the backend and asked it to do something. If you return a true value, the process is stopped and you will manually have to send the client's request to the backend, etc.

See also concat_get_poststat_pre_send, static_get_poststat_file_missing and static_get_poststat_pre_send.

concat_get_poststat_pre_send

Called when the resulting file of a request for multiple files concatenated is about to be sent, right before the 200 response code is added as a header. Return a true value to overtake the connection.

handle_put

make_high_priority

Called when a request is received and right before we're about to determine if this request is high priority or not. Return a true value to make the request high priority; false to leave it alone. Note that this is only called when the request isn't already high priority due to cookie priority scheduling, which is done inside Perlbal::Service.

modify_response_headers

Called when we've set all the headers, and are about to serve a file. You can change or add response headers at this point, or cancel the process by returning a true value. You will have to send the response to the client yourself if you do this.

return_to_base

Called when a request has been finished, and control of the Client* object is about to be transferred back to ownership by a service selector. Return a true value if the perlbal core action in this situation should be bypassed.

start_file_reproxy

Called when we've been told to reproxy a file. If you return a true value, Perlbal will not perform any operations on the file and will simply return. You can also change the file in the scalar ref passed as the second parameter.

start_http_request

A generic hook that works for both webserver and proxy modes, run after either the specific start_proxy_request or start_web_request hooks below. Like those, you return true from this hook to takeover the connection.

See also concat_get_poststat_file_missing, concat_get_poststat_pre_send and static_get_poststat_file_missing.

Service general hooks

These hooks are defined the same way as above, but general hooks are all run. The return code is ignored. This can be useful for putting in code that records statistics about an action or something to that effect.