highperapp/zero-downtime
Composer 安装命令:
composer require highperapp/zero-downtime
包简介
Zero-downtime deployment and hot reload for HighPer Framework with WebSocket connection migration
README 文档
README
Complete zero-downtime deployment and hot reload system for HighPer Framework with WebSocket connection migration support across HTTP/1.1, HTTP/2 (RFC 8441), and HTTP/3 protocols.
🚀 Features
WebSocket Protocol Support
- ✅ HTTP/1.1 WebSocket - RFC 6455 compliant via Amp WebSocket
- ✅ HTTP/2 WebSocket - RFC 8441 implementation with CONNECT method
- ✅ HTTP/3 WebSocket - QUIC-based WebSocket tunneling
Zero-Downtime Strategies
- 🔵🟢 Blue-Green Deployment - Complete worker replacement
- 🔄 Rolling Deployment - Gradual worker replacement
- 🤝 Socket Handoff - Master process handoff with FD transfer
Connection Migration
- 📦 State Serialization - Protocol-specific connection state capture
- 🔄 Cross-Protocol Support - HTTP/1.1 ↔ HTTP/2 ↔ HTTP/3 migration
- ⚡ Compression & Encryption - Optimized state transfer
Request Buffering
- 🔄 Proxy-Level Queuing - Prevents request loss during transitions
- 👥 Worker-Specific Buffers - Granular request routing
- 📊 Overflow Strategies - Reject, evict oldest, or compress
- 🏷️ Priority Headers - Request prioritization support
📋 Installation
composer require highperapp/zero-downtime
⚙️ Configuration
Environment Variables
# Zero-Downtime Configuration ENABLE_ZERO_DOWNTIME=true ZERO_DOWNTIME_STRATEGY=blue-green # blue-green|rolling|socket-handoff # Connection Migration ENABLE_CONNECTION_MIGRATION=true CONNECTION_SERIALIZATION_FORMAT=json CONNECTION_COMPRESSION=true CONNECTION_STATE_PATH=/tmp/highper-connection-states # Request Buffering ENABLE_REQUEST_BUFFERING=true REQUEST_BUFFER_SIZE_MB=10 REQUEST_BUFFER_TIME=30 REQUEST_OVERFLOW_STRATEGY=reject # reject|oldest|compress # Worker Management WORKER_COUNT=4 GRACEFUL_SHUTDOWN_TIMEOUT=30 AUTO_RESTART_WORKERS=true MAX_WORKER_MEMORY_MB=128 # Socket Handoff HANDOFF_SOCKET_PATH=/tmp/highper-socket-handoff HANDOFF_TIMEOUT=30 SOCKET_REUSE_PORT=true
🔧 Usage
Basic Integration
use HighPerApp\HighPer\ZeroDowntime\ZeroDowntimeBootstrap; use HighPerApp\HighPer\Framework\Contracts\ApplicationInterface; use Psr\Log\LoggerInterface; // Initialize in your application bootstrap $zeroDowntime = new ZeroDowntimeBootstrap($app, $logger, [ 'reload_strategy' => 'blue-green', 'enable_connection_migration' => true, 'enable_request_buffering' => true, 'worker_count' => 4, 'graceful_shutdown_timeout' => 30 ]); // Trigger zero-downtime reload posix_kill(posix_getpid(), SIGHUP); // Get reload result $result = $zeroDowntime->performZeroDowntimeReload(); echo "Migrated {$result['migrated_connections']} connections"; echo "Replayed {$result['replayed_requests']} requests";
Service Provider Integration
The package includes a service provider for automatic integration:
// In your application configuration 'providers' => [ HighPerApp\HighPer\ZeroDowntime\ZeroDowntimeServiceProvider::class, ]
Manual Component Usage
use HighPerApp\HighPer\ZeroDowntime\ConnectionMigration\ConnectionStateSerializer; use HighPerApp\HighPer\ZeroDowntime\RequestQueue\ProxyRequestQueue; use HighPerApp\HighPer\WebSocket\WebSocketConnection; use HighPerApp\HighPer\Websockets\Http2WebSocketConnection; // Connection serialization with active state management $serializer = new ConnectionStateSerializer($logger, [ 'compression_enabled' => true, 'state_storage_path' => '/tmp/highper-connection-states' ]); // Register connections $serializer->registerConnection('conn1', $webSocketConnection, 'http1_websocket'); $serializer->registerConnection('conn2', $http2WebSocketConnection, 'http2_websocket'); // Manage connection states for zero-downtime optimization $serializer->markConnectionActive('conn1'); // O(1) performance $serializer->markConnectionIdle('conn2'); // Serialize only active connections for priority migration $activeStates = $serializer->serializeActiveConnections(); echo "Active connections: " . $serializer->getActiveConnectionCount(); // Serialize all connections $allStates = $serializer->serializeAllConnections(); // Migrate connections to new workers $workers = [['pid' => 1234], ['pid' => 1235]]; $migrationResult = $serializer->migrateConnectionsToWorkers($workers, $activeStates); // Request buffering with worker-specific handling $queue = new ProxyRequestQueue($logger, [ 'max_buffer_size_mb' => 10, 'max_buffer_time_seconds' => 30, 'overflow_strategy' => 'reject', 'priority_headers' => ['x-priority', 'x-urgent'] ]); // Global request buffering $queue->startBuffering(); $queue->bufferRequest([ 'method' => 'POST', 'uri' => '/api/data', 'headers' => ['x-priority' => '1'], 'body' => json_encode(['data' => 'important']) ]); // Worker-specific buffering $workerPid = 1234; $queue->startWorkerBuffering($workerPid); $queue->bufferRequest($request, $workerPid); // Replay buffered requests $replayResult = $queue->replayBufferedRequests($workers); echo "Replayed {$replayResult['replayed_requests']} requests in {$replayResult['replay_time_ms']}ms"; // Get queue statistics $stats = $queue->getStats(); echo "Buffer utilization: {$stats['global_buffer']['size_bytes']} bytes";
🌐 WebSocket Migration
HTTP/2 WebSocket (RFC 8441)
use EaseAppPHP\HighPer\Websockets\Http2WebSocketHandler; $handler = new Http2WebSocketHandler($logger, [ 'enable_compression' => true, 'max_frame_size' => 65536 ]); // Handles CONNECT method with :protocol pseudo-header
Connection State Serialization
// Automatic protocol detection and serialization $connectionStates = $serializer->serializeAllConnections(); // Each state includes: // - Connection ID and protocol type // - Remote address and attributes // - Protocol-specific state (HTTP/2 stream ID, HTTP/3 QUIC state) // - Frame buffers and compression context
🚦 Deployment Strategies
Blue-Green Deployment
# Trigger blue-green reload kill -HUP <master_pid>
- Start request queuing - Buffer incoming requests
- Serialize connections - Capture all WebSocket states
- Spawn green workers - New worker processes
- Transfer sockets - Hand off listening sockets
- Migrate connections - Restore WebSocket states
- Replay requests - Process buffered requests
- Terminate blue workers - Clean shutdown
Rolling Deployment
Individual worker replacement with minimal disruption:
# Set rolling strategy export ZERO_DOWNTIME_STRATEGY=rolling kill -HUP <master_pid>
Socket Handoff
Master process replacement with complete state transfer:
# Set socket handoff strategy export ZERO_DOWNTIME_STRATEGY=socket-handoff kill -HUP <master_pid>
🏥 Health Monitoring
Health Check Endpoints
# Zero-downtime status GET /_zero-downtime/status # Test connection migration POST /_zero-downtime/test-migration # Flush request queue POST /_zero-downtime/flush-queue
Statistics API
$stats = $zeroDowntime->getStats(); /* { "reload_in_progress": false, "strategy": "blue-green", "active_connections": 150, "buffered_requests": 0, "worker_stats": {...}, "socket_stats": {...} } */
🧪 Testing
Run the comprehensive test suite:
# Install dependencies composer install # Run all tests ./vendor/bin/phpunit # Run specific test groups ./vendor/bin/phpunit --group unit ./vendor/bin/phpunit --group integration ./vendor/bin/phpunit --group performance ./vendor/bin/phpunit --group concurrency # Run with coverage ./vendor/bin/phpunit --coverage-html coverage # Run static analysis ./vendor/bin/phpstan analyse src --level=max # Fix code style ./vendor/bin/php-cs-fixer fix
Test Coverage
Unit Tests (43 tests)
- ✅ ConnectionStateSerializer - Active/idle state management
- ✅ ProxyRequestQueue - Request buffering and prioritization
- ✅ ZeroDowntimeBootstrap - Deployment orchestration
- ✅ Worker management and socket handling
Integration Tests
- ✅ End-to-end zero-downtime reload workflows
- ✅ Connection migration with request buffering coordination
- ✅ Blue-green deployment strategy validation
- ✅ Health monitoring and graceful shutdown
Performance Tests
- ✅ 1000+ connection serialization (< 1 second)
- ✅ 10,000+ request buffering (< 5 seconds)
- ✅ Memory stability during extended operations
- ✅ Linear scaling validation
Concurrency Tests
- ✅ Thread-safe connection registration
- ✅ Race condition protection
- ✅ Concurrent reload isolation
- ✅ Worker handoff atomicity
🎛️ Advanced Configuration
Custom Connection Migration
// Register custom connection types $serializer->registerConnection('custom_conn', $connection, 'custom_protocol'); // Implement custom serialization class CustomConnectionSerializer extends ConnectionStateSerializer { protected function serializeCustomProtocolState($connection): array { // Custom serialization logic return [ 'custom_state' => $connection->getCustomState(), 'protocol_version' => $connection->getProtocolVersion() ]; } }
Request Priority Headers
# Configure priority headers export REQUEST_PRIORITY_HEADERS=x-priority,x-urgent,x-critical # In requests curl -H "x-priority: 1" -H "x-urgent: true" http://localhost:8080/api
Worker Auto-Restart
# Configure auto-restart limits export AUTO_RESTART_WORKERS=true export WORKER_RESPAWN_LIMIT=5 export WORKER_RESPAWN_WINDOW=300
🔒 Security Considerations
Connection State Security
// Enable encryption for sensitive connection data $serializer = new ConnectionStateSerializer($logger, [ 'encryption_enabled' => true, 'encryption_key' => getenv('CONNECTION_STATE_KEY'), 'compression_enabled' => true ]);
Socket Path Permissions
# Secure socket handoff directory sudo mkdir -p /var/run/highper sudo chown www-data:www-data /var/run/highper sudo chmod 750 /var/run/highper # Set in configuration export HANDOFF_SOCKET_PATH=/var/run/highper/socket-handoff
Request Buffer Limits
// Configure secure buffer limits $queue = new ProxyRequestQueue($logger, [ 'max_buffer_size_mb' => 10, // Prevent memory exhaustion 'max_requests_per_buffer' => 1000, // Limit buffer size 'max_buffer_time_seconds' => 30, // Prevent indefinite buffering 'buffer_to_disk' => false // Avoid disk-based attacks ]);
Worker Process Isolation
# Configure process limits export MAX_WORKER_MEMORY_MB=128 export WORKER_CPU_LIMIT=80 export WORKER_FILE_DESCRIPTOR_LIMIT=1024 # Use systemd for additional isolation sudo systemctl edit highper-app --force
📊 Performance Metrics
Benchmarked Performance
Connection Migration Performance
HTTP/1.1 WebSocket: ~1.0ms per connection (baseline)
HTTP/2 WebSocket: ~1.5ms per connection (+50% overhead)
HTTP/3 WebSocket: ~2.0ms per connection (+100% overhead)
Active Connection Lookup: O(1) - ~0.001ms per lookup
Batch Migration (1000 conns): ~1.2 seconds total
Request Buffering Performance
Memory Usage: ~2KB per buffered request
Buffering Rate: ~50,000 requests/second
Serialization Rate: ~281,000 connections/second
Replay Speed: ~10,000 requests/second
Priority Sorting: ~500ms for 5,000 requests
Zero-Downtime Reload Metrics
Blue-Green Reload: ~2-5 seconds (depends on connection count)
Rolling Reload: ~10-30 seconds (gradual replacement)
Socket Handoff: ~1-3 seconds (fastest method)
Memory Overhead: <50MB during reload
Connection Loss: 0% (with proper implementation)
Request Loss: 0% (with buffering enabled)
Real-time Monitoring
// Get performance metrics $bootstrap = new ZeroDowntimeBootstrap($app, $logger); $stats = $bootstrap->getStats(); echo "Active connections: {$stats['active_connections']}"; echo "Memory usage: {$stats['memory_usage_mb']}MB"; echo "Uptime: {$stats['uptime_seconds']} seconds"; // Health status monitoring $health = $bootstrap->getHealthStatus(); echo "Status: {$health['status']}"; // healthy|reloading|degraded echo "Component status: " . json_encode($health['components']);
🐛 Troubleshooting
Common Issues
Connection Migration Fails
# Check connection state path permissions ls -la /tmp/highper-connection-states # Verify protocol support grep -r "http2_websocket" /var/log/highper/
Request Buffer Overflow
# Increase buffer size export REQUEST_BUFFER_SIZE_MB=50 # Change overflow strategy export REQUEST_OVERFLOW_STRATEGY=oldest
Worker Handoff Timeout
# Increase timeout export HANDOFF_TIMEOUT=60 # Check socket permissions ls -la /tmp/highper-socket-handoff*
📚 API Reference
ZeroDowntimeBootstrap
class ZeroDowntimeBootstrap { // Deployment operations public function performZeroDowntimeReload(): array; // Trigger hot reload public function performGracefulShutdown(): array; // Graceful shutdown public function testConnectionMigration(): void; // Test migration // Status and monitoring public function getStats(): array; // System statistics public function getHealthStatus(): array; // Health check public function isReloadInProgress(): bool; // Check reload status }
ConnectionStateSerializer
class ConnectionStateSerializer { // Connection management public function registerConnection(string $id, object $connection, string $protocol): void; public function unregisterConnection(string $connectionId): void; public function hasConnection(string $connectionId): bool; // Active state management (O(1) performance) public function markConnectionActive(string $connectionId): void; public function markConnectionIdle(string $connectionId): void; public function markConnectionClosing(string $connectionId): void; public function isConnectionActive(string $connectionId): bool; // Serialization operations public function serializeAllConnections(): array; // All connections public function serializeActiveConnections(): array; // Active only (priority) public function serializeWorkerConnections(int $workerPid): array; // Migration operations public function migrateConnectionsToWorkers(array $workers, array $states): array; public function migrateWorkerConnections(array $newWorker, array $connectionStates): void; // Statistics and validation public function getActiveConnectionCount(): int; // O(1) performance public function getTotalConnectionCount(): int; public function getConnectionStats(): array; public function validateStates(array $states): array; // Compression utilities public function compressStates(array $states): string; public function decompressStates(string $compressedData): array; }
ProxyRequestQueue
class ProxyRequestQueue { // Buffer management public function startBuffering(): void; // Global buffering public function stopBuffering(): void; public function startWorkerBuffering(int $workerPid): void; // Worker-specific public function stopWorkerBuffering(int $workerPid): void; // Request operations public function bufferRequest($request, ?int $targetWorkerPid = null): bool; public function replayBufferedRequests(array $newWorkers): array; public function replayWorkerRequests(array $newWorker, int $oldWorkerPid): void; // Queue management public function flush(): void; // Emergency flush public function flushBuffer(): array; // Get and clear public function getBufferedRequests(): array; // Priority sorted public function getRequestsForWorker(string $workerId): array; public function getExpiredRequests(): array; // Status and statistics public function getBufferedRequestCount(): int; public function isBufferingEnabled(): bool; public function getBufferStats(): array; public function getPerformanceMetrics(): array; public function getStats(): array; }
Response Formats
performZeroDowntimeReload() Response:
[
'reload_successful' => true,
'migrated_connections' => 150,
'replayed_requests' => 45,
'strategy' => 'blue-green',
'duration_ms' => 2340.50
]
getHealthStatus() Response:
[
'status' => 'healthy', // healthy|reloading|degraded
'components' => [
'connection_migration' => [
'status' => 'healthy',
'active_connections' => 150
],
'request_buffering' => [
'status' => 'idle',
'buffered_requests' => 0
]
],
'uptime_seconds' => 3600.45,
'memory_usage_mb' => 45.2
]
🤝 Contributing
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
🔧 Requirements
- PHP: ^8.3|^8.4
- Extensions: ext-pcntl, ext-posix, ext-sockets
- Dependencies: highperapp/websockets ^1.1
- Framework: Compatible with HighPer Framework
- Environment: Linux/Unix systems (process management)
📄 License
This package is open-sourced software licensed under the MIT license.
🙏 Acknowledgments
- RFC 8441 - WebSocket over HTTP/2 specification
- Amp Framework - Asynchronous PHP components
- QUIC Protocol - HTTP/3 transport layer
- HighPer Framework - High-performance PHP framework
highperapp/zero-downtime 适用场景与选型建议
highperapp/zero-downtime 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「websocket」 「http2」 「zero-downtime」 「http3」 「quic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 highperapp/zero-downtime 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 highperapp/zero-downtime 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 highperapp/zero-downtime 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Sends Link headers to bring HTTP/2 Server Push for scripts and styles to WordPress.
This bundle provides you with an easy way to include assets on demand, but still include them in your header or footer. This can be used for your BEM blocks or facilitates HTTP2.
TYPO3 Extension to push javascript and css files over a http/2 connection.
WebSocket view engine
APNS - Apple Push Notification Service PHP client
Alfabank REST API integration
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 3
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-12