Writing hooks for Perlbal

Raistlin Majere raistlin at e-raist.com
Fri Oct 6 06:35:21 UTC 2006


Elliot,
	Thank you for the information, I greatly appreciate it.

	I apologize for my ignorance as I'm still new to Perlbal.  I'm  
looking at AtomStream.pm at: http://code.sixapart.com/svn/perlbal/ 
trunk/lib/Perlbal/Plugin/AtomStream.pm and I see the snippet:

     Perlbal::Socket::register_callback(1, sub {
         my $now = time();
         emit_timestamp($now) if $now > $last_timestamp;
         return 1;
     });

And from that I'm guessing I can do something along the lines create  
a package (like the one below (not sure if I'm even close on the  
structure) and place it in the Plugin directory.

How would I get Perlbal to trigger my code via the plugin?  Would I  
have to modify my perlbal.conf file?  (I'm guessing I'd have to  
modify my service that's setup to be a selector which contains my  
vhosts otherwise I'd have to add it to each one of my reproxy services.)

Thanks again in advance for your any help you can provide.


---

package Perlbal::Plugin::TestServer.pm;
use Perlbal;
use strict;
use warnings

sub register {
     my ($class, $svc) = @_;


     Perlbal::Socket::register_callback(1, sub {
         my_subroutine($args);
         return 1;
     });

     $svc->register_hook('TestServer', 'start_proxy_request',  
&my_subroutine);

     return 1;
}

sub my_subroutine {
         my Perlbal::ClientProxy $self = shift;
         my Perlbal::HTTPHeaders $hds = $self->{req_headers};
         return 0 unless $hds;

	#psuedo code
	if (!backend_server_is_alive) { $hds->{headers}->{host} = "newhost: 
787"; }

	return 1;
}
sub unregister {
     my ($class, $svc) = @_;

     return 1;
}

# called when we are loaded
sub load {
     return 1;
}

# called for a global unload
sub unload {
     return 1;
}

On 5-Oct-06, at 10:25 PM, Elliot F wrote:

> Raistlin,
>
> Take a look at registering a callback that perlbal will trigger  
> every N seconds.  AtomStream.pm has it being used.  It's called  
> 'Perlbal::Socket::register_callback'.  You would be able to do  
> whatever check(s) you want, and react accordingly (mark it as down  
> somehow, reconfigure perlbal, etc.)  And it wouldn't be doing it  
> for every request.
>
> Just make sure you don't block.  :)
>
> Elliot
>
> Raistlin Majere wrote:
>> I've installed and configured perlbal and have run into some  
>> difficulties.  I've setup perlbal to be a reverse proxy so that it  
>> correctly proxies virtualhosts to the appropriate servers.  (i.e.  
>> vhost1.example.com, vhost2.example.com correctly reverse proxies  
>> for vhost1.com and vhost2.com respectively) This is working  
>> perfectly, and wasn't too difficult to configure.
>> However now I want to write some code that will:
>> First verify that the backend server is alive
>> Second that the backend server can answer requests on the port I'm  
>> trying to talk to
>> And if it's NOT alive and able to respond to requests that I can  
>> redirect requests to a different server, or execute some code to  
>> bring the downed server up, before retrying.
>> I've tried to use the Perlbal::ClientProxy "start_proxy_request"  
>> hook, however that seems to affect each and every request (i.e.  
>> each html page, graphic, etc.)
>> I need to be able to check to see if the backend server is alive,  
>> and if it's not, tell Perlbal: "Whoa hold your horeses" long  
>> enough for me to bring a server up (I can do this with some  
>> scripts I have), and then redirect the request to the new server.
>> Any help or advice would be greatly appreciated.



More information about the perlbal mailing list