create-object-pool
Object Pool Pattern Generator
Creates Object Pool pattern infrastructure for managing reusable expensive objects.
When to Use
| Scenario | Example |
|---|---|
| Expensive creation | Database connections |
| Limited resources | HTTP client handles |
| Connection reuse | Socket connections |
| Memory management | Large object caching |
Component Characteristics
PoolInterface
- Acquire/release semantics
- Pool lifecycle management
- Capacity configuration
Pool Implementation
- Manages available objects
- Creates on demand
- Recycles returned objects
Poolable Objects
- Resettable state
- Validate before reuse
- Track usage metrics
Generation Process
Step 1: Generate Core Pool Components
Path: src/Infrastructure/Pool/
PoolInterface.php— Generic pool contract with acquire/releasePoolConfig.php— Configuration value object (min/max size, timeouts)PoolableInterface.php— Contract for poolable objects (reset, isValid, close)ObjectPool.php— Generic pool implementationPooledObject.php— Wrapper tracking usage metricsPoolExhaustedException.php— Exception for exhausted poolInvalidPoolObjectException.php— Exception for invalid objects
Step 2: Generate Specialized Pool (if needed)
Path: src/Infrastructure/{Domain}/
{Type}Pool.php— Specialized pool (ConnectionPool, HttpClientPool)Poolable{Type}.php— Poolable implementation for specific resource
Step 3: Generate Tests
Path: tests/Unit/Infrastructure/Pool/
ObjectPoolTest.php— Core pool functionality tests{Type}PoolTest.php— Specialized pool tests
File Placement
| Component | Path |
|---|---|
| Pool Interface | src/Infrastructure/Pool/ |
| Pool Implementation | src/Infrastructure/Pool/ |
| Specialized Pools | src/Infrastructure/{Domain}/ |
| Unit Tests | tests/Unit/Infrastructure/Pool/ |
Naming Conventions
| Component | Pattern | Example |
|---|---|---|
| Interface | PoolInterface |
PoolInterface |
| Implementation | ObjectPool |
ObjectPool |
| Poolable Interface | PoolableInterface |
PoolableInterface |
| Config | PoolConfig |
PoolConfig |
| Wrapper | PooledObject |
PooledObject |
| Specialized Pool | {Type}Pool |
ConnectionPool |
| Test | {ClassName}Test |
ObjectPoolTest |
Quick Template Reference
PoolInterface
/**
* @template T
*/
interface PoolInterface
{
/** @return T */
public function acquire(): mixed;
/** @param T $object */
public function release(mixed $object): void;
public function getAvailableCount(): int;
public function getActiveCount(): int;
public function getMaxSize(): int;
public function clear(): void;
}
PoolableInterface
interface PoolableInterface
{
public function reset(): void;
public function isValid(): bool;
public function close(): void;
}
PoolConfig
final readonly class PoolConfig
{
public function __construct(
public int $minSize = 0,
public int $maxSize = 10,
public int $maxWaitTimeMs = 5000,
public int $idleTimeoutSeconds = 300,
public bool $validateOnAcquire = true,
public bool $validateOnRelease = false
);
public static function default(): self;
public static function forDatabase(): self;
public static function forHttpClients(): self;
}
Usage Example
// Create pool
$pool = new ObjectPool(
name: 'database',
factory: fn() => $connectionFactory->create(),
config: PoolConfig::forDatabase(),
logger: $logger
);
// Acquire and release
$connection = $pool->acquire();
try {
$result = $connection->query('SELECT ...');
} finally {
$pool->release($connection);
}
// Or use helper
$result = $connectionPool->execute(fn($conn) => $conn->query('SELECT ...'));
Anti-patterns to Avoid
| Anti-pattern | Problem | Solution |
|---|---|---|
| No Validation | Returning broken objects | Validate on acquire |
| No Reset | State leaks between uses | Call reset() on release |
| No Timeout | Infinite wait | Set maxWaitTime |
| Unbounded Pool | Memory exhaustion | Set maxSize |
| Not Releasing | Pool exhaustion | Use try/finally |
| Wrong Scope | Per-request pools | Use singleton/shared pool |
References
For complete PHP templates and examples, see:
references/templates.md— All component templatesreferences/examples.md— ConnectionPool, HttpClientPool examples and tests
More from dykyi-roman/awesome-claude-code
psr-overview-knowledge
PHP Standards Recommendations (PSR) overview knowledge base. Provides comprehensive reference for all accepted PSRs including PSR-1,3,4,6,7,11,12,13,14,15,16,17,18,20. Use for PSR selection decisions and compliance audits.
22detect-code-smells
Detects code smells in PHP codebases. Identifies God Class, Feature Envy, Data Clumps, Long Parameter List, Long Method, Primitive Obsession, Message Chains, Inappropriate Intimacy. Generates actionable reports with refactoring recommendations.
15clean-arch-knowledge
Clean Architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Clean Architecture and Hexagonal Architecture audits.
15ddd-knowledge
DDD architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Domain-Driven Design audits.
14testing-knowledge
Testing knowledge base for PHP 8.4 projects. Provides testing pyramid, AAA pattern, naming conventions, isolation principles, DDD testing guidelines, and PHPUnit patterns.
12bug-root-cause-finder
Root cause analysis methods for PHP bugs. Provides 5 Whys technique, fault tree analysis, git bisect guidance, and stack trace parsing.
12