You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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
orshell_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 withexec
orshell_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 ofexec
andshell_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
Thank you for taking the time to contribute to Flexi! Your feature request will be reviewed by the maintainers and considered for future development.
The text was updated successfully, but these errors were encountered: