PHP code example of utopia-php / pools

1. Go to this page and download the library: Download utopia-php/pools 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/ */

    

utopia-php / pools example snippets


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

$pool = new Pool(
    adapter: new Swoole,
    name: 'mysql-pool',
    size: 8,
    init: fn () => new PDO('mysql:host=127.0.0.1;dbname=test;charset=utf8mb4', 'root', ''),
    timeout: 2.0,
);

// Preferred: the pool hands the resource to the callback and takes it back
// afterwards, discarding it if the callback threw.
$rows = $pool->use(fn (PDO $pdo) => $pdo->query('SELECT 1')->fetchAll());

// Manual lifetime, when `use()` does not fit.
$connection = $pool->pop();
$connection->id;         // "mysql-pool-6885a1f2c4e19"
$connection->resource;   // the PDO instance
$connection->reclaim();  // return it for reuse
$connection->destroy();  // or discard it and free the capacity

$pool->count();     // connections a caller could still obtain
$pool->isEmpty();   // none available without one being returned first
$pool->isFull();    // nothing checked out

$group = new Group;
$group->add($pool);
$group->get('mysql-pool');
$group->use(['mysql-pool'], fn (PDO $pdo) => $pdo->query('SELECT 1'));

$pool = new Pool(
    adapter: new Swoole,
    name: 'mysql-pool',
    size: 8,
    init: fn () => $breaker->call(fn () => new PDO(/* ... */)),
    timeout: 2.0,
);
bash
composer