utopia-php/pools

A simple library to manage connection pools

0.4.2 2022-11-22 07:55 UTC

README

Build Status Total Downloads Discord

Utopia pools library is simple and lite library for managing long living connection pools. This library is aiming to be as simple and easy to learn and use. This library is maintained by the Appwrite team.

Although this library is part of the Utopia Framework project it is dependency free, and can be used as standalone with any other PHP project or framework.

Concepts

  • Pool - A list of long living connections. You can pop connections out and use them and push them back to the pool for reuse.
  • Connection - An object that holds a long living database or other external connection in a form of a resource. PDO object or a Redis client are examples of resources that can be used inside a connection.
  • Group - A group of multiple pools.

Getting Started

Install using composer:

composer require utopia-php/pools

Examples

use PDO;
use Utopia\Pools\Pool;
use Utopia\Pools\Group;

$pool = new Pool('mysql-pool', 1 /* number of connections */, function() {
    $host = '127.0.0.1';
    $db   = 'test';
    $user = 'root';
    $pass = '';
    $charset = 'utf8mb4';

    try {
        $pdo = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);
    } catch (\PDOException $e) {
        throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }

    return $pdo;
});

$pool->setReconnectAttempts(3); // number of attempts to reconnect
$pool->setReconnectSleep(5); // seconds to sleep between reconnect attempts

$pool->setRetryAttempts(3); // number of attempts to get connection
$pool->setRetrySleep(5); // seconds to sleep between failed pop-connection attempts

$connection = $pool->pop(); // Get a connection from the pool
$connection->getID(); // Get the connection ID
$connection->getResource(); // Get the connection resource

$pool->push($connection); // Return the connection to the pool

$pool->reclaim(); // Recalim the pool, return all active connections automatically

$pool->count(); // Get the number of available connections

$pool->isEmpty(); // Check if the pool is empty

$pool->isFull(); // Check if the pool is full

$group = new Group(); // Create a group of pools
$group->add($pool); // Add a pool to the group
$group->get('mysql-pool'); // Get a pool from the group
$group->setReconnectAttempts(3); // Set the number of reconnect attempts for all pools
$group->setReconnectSleep(5); // Set the sleep time between reconnect attempts for all pools

Reconnect and Retry

Both reconnect and retry logic is used in pop() method to handle problematic scenarios. Both allow you to configure 2 properties:

  • attempts - How many times library will retry when problem occurs
  • sleep - How long will library wait for before next retry attempt

Reconnect settings are used when your connection initialization callback throws an exception. This can occur for example, when a handshake with SQL server fails.

Retry settings are used when pool of connection is empty and there is no more connections to pop. This can occur for example, when your server supports more concurrent actions than your pool.

System Requirements

Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.

Tests

To run all unit tests, use the following Docker command:

docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests

To run static code analysis, use the following Psalm command:

docker compose exec tests vendor/bin/psalm --show-info=true

Copyright and license

The MIT License (MIT) http://www.opensource.org/licenses/mit-license.php