diff --git a/CHANGES.md b/CHANGES.md deleted file mode 100644 index 3abd848..0000000 --- a/CHANGES.md +++ /dev/null @@ -1,265 +0,0 @@ -# Event-Driven Workers Implementation - Change Summary - -**Date:** December 10, 2025 -**Version:** 1.1.0 (proposed) -**Type:** Feature Addition (Non-Breaking) - ---- - -## ๐Ÿ“ Summary - -Added support for **Event-Driven Workers** in Worker Pool mode, allowing full applications with custom event loops to run in worker processes. This enables proper integration with Event Bus systems (like Duyler Event Bus) and asynchronous request processing. - -## โœจ New Features - -### EventDrivenWorkerInterface - -New interface for running full applications with event loops in workers: - -```php -interface EventDrivenWorkerInterface -{ - public function run(int $workerId, Server $server): void; -} -``` - -**Key difference from WorkerCallbackInterface:** -- `WorkerCallbackInterface::handle()` - called for EACH connection (synchronous) -- `EventDrivenWorkerInterface::run()` - called ONCE on worker start (event loop inside) - -### Server Enhancements - -**New methods in ServerInterface:** -- `setWorkerId(int $workerId): void` - Set worker ID for Worker Pool mode -- `registerFiber(\Fiber $fiber): void` - Register background Fibers - -**Updated behavior:** -- `hasRequest()` now automatically resumes all registered Fibers - -### Worker Pool Dual Mode - -Both `SharedSocketMaster` and `CentralizedMaster` now support two modes: - -**1. Callback Mode (Legacy)** -```php -$master = new SharedSocketMaster( - config: $config, - serverConfig: $serverConfig, - workerCallback: $callback, // Old way -); -``` - -**2. Event-Driven Mode (New)** -```php -$master = new SharedSocketMaster( - config: $config, - serverConfig: $serverConfig, - eventDrivenWorker: $worker, // New way -); -``` - ---- - -## ๐Ÿ“ Changed Files - -### Modified (5 files): -1. `README.md` - Updated Worker Pool example -2. `src/Server.php` - Added Fiber support and worker ID -3. `src/ServerInterface.php` - Added new methods -4. `src/WorkerPool/Master/SharedSocketMaster.php` - Dual mode support -5. `src/WorkerPool/Master/CentralizedMaster.php` - Dual mode support - -### Added (1 file): -1. `src/WorkerPool/Worker/EventDrivenWorkerInterface.php` - New interface - -### Documentation (4 files): -1. `examples/event-driven-worker.php` - Complete working example -2. `docs/PROPOSAL-event-driven-workers.md` - Original proposal -3. `docs/IMPLEMENTATION-PLAN.md` - Implementation plan -4. `docs/IMPLEMENTATION-SUMMARY.md` - Implementation summary -5. `docs/worker-pool-event-driven-architecture.md` - Architecture details -6. `CHANGES.md` - This file - ---- - -## ๐Ÿ”„ Migration Guide - -### For New Projects - -Use the new Event-Driven mode: - -```php -class MyApp implements EventDrivenWorkerInterface -{ - public function run(int $workerId, Server $server): void - { - $eventBus = new EventBus(); - - while (true) { - if ($server->hasRequest()) { - $request = $server->getRequest(); - $eventBus->dispatch('http.request', $request); - } - - $eventBus->tick(); - - if ($server->hasPendingResponse()) { - $response = $eventBus->getResponse(); - $server->respond($response); - } - - usleep(1000); - } - } -} -``` - -### For Existing Projects - -**No changes required!** Old code continues to work: - -```php -// This still works -$master = new SharedSocketMaster( - config: $config, - serverConfig: $serverConfig, - workerCallback: $oldCallback, // โœ… Still works -); -``` - ---- - -## โœ… Backward Compatibility - -- โœ… **100% backward compatible** -- โœ… No breaking changes -- โœ… Old WorkerCallbackInterface still supported -- โœ… Existing code works without modifications - ---- - -## ๐Ÿงช Testing Status - -### Completed: -- โœ… PHP-CS-Fixer passed (code style) -- โœ… Manual testing with example - -### TODO: -- [ ] Unit tests for EventDrivenWorkerInterface -- [ ] Integration tests for dual mode -- [ ] Performance tests -- [ ] PHPStan analysis (requires environment setup) - ---- - -## ๐Ÿ“– Documentation - -### Available: -- โœ… `examples/event-driven-worker.php` - Working example -- โœ… `README.md` - Updated with new examples -- โœ… PHPDoc comments in all new code -- โœ… Architecture documentation in `docs/` - -### TODO: -- [ ] Detailed Event-Driven Worker guide -- [ ] Migration guide (Callback โ†’ Event-Driven) -- [ ] Best practices document - ---- - -## ๐ŸŽฏ Benefits - -### For Event-Driven Applications: -1. โœ… Full control over event loop -2. โœ… Asynchronous request processing -3. โœ… One application instance per worker -4. โœ… Natural Event Bus integration -5. โœ… Responses in different ticks - -### For Existing Code: -1. โœ… No changes required -2. โœ… Gradual migration possible -3. โœ… Both modes can coexist - ---- - -## ๐Ÿš€ Next Steps - -1. **Commit changes:** - ```bash - git add -A - git commit -m "feat: Add EventDrivenWorkerInterface for Worker Pool" - ``` - -2. **Test the example:** - ```bash - php examples/event-driven-worker.php - ``` - -3. **Add tests** (Phase 6 from IMPLEMENTATION-PLAN.md) - -4. **Update version** to 1.1.0 in composer.json - ---- - -## ๐Ÿ“Š Statistics - -- **Time to implement:** ~2 hours -- **Files created:** 2 (interface + example) -- **Files modified:** 5 (core components) -- **Lines of code:** ~400 -- **Breaking changes:** 0 -- **Backward compatibility:** 100% - ---- - -## โœ… Checklist - -### Implementation: -- [x] Create EventDrivenWorkerInterface -- [x] Update ServerInterface -- [x] Update Server -- [x] Update SharedSocketMaster -- [x] Update CentralizedMaster -- [x] Create example -- [x] Update README -- [x] Code style (PHP-CS-Fixer) - -### Testing: -- [ ] Unit tests -- [ ] Integration tests -- [ ] Performance tests - -### Documentation: -- [x] README updated -- [x] Example created -- [x] PHPDoc comments -- [ ] Detailed guide - -### Release: -- [ ] Update CHANGELOG -- [ ] Update version -- [ ] Git tag -- [ ] Announce - ---- - -**Status:** โœ… **READY FOR REVIEW** - ---- - -## ๐ŸŽ‰ Conclusion - -EventDrivenWorkerInterface successfully addresses the original requirement: - -> "Worker process should run a full application with its own event loop, -> where Master passes connections and application polls hasRequest() -> on each tick, processing requests asynchronously via Event Bus." - -**This implementation is production-ready and fully backward compatible!** - ---- - -**Prepared by:** AI Code Review System -**Date:** December 10, 2025 -**Version:** 1.0 diff --git a/README.md b/README.md index 4e03e11..c2d0654 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=duyler_di&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=duyler_http-server) +[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=duyler_di&metric=coverage)](https://sonarcloud.io/summary/new_code?id=duyler_http-server) +[![type-coverage](https://shepherd.dev/github/duyler/http-server/coverage.svg)](https://shepherd.dev/github/duyler/http-server) +[![psalm-level](https://shepherd.dev/github/duyler/http-server/level.svg)](https://shepherd.dev/github/duyler/http-server) + # Duyler HTTP Server Non-blocking HTTP server for Duyler Framework worker mode with full PSR-7 support and integrated Worker Pool. @@ -18,14 +23,13 @@ Non-blocking HTTP server for Duyler Framework worker mode with full PSR-7 suppor - **Server Metrics** - Built-in performance and health monitoring - **High Performance** - Optimized for long-running worker processes -### Worker Pool Features (New) +### Worker Pool Features - **Process Management** - Fork-based worker processes with auto-restart - **Load Balancing** - Least Connections and Round Robin algorithms - **IPC System** - Unix domain sockets with FD passing support - **Dual Architecture** - FD Passing (Linux) and Shared Socket (Docker/fallback) - **Auto CPU Detection** - Automatic worker count based on CPU cores - **Signal Handling** - Graceful shutdown via SIGTERM/SIGINT -- **Cross-Platform** - Linux, Docker, with macOS support via Docker ## Requirements @@ -44,9 +48,9 @@ composer require duyler/http-server ### Worker Pool HTTP Server (Recommended for Production) -**โœจ New: Event-Driven Worker Mode** (Recommended for full applications) +**Event-Driven Worker Mode** -For event-driven applications with Event Bus (like Duyler Framework): +For long running applications with Event Bus (like Duyler Framework): ```php use Duyler\HttpServer\Config\ServerConfig; @@ -65,28 +69,8 @@ class MyApp implements EventDrivenWorkerInterface // Server is automatically running in Worker Pool mode. // Initialize your application ONCE - $eventBus = new EventBus(); - $db = new Database(); - - // Event loop - while (true) { - // Get requests from Worker Pool - if ($server->hasRequest()) { - $request = $server->getRequest(); - $eventBus->dispatch('http.request', $request); - } - - // Process events - $eventBus->tick(); - - // Send responses - if ($server->hasPendingResponse()) { - $response = $eventBus->getResponse(); - $server->respond($response); - } - - usleep(1000); - } + $application = new Application($workerId, $server); + $application->run(); } } @@ -98,7 +82,7 @@ $app = new MyApp(); $master = new SharedSocketMaster( config: $workerPoolConfig, serverConfig: $serverConfig, - eventDrivenWorker: $app, // โ† Event-Driven mode + eventDrivenWorker: $app, ); $master->start(); @@ -449,7 +433,7 @@ composer phpstan ## Roadmap -### Version 1.2.0 (In Progress) +### Version 1.0 (In Progress) - [x] Worker Pool - Dual architecture (FD Passing + Shared Socket) - [x] WebSocket - RFC 6455 compliant implementation - [x] MasterFactory with auto-detection @@ -458,7 +442,8 @@ composer phpstan - [ ] Enhanced documentation ### Future Versions -- [ ] HTTP/2 support (planned for 2.0.0) +- [ ] HTTP/2 support +- [ ] gRPC support - [ ] Advanced Worker Pool features ## Contributing diff --git a/composer.json b/composer.json index 1fff957..6a1ec1f 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,6 @@ { "name": "duyler/http-server", "description": "Non-blocking HTTP server for Duyler Framework worker mode with PSR-7 support", - "version": "1.0.0", "type": "library", "license": "MIT", "keywords": [