|
NAMEResourcePool - A connection caching and pooling class.SYNOPSISuse ResourcePool; use ResourcePool::Factory; my $factory = ResourcePool::Factory->new("arg1"); my $pool = ResourcePool->new($factory, MaxTry => 3); my $resource = $pool->get(); # get a resource out of the pool #[...] # do something with $resource $pool->free($resource); # give it back to the pool $pool->fail($resource); # give back a failed resource DESCRIPTIONThe ResourcePool is a generic connection caching and pooling management facility. It might be used in an Apache/mod_perl environment to support connection caching like Apache::DBI for non-DBI resources (e.g. Net::LDAP). It's also useful in a stand alone perl application to handle connection pools.The key benefit of ResourcePool is the generic design which makes it easily extensible to new resource types. The ResourcePool has a simple check mechanism to detect and close broken connections (e.g. if the database server was restarted) and opens new connections if possible. If you are new to ResourcePool you should go to the ResourcePool::BigPicture documentation which provides the best entry point to this module. The ResourcePool itself handles always exactly equivalent connections (e.g. connections to the same server with the same user-name and password) and is therefore not able to do a load balancing. The ResourcePool::LoadBalancer is able to do a advanced load balancing across different servers and increases the overall availability by applying a failover policy if there is a server breakdown. ResourcePool->new($factory, @Options)Creates a new ResourcePool. It uses a previously created ResourcePool if possible. So if you call the new method with the same arguments twice, the second call returns the same object as the first did. This is even true if you call the new method while handling different Apache/mod_perl requests (as long as they are in the same Apache process). (This is implemented using the ResourcePool::Singleton class included in this distribution)
Using an exponential time scheme like this one, is usually the most efficient. However it is highly recommended to leave the first value always "0" since this is required to allow the ResourcePool to try to establish a new connection without delay. The number of sleeps can not be more than > - 1, if you specify more values the list is truncated. If you specify less values the list is extended using the last value for the extended elements. e.g. [0, 1] in the above example would have been extended to [0, 1, 1, 1] If you have Time::HiRes installed on your system, you can specify a fractal number of seconds. If you are doing load balancing you should use ResourcePool::LoadBalancer's SleepOnFail option instead of this one. Please see also the TIMEOUTS section for more information about timeouts. Default: [0] $pool->getReturns a resource. This resource has to be given back via the free() or fail() method. The get() method calls the precheck() method of the according resource to determine if a resource is valid. The get() method may return undef if there is no valid resource available. (e.g. because the > or the > values are reached)$pool->free($resource)Marks a resource as free. This resource will be re-used by get() calls. The free() method calls the postcheck() method of the resource to determine if the resource is valid.Returns: true on success or false if the resource doesn't belong to that pool $pool->fail($resource)Marks a resource as bad. The ResourcePool will throw this resource away and NOT return it to the pool of available connections.Returns: true on success or false if the resource doesn't belong to that pool $pool->execute($Command)Executes a ResourcePool::Command with a resource from this pool. The execute() method implements the Command design pattern from the GoF book "Design Patterns". This enables you to use the ResourcePool without using the get(), free() and fail() methods manually. Basically the execute() method obtains a resource from this pool by calling its get() method, then calling the execute() method of the supplied Command and releasing the resource afterwards.The execute method will call die() if an error happened, therefore it's best practice to wrap it in an eval{} block. The ResourcePool::Command::Execute documentation covers this in all the details and facets which are introduced by this pattern. Returns: EXAMPLESThis section includes a typical configuration for a persistent Net::LDAP pool.use ResourcePool; use ResourcePool::Factory::Net::LDAP; my $factory = ResourcePool::Factory::Net::LDAP->new("ldaphost", version => 2 ); $factory->bind('cn=Manager,dc=fatalmind,dc=com', password => 'secret'); my $pool = ResourcePool->new($factory, MaxTry => 5, SleepOnFail => [0,1,2,4] ); if ($something) { my $ldaph = $pool->get(); # do something useful with $ldaph $pool->free($ldaph); } # some code nothing to do with ldap if ($somethingdifferent) { my $ldaph = $pool->get(); # do something different with $ldaph $pool->free($ldaph); } So, lets sum up some characteristics of this example:
Examples for the execute() method can be found in the ResourcePool::Command documentation. TIMEOUTSTime to say some more words about timeouts which take place in the get() method. As you have seen above you can configure this timeouts with the > option, but thats only the half truth. As the name of the option says, thats only the time which is actually slept if an attempt to obtain a valid resource fails. But there is also the time which is needed to try to obtain a valid resource. And this time might vary in a very wide range.Imagine a situation where you have a newly created ResourcePool for a database (without >) and call the get() method for the first time. Obviously the ResourcePool has to establish a new connection to the database. But what happens if the database is down? Then the time which is needed to recognize that the database is down does also block the get() call. Thats usually not a problem if the operating system of the database server is up and only the database application is down. In that case the operating system rejects the connection actively and the connection establishment will fail very fast. But it's very different if the operating system is also not available (hardware down, network down,...), in this case it might take several seconds (or even minutes!!) before the connection establishment fails, because it has to wait for internal timeouts (which are not affected by ResourcePool). Another example is if your DNS server is down, it might also take a long time before the establishment fails. The usual DNS resolver tries more then one minute before it gives up. Therefore you have to keep in mind that the timeouts configured via > are only minimum values. If you configure a > value of one second between the attempts, than you have a guarantee the there is at least a sleep of one second before the next attempt is done. If you want to limit the overall timeouts you have two choices. 1. Use the timeout functionality of the underlaying modules. This is not always safe since the modules might use some functions which do not implement a configurable timeout or does just not use it. 2. Implement your own timeout using alarm(). Thats also not safe, since there is no guarantee that a blocking system call gets interrupted when a signal is received. But to my knowledge thats still the more reliable variant. The best way of handling this timeouts is to avoid them. For a High Availability configuration it is important fail very fast to be effective. But thats easy to say... LIMITATIONS
SEE ALSOResourcePool::LoadBalancer, ResourcePool::Resource, ResourcePool::Factory, ResourcePool::Factory::DBI, ResourcePool::Factory::Net::LDAPAUTHORCopyright (C) 2001-2009 by Markus Winand <mws@fatalmind.com> This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. POD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |