@@ -9,29 +9,88 @@ The library includes two implementations of the pool:
99- ` ResourcePool ` – provides basic pooling logic, including borrowing and returning resources.
1010- ` AsyncResourcePool ` – a [ ReactPHP] ( https://reactphp.org/ ) -based implementation that adds resource retry functionality to the basic features.
1111
12+ This library is resource-agnostic, meaning you can use it with any type of resource, such as database connections, file handles, or network sockets.
13+
14+ ## Installation
15+
16+ You can install the library using Composer:
17+
18+ ``` bash
19+ composer require shado/php-resource-pool
20+ ```
21+
1222## Requirements
1323
1424- PHP >= 8.1
1525
1626> [ !TIP]
1727> Thanks to Fibers, you can freely use the ReactPHP-based implementation in your traditional PHP projects.
28+ >
29+ > New to ReactPHP? Check out the [ ReactPHP documentation] ( https://reactphp.org/ ) .
1830
1931## Example
2032
33+ ### Basic usage of the ` ResourcePool `
34+
2135``` php
2236$factory = function (\Shado\ResourcePool\FactoryController $controller) {
2337 $newConnection = new DbConnection();
24- $newConnection->onClose($controller->detach(...)); // When connection closes, detach it from the pool
38+ $newConnection->onClose($controller->detach(...));
39+ $newConnection->onError($controller->detach(...));
2540 return $newConnection;
2641};
2742
28- $pool = new \Shado\ResourcePool\ResourcePool($factory, 10);
43+ $pool = new \Shado\ResourcePool\ResourcePool($factory, limit: 10);
2944
3045$connection = $pool->borrow(); // `$connection` is ready to use :)
46+
47+ try {
3148// $connection->query(...);
32- $pool->return($connection);
49+ } finally {
50+ $pool->return($connection);
51+ }
52+ ```
53+
54+ The factory function is responsible for creating new resources. It receives a FactoryController which can be used to
55+ detach the resource from the pool (e.g., when it closes or an error occurs).
56+
57+ In this example, the pool lazily creates new resources using the factory function and limits the number
58+ of concurrently maintained resources to 10.
59+
60+ If all resources are in use, the ` borrow ` method throws an ` Shado\ResourcePool\Exceptions\ResourceSelectingException ` .
61+
62+ ### Using the retry functionality with ` AsyncResourcePool `
63+
64+ ``` php
65+ $factory = function (\Shado\ResourcePool\FactoryController $controller) {
66+ $newConnection = new DbConnection();
67+ $newConnection->onClose($controller->detach(...));
68+ $newConnection->onError($controller->detach(...));
69+ return $newConnection;
70+ };
71+
72+ $pool = new \Shado\ResourcePool\AsyncResourcePool(
73+ new \Shado\ResourcePool\ResourcePool($factory, limit: 10),
74+ retryingTimeout: null,
75+ retryingDelay: 0.01
76+ );
77+
78+ $connection = $pool->borrow(); // `$connection` is ready to use :)
79+ // ...or you can make use of the Promise:
80+ // $connection = \React\Async\await($pool->borrowAsync());
81+
82+ try {
83+ // $connection->query(...);
84+ } finally {
85+ $pool->return($connection);
86+ }
3387```
3488
89+ After wrapping the ` ResourcePool ` with the ` AsyncResourcePool ` , you can use the retry functionality.
90+
91+ If all resources are in use, the ` borrow ` method waits up to the specified ` retryingTimeout ` (or indefinitely if set to ` null ` )
92+ and retry borrowing a resource after the specified ` retryingDelay ` .
93+
3594## At the end...
3695- Run tests: ` ./vendor/bin/phpunit tests ` .
3796- Feel free to create an issue or submit your PR! 🤗
0 commit comments