Skip to content

Completely refactor to enable a fully non-blocking, event-loop driven architecture #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2024

Conversation

Thavarshan
Copy link
Owner

Purpose

This PR introduces a complete overhaul of the Matrix library’s asynchronous execution model. Previously, Matrix relied on Fibers for concurrency, but this proved challenging and introduced complexity in error handling and interleaving tasks. Now, we have shifted to using pcntl_fork() and ReactPHP promises to achieve true parallelism and a simpler, promise-like API—making asynchronous task handling more intuitive, JavaScript-like, and robust.

For reference, this change addresses multiple issues related to confusing Fiber usage, lack of proper concurrency, and difficulties in error propagation. The new model ensures tasks actually run concurrently in separate processes, returning results asynchronously without blocking the main script.

Approach

  • Replace Fibers with pcntl_fork(): The asynchronous execution now happens in child processes, allowing true parallel execution and simpler architecture.
  • Integration with ReactPHP: We leverage React’s event loop and promises for non-blocking I/O and a familiar .then() / .catch() promise style.
  • AsyncProcessManager & AsyncPromise: Introduces AsyncProcessManager for forking and communicating with child processes, and AsyncPromise for a promise-like API.
  • Global async() helper: Simplifies the user experience, providing a direct function to start async tasks and attach handlers, closely mirroring JavaScript’s async/await feel.
  • Updated Documentation & README: The README now reflects the new architecture, setup instructions, usage examples, and testing guidelines.

Open Questions and Pre-Merge TODOs

  • Review the integration tests to ensure they cover timing-based concurrency checks.
  • Confirm that error messages from child processes are properly serialized and deserialized into parent exceptions.
  • Update the version number to 3.0.0 to reflect the breaking changes.

Learning

Research included exploring ReactPHP’s event loop and promise design patterns, revisiting pcntl_fork() usage, and studying best practices for inter-process communication in PHP. We also examined how other asynchronous PHP libraries structure their concurrency models, leading us to adopt a more conventional process-based approach rather than Fibers. This has simplified error handling, provided clearer concurrency guarantees, and made the user-facing API more intuitive.

@Thavarshan Thavarshan added documentation Improvements or additions to documentation enhancement New feature or request labels Dec 16, 2024
@Thavarshan Thavarshan self-assigned this Dec 16, 2024
@Thavarshan Thavarshan merged commit 6b63128 into main Dec 16, 2024
4 checks passed
@Thavarshan Thavarshan deleted the feature/concurrency branch December 16, 2024 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant