|
NAMEResourcePool::ExtensionGuide - How to write extensions for ResourcePool DESCRIPTIONThis document describes how to build extensions for ResourcePool. As you will see you need only a few lines of code to create your own resource type. Afterwards you will be able to use this resource for ResourcePool and ResourcePool::LoadBalancer. A resource by means of ResourcePool is some perl object which could benefit from the feature set provided by ResourcePool and ResourcePool::LoadBalancer. The reason why I use such a meaningless sentence to describe is what a resource is, is that there is no other clear separation which could be used. In many cases a resource is something which has a connection to a server like a database connection. But ResourcePool is not limited to such resources, to be honest I have some plans for ResourcePool which will result in the ability to use ResourcePool for LWP requests (including load balancing and fail over) and so on. But that's not finished yet. If it is easier for you, think of a resource as a connection to a server. Every ResourcePool resource consists of two major parts:
If you have implemented this two packages you are nearly done. The only missing parts are: documentation, tests and uploading your extension to CPAN. Resource adapterA resource adapter has to provide a generic interface to ResourcePool for your resource. This interface is used to interact with the resource. Some of the methods are optional, you can just skip them if you have carefully derived your own resource adapter from ResourcePool::Resource which will provide some defaults for you. Each resource adapter manages exactly one resource. This means for example one database connection or whatever your resource might be. The pooling and management of more resources is done by ResourcePool and does not require any attention of your resource adapter. I will describe the methods of this class in the order in which they are used by ResourcePool.
The normal life cycle of a resource adapter ends here. The remaining method are used to handle failure...
The following simplified call-graph tries to demonstrate the most important important processes which take place with your resource. Please note that this is reduced to the processes relevant to the resource adapter class. Client . ResourcePool and . resource adapter
code . LoadBalancer .
. .
--------> get() ----------------------> precheck() -----------+
. . |
. +------------------------<-----------------------+
. | on failure .
. +------------------------> fail_close() ---------+
. | tries another . |
. | available resource <---------------------------+
. | .
. | on success .
. +------------------------> get_plain_resource() -+
. . |
--------<-----------------------------<-----------------------+
. .
. .
--------> free() ---------------------> postcheck() ----------+
. . |
. +------------------------<-----------------------+
. | on failure .
. +------------------------> fail_close() ---------|
. | on success .
. +--> Add back to pool .
. .
. .
--------> fail() ---------------------> fail_close() ---------|
. .
(the free() and fail() methods do return meaningful values which are in not directly related to the resource adapter implementation.) The diagram is separated into three sections: the leftmost section represents the user code, this is the software which uses ResourcePool; the middle one represents the ResourcePool core. For this diagram it makes no difference if you are using ResourcePool::LoadBalancer as well; the right most section represents the resource adapter of the extension. FactoriesThe purpose of a factory is to store information which is required to create a resource. The factory is constructed and configured by the user code and then passed to the ResourcePool on construction. The ResourcePool::Factory class acts as base for all factories. Besides the constructor the ResourcePool::Factory defines only two methods which should be overloaded.
Naming conventionsAs you have learned every ResourcePool consists of two parts. The namespace where you should place this two packages are ResourcePool::Factory and ResourcePool::Resource. Below this two namespaces you should add the complete perl-class name for which resource your extension is. E.g. The Net::LDAP factory and resource adapter are called ResourcePool::Factory::Net::LDAP and ResourcePool::Resource::Net::LDAP. If you upload you extension to CPAN you should name the package according to your resource adapter name. EXAMPLESThe reality delivers the best examples. Please have a look into the implementation of the already existing extensions like DBI or Net::LDAP. SEE ALSOResourcePool, ResourcePool::Resource, ResourcePool::Factory AUTHOR Copyright (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.
|