sockeon/event-loop

A high-performance, extensible event loop implementation for PHP with support for promises, coroutines, async I/O, and worker pools

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 1

Open Issues: 0

pkg:composer/sockeon/event-loop

dev-main 2025-11-22 04:36 UTC

This package is auto-updated.

Last update: 2025-11-22 04:36:35 UTC


README

A high-performance, extensible event loop implementation for PHP with support for promises, coroutines, async I/O, and worker pools.

Features

  • ๐Ÿš€ High Performance: Non-blocking I/O with efficient event loop
  • ๐Ÿ”„ Promise Support: Promise/A+ compliant promises with async/await-like syntax
  • ๐Ÿงต Coroutines: Generator-based coroutines for elegant async code
  • ๐Ÿ”Œ Async Sockets: Non-blocking TCP and Unix socket support
  • ๐Ÿ‘ท Worker Pools: Process-based worker pools for true parallelism
  • ๐Ÿ”Œ Extensible: Support for multiple event loop drivers (native, libev, libuv)
  • ๐Ÿ“ฆ Framework Agnostic: Can be used with any PHP application

Architecture

Core Components

  1. Event Loop - Main event loop with driver support
  2. Promises - Promise/A+ compliant promise implementation
  3. Coroutines - Generator-based coroutines
  4. Async Sockets - Non-blocking socket I/O
  5. Worker Pools - Process-based worker management
  6. Streams - Readable and writable stream abstractions

Features to Implement

Phase 1: Core Event Loop โœ…

  • LoopInterface - Main event loop interface
  • Loop - Singleton event loop instance
  • DriverInterface - Driver abstraction
  • NativeDriver - Native PHP stream_select driver
  • Basic event loop operations:
    • run() - Start the event loop
    • stop() - Stop the event loop
    • defer() - Schedule callback for next tick
    • delay() - Schedule callback after delay
    • repeat() - Schedule repeating callback
    • onReadable() - Watch for readable events
    • onWritable() - Watch for writable events
    • cancel() - Cancel a watcher

Phase 2: Promises โœ…

  • PromiseInterface - Promise/A+ compliant interface
  • Promise - Promise implementation
  • Deferred - Deferred promise resolver
  • Promise methods:
    • then() - Chain promises
    • catch() - Handle errors
    • finally() - Always execute
    • Promise::all() - Wait for all promises
    • Promise::any() - Wait for any promise
    • Promise::race() - Race promises
    • Promise::resolve() - Create resolved promise
    • Promise::reject() - Create rejected promise

Phase 3: Coroutines โœ…

  • Coroutine - Coroutine wrapper
  • Generator-based coroutines
  • Automatic promise unwrapping
  • Exception handling in coroutines
  • Async/await-like syntax support

Phase 4: Async Sockets โœ…

  • SocketInterface - Socket abstraction
  • ServerSocket - Async server socket
  • ClientSocket - Async client socket
  • TCP socket support
  • Unix socket support
  • SSL/TLS support
  • Event-driven I/O:
    • on('connection') - New connection event
    • on('data') - Data received event
    • on('close') - Connection closed event
    • on('error') - Error event
    • write() - Write data
    • close() - Close connection

Phase 5: Worker Pools โœ…

  • WorkerInterface - Worker interface
  • Worker - Individual worker process
  • WorkerPool - Worker pool manager
  • Process forking
  • Task queue
  • Load balancing
  • Worker lifecycle management:
    • Start workers
    • Stop workers
    • Restart crashed workers
    • Graceful shutdown

Phase 6: Streams โœ…

  • StreamInterface - Stream abstraction
  • ReadableStream - Readable stream
  • WritableStream - Writable stream
  • DuplexStream - Bidirectional stream
  • Stream events:
    • on('data') - Data available
    • on('end') - Stream ended
    • on('error') - Stream error
    • on('close') - Stream closed

Phase 7: Advanced Drivers (Optional) โœ…

  • EvDriver - libev driver
  • UvDriver - libuv driver
  • Driver auto-detection
  • Performance optimizations

Testing

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

License

MIT License - see LICENSE file for details.

Status

๐Ÿšง Work in Progress - This package is currently under active development.

Roadmap

  • Package structure
  • Phase 1: Core Event Loop
  • Phase 2: Promises
  • Phase 3: Coroutines
  • Phase 4: Async Sockets
  • Phase 5: Worker Pools
  • Phase 6: Streams
  • Phase 7: Advanced Drivers
  • Documentation
  • Tests
  • Examples