Skip to content

Latest commit

 

History

History
38 lines (26 loc) · 1.35 KB

README.md

File metadata and controls

38 lines (26 loc) · 1.35 KB

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.