Skip to content
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

[FEAT] - Asynchronous EventBus #38

Closed
cbatista8a opened this issue Aug 7, 2024 · 0 comments · Fixed by #43
Closed

[FEAT] - Asynchronous EventBus #38

cbatista8a opened this issue Aug 7, 2024 · 0 comments · Fixed by #43
Assignees
Labels
enhancement New feature or request

Comments

@cbatista8a
Copy link
Contributor

Feature Request

Description

For the Flexi framework, a synchronous EventBus class has been implemented, which, although it allows subscribers to listen to events, represents a bottleneck in the application's performance since the execution has to wait for all listeners subscribed to the event to carry out their operations before being able to return a response to the user or end the execution in the case where the request is made through a command (CQRS).

Proposed Solution

To improve the performance of Events in the Flexi framework, it is necessary to implement an asynchronous EventBus. To do this, several strategies can be used that do not depend on too many external libraries, preferring native/simple solutions over external dependencies or extensions.

Alternatives Considered

PHP's Built-In Asynchronous Functions

Use pcntl_fork to create child processes that handle events, allowing the main process to continue execution.

Launch separate PHP scripts for each listener to handle the event asynchronously

Use exec or shell_exec

PHP's Built-In Asynchronous Extension (Swoole)

Swoole provides coroutines, an async I/O library, and a set of tools for creating efficient network servers.
(This extension is not available on Windows platforms)

Multi-Threading with pthreads (if ZTS enabled)

For environments where PHP is compiled with ZTS (Zend Thread Safety), the pthreads extension can be used to create multi-threaded applications but requires servers to use that compilation of PHP.

react/child-process Package

By using this dependency to implement an asynchronous bus the main application can continue execution and send a response to the user while the event handlers run simultaneously in the background.

External Queue

Use an external queue to capture the events that are triggered, then the external service queue will invoke the subscribers by passing them the event. At this point Flexi should have a public endpoint that will receive two parameters, the event and the subscriber to invoke, the latter would be executed in a separate process as it is an external call from the queue service.

Additional Context

For users who may not have access to install extensions on shared servers, the most accessible solution is using PHP's built-in functions like pcntl_fork ,launching separate PHP scripts with exec or shell_exec or implement the react/child-process Package . These methods do not require any additional extensions and should work in most shared hosting environments where PHP is available (in some cases shared servers disable the use of exec and shell_exec for security reasons).

The external queue alternative is more complex as it requires more infrastructure and adapters to connect to the service and should only be used in applications that require communication between multiple microservices.

Possible Implementation

Logical Steps

  • Spawn a new process or thread to handle the listener
  • Execute the command asynchronously or isolated
  • Handle exceptions and ensure the command is executed correctly.
  • The application continues its execution and returns the response without waiting for subscribers.

Thank you for taking the time to contribute to Flexi! Your feature request will be reviewed by the maintainers and considered for future development.

@cbatista8a cbatista8a added the enhancement New feature or request label Aug 7, 2024
@Qupiter Qupiter linked a pull request Sep 27, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants