Download the PHP package allsilaevex/swoole-connection-pool without Composer
On this page you can find all versions of the php package allsilaevex/swoole-connection-pool. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download allsilaevex/swoole-connection-pool
More information about allsilaevex/swoole-connection-pool
Files in allsilaevex/swoole-connection-pool
Package swoole-connection-pool
Short Description Swoole Connection Pool
License MIT
Informations about the package swoole-connection-pool
Swoole Connection Pool
A solid, flexible and high-performance Swoole based connection pool.
⚙️ Installation
Requirements
- PHP 8.2.0 or later
- Swoole 5.1.0 or later
[!WARNING] Pool has not been tested with
swoole.enable_preemptive_scheduler = 1
. Use at your own risk!
⚡️ Quickstart
This example demonstrates the creation of a simple pool of connections to the MySQL database.
✨ Features
- High-performance even in unusual cases (see Benchmarks)
- Handling connection failure and self-recovery
- Doesn't burden the garbage collector
- Coverage by static analyzers (PHPStan, Psalm) and support generics
- Out of the box connection pool provides:
- load-dependent resizing number of connections
- reconnection for long-lived connections
- leaked connection detection
- support lifetime hooks for connections
- Metrics that can be easily stored into Prometheus and used for analysis
❓ Why should I use a connection pool?
The most obvious reason: connection pool saves time by not establishing a new connection for each request.
A less obvious reason lies in the way Swoole and coroutines work. By design, it's not possible to use the same connection simultaneously in two different coroutines, which means you must create a separate connection for each coroutine. This adds extra overhead compared to using a single connection sequentially. It can also lead to uncontrolled growth in the number of connections.
And the least obvious issue: slowdowns due to multiple context switches during execution. Context switching occurs during IO operations, including establishing a connection. Thus, the execution flow of the following code may not be obvious:
What happens if a CPU-bound load appears in the second coroutine? Since coroutines are executed within the same process, they cannot parallelize the execution of CPU-bound code. Therefore, if one coroutine is executing PHP code, the others will wait. So, the execution of query will be deferred until the second coroutine finishes its work and returns control back to the first coroutine.
🚀 Benchmarks
Without connection pool:
With connection pool:
Read more about the tests in the document.
🔧 Configuration
For simplifying configuration and creating a pool, there's the ConnectionPoolFactory
.
The factory comes with default settings, but it's highly recommended to customize the parameters
according to your specific needs.
The following example demonstrates the configuration options:
License
MIT