|
| 1 | +# Distributed Pooling Extensions |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +PivotPHP v1.1.0 introduces support for distributed object pooling across multiple application instances. The core framework provides the interfaces and infrastructure, while specific coordination backends are implemented as separate extensions. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +``` |
| 10 | +┌─────────────────────────────────────────────────┐ |
| 11 | +│ Application │ |
| 12 | +├─────────────────────────────────────────────────┤ |
| 13 | +│ HighPerformanceMode │ |
| 14 | +├─────────────────────────────────────────────────┤ |
| 15 | +│ DistributedPoolManager │ |
| 16 | +├─────────────────────────────────────────────────┤ |
| 17 | +│ CoordinatorInterface │ |
| 18 | +├─────────────────────────────────────────────────┤ |
| 19 | +│ NoOpCoordinator │ RedisCoordinator (ext) │ |
| 20 | +│ │ EtcdCoordinator (ext) │ |
| 21 | +│ │ ConsulCoordinator (ext) │ |
| 22 | +└─────────────────────────────────────────────────┘ |
| 23 | +``` |
| 24 | + |
| 25 | +## Built-in Support |
| 26 | + |
| 27 | +### NoOpCoordinator |
| 28 | +- Default implementation when no external coordination is configured |
| 29 | +- Provides single-instance operation |
| 30 | +- Zero external dependencies |
| 31 | +- Automatically used as fallback |
| 32 | + |
| 33 | +## Available Extensions |
| 34 | + |
| 35 | +### Redis Pool Extension |
| 36 | +```bash |
| 37 | +composer require pivotphp/redis-pool |
| 38 | +``` |
| 39 | + |
| 40 | +Features: |
| 41 | +- Redis-based coordination |
| 42 | +- Leader election |
| 43 | +- Distributed queues |
| 44 | +- Shared state management |
| 45 | + |
| 46 | +Configuration: |
| 47 | +```php |
| 48 | +[ |
| 49 | + 'distributed' => [ |
| 50 | + 'enabled' => true, |
| 51 | + 'coordination' => 'redis', |
| 52 | + 'redis' => [ |
| 53 | + 'host' => 'localhost', |
| 54 | + 'port' => 6379, |
| 55 | + 'password' => null, |
| 56 | + 'database' => 0, |
| 57 | + ] |
| 58 | + ] |
| 59 | +] |
| 60 | +``` |
| 61 | + |
| 62 | +### etcd Pool Extension (Coming Soon) |
| 63 | +```bash |
| 64 | +composer require pivotphp/etcd-pool |
| 65 | +``` |
| 66 | + |
| 67 | +### Consul Pool Extension (Coming Soon) |
| 68 | +```bash |
| 69 | +composer require pivotphp/consul-pool |
| 70 | +``` |
| 71 | + |
| 72 | +## Creating Custom Extensions |
| 73 | + |
| 74 | +### 1. Implement the Coordinator |
| 75 | + |
| 76 | +```php |
| 77 | +namespace YourVendor\YourExtension; |
| 78 | + |
| 79 | +use PivotPHP\Core\Pool\Distributed\Coordinators\CoordinatorInterface; |
| 80 | + |
| 81 | +class CustomCoordinator implements CoordinatorInterface |
| 82 | +{ |
| 83 | + public function connect(): bool |
| 84 | + { |
| 85 | + // Connect to your backend |
| 86 | + } |
| 87 | + |
| 88 | + public function set(string $key, mixed $value, ?int $ttl = null): bool |
| 89 | + { |
| 90 | + // Store value with optional TTL |
| 91 | + } |
| 92 | + |
| 93 | + public function get(string $key): mixed |
| 94 | + { |
| 95 | + // Retrieve value |
| 96 | + } |
| 97 | + |
| 98 | + // ... implement all interface methods |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### 2. Register with Service Provider |
| 103 | + |
| 104 | +```php |
| 105 | +namespace YourVendor\YourExtension; |
| 106 | + |
| 107 | +use PivotPHP\Core\Providers\ServiceProvider; |
| 108 | + |
| 109 | +class CustomPoolServiceProvider extends ServiceProvider |
| 110 | +{ |
| 111 | + public function register(): void |
| 112 | + { |
| 113 | + // Register coordinator factory |
| 114 | + $this->app->bind('pool.coordinator.custom', function($app) { |
| 115 | + return new CustomCoordinator($app->config('distributed')); |
| 116 | + }); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### 3. Package Configuration |
| 122 | + |
| 123 | +Create `composer.json`: |
| 124 | +```json |
| 125 | +{ |
| 126 | + "name": "yourvendor/pivotphp-custom-pool", |
| 127 | + "description": "Custom distributed pool coordinator for PivotPHP", |
| 128 | + "require": { |
| 129 | + "pivotphp/core": "^1.1" |
| 130 | + }, |
| 131 | + "autoload": { |
| 132 | + "psr-4": { |
| 133 | + "YourVendor\\YourExtension\\": "src/" |
| 134 | + } |
| 135 | + }, |
| 136 | + "extra": { |
| 137 | + "pivotphp": { |
| 138 | + "providers": [ |
| 139 | + "YourVendor\\YourExtension\\CustomPoolServiceProvider" |
| 140 | + ] |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Usage Example |
| 147 | + |
| 148 | +```php |
| 149 | +use PivotPHP\Core\Performance\HighPerformanceMode; |
| 150 | + |
| 151 | +// Enable high performance mode with distributed pooling |
| 152 | +HighPerformanceMode::enable([ |
| 153 | + 'distributed' => [ |
| 154 | + 'enabled' => true, |
| 155 | + 'coordination' => 'redis', // or 'custom', 'etcd', etc. |
| 156 | + // ... backend-specific config |
| 157 | + ] |
| 158 | +]); |
| 159 | +``` |
| 160 | + |
| 161 | +## Fallback Behavior |
| 162 | + |
| 163 | +If a coordinator extension is not available or fails to initialize: |
| 164 | +1. The system logs a warning |
| 165 | +2. Falls back to NoOpCoordinator |
| 166 | +3. Continues operation in single-instance mode |
| 167 | +4. No application errors occur |
| 168 | + |
| 169 | +## Performance Considerations |
| 170 | + |
| 171 | +- Distributed coordination adds network latency |
| 172 | +- Use local pools for hot objects |
| 173 | +- Configure appropriate sync intervals |
| 174 | +- Monitor network traffic between instances |
| 175 | + |
| 176 | +## Best Practices |
| 177 | + |
| 178 | +1. **Start Simple**: Use NoOpCoordinator for single-instance deployments |
| 179 | +2. **Add When Needed**: Only enable distributed pooling for multi-instance deployments |
| 180 | +3. **Monitor Performance**: Track coordination overhead |
| 181 | +4. **Configure Timeouts**: Set appropriate timeouts for network operations |
| 182 | +5. **Handle Failures**: Design for network partitions and coordinator failures |
0 commit comments