PHP code example of phpdot / pool

1. Go to this page and download the library: Download phpdot/pool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpdot / pool example snippets


interface ConnectorInterface
{
    public function connect(): object;
    public function isAlive(object $connection): bool;
    public function close(object $connection): void;
}

// Example: MongoDB connector (lives in phpdot/dot)
final class MongoConnector implements ConnectorInterface
{
    public function connect(): object
    {
        $connection = new Connection($this->config);
        $connection->connect();
        return $connection;
    }

    public function isAlive(object $connection): bool
    {
        return $connection->isConnected(); // local check, no network
    }

    public function close(object $connection): void
    {
        $connection->close();
    }
}

use PHPdot\Pool\Pool;
use PHPdot\Pool\PoolConfig;

// Created in onWorkerStart (after fork)
$pool = new Pool($connector, new PoolConfig(
    minConnections: 2,
    maxConnections: 10,
));

$pool->init(); // pre-creates minConnections, starts timers

// ... application runs, coroutines borrow and release ...

$pool->close(); // onWorkerStop — closes everything

$connection = $pool->borrow();

$pool->release($connection);

$pool->discard($connection);

$stats = $pool->stats();

$stats->active;       // currently borrowed
$stats->idle;         // sitting in Channel
$stats->total;        // active + idle
$stats->borrowCount;  // total borrows since init
$stats->releaseCount;
$stats->discardCount;
$stats->createCount;
$stats->closeCount;
$stats->timeoutCount;
$stats->waitingCount; // coroutines waiting for a connection right now

$pool->close();

new PoolConfig(
    minConnections: 2,                  // pre-created, never shrink below
    maxConnections: 10,                 // hard limit per worker
    borrowTimeout: 3.0,                 // seconds to wait when exhausted
    maxIdleTime: 300.0,                 // seconds before idle cleanup (0.0 = disabled)
    idleCheckInterval: 30.0,            // seconds between cleanup runs
    heartbeatInterval: 0.0,             // seconds between heartbeat (0.0 = disabled)
    validateOnBorrowAfterIdle: 5.0,     // call isAlive() on borrow when idle ≥ this many seconds
                                        //   0.0 = always validate; negative = disabled
    validateOnReturn: false,            // call isAlive() on release; close (don't re-pool) dead
);

new PoolConfig(
    minConnections: 2,
    maxConnections: 10,
    maxIdleTime: 300.0,       // close after 5 min idle
    idleCheckInterval: 30.0,  // check every 30s
);

new PoolConfig(heartbeatInterval: 60.0);

new PoolConfig(
    validateOnBorrowAfterIdle: 5.0,   // ping if idle ≥ 5s
    // 0.0  = always validate (every borrow)
    // -1.0 = disabled
);

new PoolConfig(validateOnReturn: true);

// Swoole mode (in onWorkerStart)
$pool = new Pool(new MongoConnector($config), new PoolConfig(...));
$pool->init();

$container->scoped(Connection::class, function () use ($pool) {
    $connection = $pool->borrow();
    Coroutine::defer(fn () => $pool->release($connection));
    return $connection;
});

// FPM mode — no pool, direct binding
$container->singleton(Connection::class, fn () => new Connection($config));

public function __construct(private Connection $mongodb) {}

src/
├── ConnectorInterface.php      # How to create, check, close connections
├── Pool.php                    # Channel + Timers + borrow/release/discard
├── PoolConfig.php              # 6 readonly config properties
├── PoolStats.php               # 10 readonly monitoring counters
├── PooledItem.php              # Internal: connection + lastReleasedAt
└── Exception/
    ├── PoolException.php       # Base
    ├── BorrowTimeoutException.php
    └── PoolClosedException.php