Skip to content

A PHP library providing resource pooling support, commonly used as a database connection pool.

Notifications You must be signed in to change notification settings

szado/php-resource-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shado/php-resource-pool

A PHP library providing resource pooling support, commonly used as a database connection pool.

Resource pooling allows you to easily manage a range of concurrently maintained resources. You can define how many of them can be created or which algorithm should be used for selecting the next resource from the pool.

The library includes two implementations of the pool:

  • ResourcePool – provides basic pooling logic, including borrowing and returning resources.
  • AsyncResourcePool – a ReactPHP-based implementation that adds resource retry functionality to the basic features.

Requirements

  • PHP >= 8.1

Tip

Thanks to Fibers, you can freely use the ReactPHP-based implementation in your traditional PHP projects.

Example

$factory = function (\Shado\ResourcePool\FactoryController $controller) {
    $newConnection = new DbConnection();
    $newConnection->onClose($controller->detach(...)); // When connection closes, detach it from the pool
    return $newConnection;
};

$pool = new \Shado\ResourcePool\ResourcePool($factory, 10);

$connection = $pool->borrow(); // `$connection` is ready to use :)
// $connection->query(...);
$pool->return($connection);

At the end...

  • Run tests: ./vendor/bin/phpunit tests.
  • Feel free to create an issue or submit your PR! 🤗
  • Licence: MIT.

About

A PHP library providing resource pooling support, commonly used as a database connection pool.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages