=head1 NAME Perlbal::Manual - Creating and using plugins =head2 VERSION Perlbal 1.78. =head2 DESCRIPTION How to create and use Perlbal plugins. =head2 How to use a plugin Perlbal supports plugins through modules under C that implement a set of functions described further down this document. Some of these plugins are shipped with Perlbal itself, while others can be found on CPAN (you can also create your own plugin and have it available only locally). In order to use a plugin you first have to load it; on your Perlbal's configuration file add something like: Load MyPlugin This loads plugin C. Each plugin will have its own way of being configured (some don't require any configuration at all), so you'll have to refer to their documentation (or code). Typically (but not always), a plugin will allow you to set additional parameters to a service; for instance: LOAD MaxContentLength CREATE SERVICE example SET max_content_length = 100000 SET plugins = MaxContentLength C is a parameter of L. If you're worried that two plugins may have the same parameter, of if you simply want to define those variables all in the same spot and thus will be doing it outside of the plugin's context, you can use the more verbose syntax: SET my_service.my_plugin.my_variable = my_value Notice that some plugins need to be stated service by service; hence, this line: SET plugins = MaxContentLength The C parameter (a list of strings separated by commas or spaces) defines which plugins are acceptable for a service. =head3 Troubleshooting If you try to load a plugin and receive the following error message: ERROR: Can't locate Perlbal/Plugin/MyPlugin.pm in @INC That means that either the plugin isn't installed or perlbal couldn't find it. (perhaps it is installed in a different version of perl other than the one used to run perlbal?) =head2 How to create a plugin A Perlbal plugin consists in a package under the C namespace that implements a number of functions: C, C, C and C. These steps and functions (plus some helper functions you can define or use) are described below. PLEASE KEEP IN MIND: Perlbal is a single-process, asynchronous web server. You must not do things in plugins which will cause it to block, or no other requests can be served at the same time. =head3 Creating a package While there are many ways of creating a package, we'd recommend that you use something to do it for you. A good option is L. (note: if you really want to, you can just create a file with your package and use it; by using something like L you're making sure that several pitfalls are avoided, lots of basic rules are followed and that your package can easily be made available as a distribution that you can deploy on any machine - or, if you feel so inclined, upload to CPAN - in a simple way) Let's assume you want to create a plugin that checks requests for a C header and, if present, add an header C to the response when serving a file. Let's assume your plugin will be called C. Having installed L, here's a command you can run that will create your package for you: $ module-starter --module=Perlbal::Plugin::ColorOfMagic --author="My name" --email=my@email.address That should create a file tree that you can get better acquainted with by reading L's fine documentation. For this example, the file you really need should now reside in C. This file probably starts with something like the following: package Perlbal::Plugin::ColorOfMagic; use warnings; use strict; You'll have to add a few functions to this file. These are described below. (note: upon creating this package, some boilerplate documentation will also be present on the file; you should revise it and even remove bits that don't feel right for your plugin) =head3 register C is called when the plugin is being added to a service. This is where you register your plugin's hooks, if required (see L for the list of existing hooks and further documentation on how they work). For the sake of our example (C, see above), what we want to do is register a hook that modifies the response headers; that means we want a C hook. Here's what you'd do: sub register { my ($class, $service) = @_; my $my_hook_code = sub { my Perlbal::ClientHTTPBase $cp = shift; if ( $cp->{req_headers}->header('X-Magic') ) { $cp->{res_headers}->header( 'X-Color', 'Octarine' ); } return 0; }; $service->register_hook('ColorOfMagic','modify_response_headers', $my_hook_code); } Inside C, we're calling C to register our C C hook. Its code, that will run "when we've set all the headers, and are about to serve a file" (see L), receives a L object (you can see what kind of object your hook will receive on L). We're checking to see if C is defined on the request and, if so, we're setting header C on the response to C. Notice that the hook ends with C. This is because returning a true value means that you want to cancel the connection to the backend and send the response to the client yourself. =head3 unregister C is called when the plugin is removed from a service. It's a standard good practice to unregister your plugin's hooks here, like so: sub unregister { my ($class, $service) = @_; $service->unregister_hooks('ColorOfMagic'); return 1; } You can also use C to unregister one single hook: $service->unregister_hook('ColorOfMagic', 'modify_response_headers'); =head3 load C is called when your plugin is loaded (or reloaded). This is where you should perform your plugin's initialization, which can go from setting up some variables to registering a management command (to register commands see the documentation for C further down this document). my $color; sub load { my $class = shift; $color = 'Octarine'; return 1; } C must always be defined, but if you really don't need it you can have it simply returning a true value: sub load { return 1; } =head3 unload C is called when your plugin is unloaded. This is where you should perform any clean up tasks. C must always be defined, but if you really don't need it you can have it simply returning a true value: sub unload { return 1; } Don't forget to call C if you have registered any (see the documentation for C further down this document and you'll see what we're talking about). =head3 register vs. load C is called when the plugin is loaded, while C is called whenever the plugin is set for a service. This means that you should use C for anything that is global, such as registering a global hook, and you should use C for things that are specific to a service, such as registering service hooks. =head3 dumpconfig C is not required. When managing Perlbal (see L) you can send a C command that will result in a configuration dump. Apart from the global configuration, each plugin that implements a C function will also have that function called. C should return an array of messages to be displayed. sub dumpconfig { my ($class, $service) = @_; my @messages; push @messages, "COLOROFMAGIC is $color"; return @messages; } Again, C is not required, so implement it only if it makes sense for your plugin. =head3 Helper functions =head4 add_tunable Adding a tunable will allow you to set its value within each plugin: LOAD MyPlugin CREATE SERVICE my_service SET my_new_parameter = 42 SET plugins = MyPlugin ENABLE my_service C can be used by plugins that want to add tunables so that the config file can have more options for service settings. sub load { Perlbal::Service::add_tunable( my_new_parameter => { check_role => '*', check_type => 'int', des => "description of my new parameter", default => 0, }, ); return 1; } C defines for which roles the value can be set (C, C, etc). A value of C<*> mean that the value can be set for any role. The acceptable values for C are C, C, C, C, C, C, C and C. An B error message will be displayed whenever you try to set a value that has an unknown C. C can also contain a code reference that will be used to validate the type. check_type => sub { my $self = shift; my $val = shift; my $emesg = shift; ... }, This code reference should return a true or false value. If returning false, the contents of C<$emesg> (which is passed as a reference to the function) will be used as the error message. Here's a better explanation of the acceptable values for C: =over 4 =item bool Boolean value. Must be defined and will be checked as a Perl value. =item directory_or_none The value needs to be defined and the content must be an existing directory (validated against perl's B<-d> switch). =item enum An array reference containing the acceptable values: check_type => ["yellow", "blue", "green"], =item file A filename, validated against perl's B<-f> switch. =item file_or_none A filename, validated against perl's B<-f> switch, or the default value. =item int An integer value, validated against C. =item regexp Regular expression. The correct form of setting a regexp tunable is by setting it as an array reference containing the type (C), the regular expression and a message that can explain it: check_type => ["regexp", qr/^\d+\.\d+\.\d+\.\d+:\d+$/, "Expecting IP:port of form a.b.c.d:port."], =item size A size, validated against C. =back =head4 manage_command Perlbal catches unknown configuration commands and tries to match them against hooks in the form of C. Let's say that you want to set a management command C