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
);
// 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) {}