snapflowio/pooling

A simple and lightweight library to manage long lived connection pools.

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/snapflowio/pooling

v0.1.0 2025-12-15 18:51 UTC

This package is auto-updated.

Last update: 2025-12-15 18:55:25 UTC


README

A simple and lightweight library to manage long lived connection pools.

Installation

composer require snapflow/pooling

Quick Start

<?php

use Snapflow\Pooling\Pooling;

// Create a connection pool
$pool = new Pooling(
    name: 'database',
    size: 10,
    init: fn () => new PDO('mysql:host=localhost;dbname=mydb', 'user', 'pass')
);

// Use a connection with automatic lifecycle management
$result = $pool->use(function (PDO $pdo) {
    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
    $stmt->execute([1]);
    return $stmt->fetch();
});

// Configure retry/reconnect behavior
$pool
    ->setRetryAttempts(5)        // Retry 5 times if pool is empty
    ->setRetrySleep(2)            // Wait 2 seconds between retries
    ->setReconnectAttempts(3)     // Retry 3 times if connection fails
    ->setReconnectSleep(1);       // Wait 1 second between reconnects

// Monitor pool health
$stats = $pool->getStats();
// Returns: ['name', 'size', 'available', 'active', 'idle', 'uninitialized']

// Optional: Cleanup resources when connections are destroyed
$poolWithCleanup = new Pooling(
    name: 'redis',
    size: 5,
    init: fn () => new Redis(),
    cleanup: fn (Redis $redis) => $redis->close()
);

License

This library is available under the MIT License.

Copyright

Copyright (c) 2025 Snapflow