From b30a49b92395e1b2f612fdbf156d39c3e728915d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 6 Nov 2023 17:54:06 +0100 Subject: [PATCH] Reduce default idle time to 1ms --- README.md | 32 ++++++++++++++++++-------------- examples/01-query.php | 2 -- examples/02-query-stream.php | 2 -- src/Factory.php | 26 ++++++++++++++------------ src/Io/LazyConnection.php | 2 +- tests/FactoryTest.php | 2 +- tests/Io/LazyConnectionTest.php | 2 +- 7 files changed, 35 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 6fe2343..396ec96 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ It is written in pure PHP and does not require any extensions. This example runs a simple `SELECT` query and dumps all the records from a `book` table: ```php +createLazyConnection('user:pass@localhost/bookstore'); @@ -54,8 +58,6 @@ $connection->query('SELECT * FROM book')->then( echo 'Error: ' . $error->getMessage() . PHP_EOL; } ); - -$connection->quit(); ``` See also the [examples](examples). @@ -202,9 +204,11 @@ This method immediately returns a "virtual" connection implementing the interface with your MySQL database. Internally, it lazily creates the underlying database connection only on demand once the first request is invoked on this instance and will queue all outstanding requests until -the underlying connection is ready. Additionally, it will only keep this -underlying connection in an "idle" state for 60s by default and will -automatically end the underlying connection when it is no longer needed. +the underlying connection is ready. This underlying connection will be +reused for all requests until it is closed. By default, idle connections +will be held open for 1ms (0.001s) when not used. The next request will +either reuse the existing connection or will automatically create a new +underlying connection if this idle time is expired. From a consumer side this means that you can start sending queries to the database right away while the underlying connection may still be @@ -277,17 +281,17 @@ in seconds (or use a negative number to not apply a timeout) like this: $factory->createLazyConnection('localhost?timeout=0.5'); ``` -By default, this method will keep "idle" connection open for 60s and will -then end the underlying connection. The next request after an "idle" -connection ended will automatically create a new underlying connection. -This ensure you always get a "fresh" connection and as such should not be -confused with a "keepalive" or "heartbeat" mechanism, as this will not -actively try to probe the connection. You can explicitly pass a custom -idle timeout value in seconds (or use a negative number to not apply a -timeout) like this: +By default, idle connections will be held open for 1ms (0.001s) when not +used. The next request will either reuse the existing connection or will +automatically create a new underlying connection if this idle time is +expired. This ensures you always get a "fresh" connection and as such +should not be confused with a "keepalive" or "heartbeat" mechanism, as +this will not actively try to probe the connection. You can explicitly +pass a custom idle timeout value in seconds (or use a negative number to +not apply a timeout) like this: ```php -$factory->createLazyConnection('localhost?idle=0.1'); +$factory->createLazyConnection('localhost?idle=10.0'); ``` By default, the connection provides full UTF-8 support (using the diff --git a/examples/01-query.php b/examples/01-query.php index 269b066..776e1f5 100644 --- a/examples/01-query.php +++ b/examples/01-query.php @@ -29,5 +29,3 @@ // the query was not executed successfully echo 'Error: ' . $error->getMessage() . PHP_EOL; }); - -$connection->quit(); diff --git a/examples/02-query-stream.php b/examples/02-query-stream.php index 1bc3744..c4e69b7 100644 --- a/examples/02-query-stream.php +++ b/examples/02-query-stream.php @@ -24,5 +24,3 @@ $stream->on('close', function () { echo 'CLOSED' . PHP_EOL; }); - -$connection->quit(); diff --git a/src/Factory.php b/src/Factory.php index f43f70b..7fb89d0 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -276,9 +276,11 @@ public function createConnection( * interface with your MySQL database. Internally, it lazily creates the * underlying database connection only on demand once the first request is * invoked on this instance and will queue all outstanding requests until - * the underlying connection is ready. Additionally, it will only keep this - * underlying connection in an "idle" state for 60s by default and will - * automatically end the underlying connection when it is no longer needed. + * the underlying connection is ready. This underlying connection will be + * reused for all requests until it is closed. By default, idle connections + * will be held open for 1ms (0.001s) when not used. The next request will + * either reuse the existing connection or will automatically create a new + * underlying connection if this idle time is expired. * * From a consumer side this means that you can start sending queries to the * database right away while the underlying connection may still be @@ -351,17 +353,17 @@ public function createConnection( * $factory->createLazyConnection('localhost?timeout=0.5'); * ``` * - * By default, this method will keep "idle" connection open for 60s and will - * then end the underlying connection. The next request after an "idle" - * connection ended will automatically create a new underlying connection. - * This ensure you always get a "fresh" connection and as such should not be - * confused with a "keepalive" or "heartbeat" mechanism, as this will not - * actively try to probe the connection. You can explicitly pass a custom - * idle timeout value in seconds (or use a negative number to not apply a - * timeout) like this: + * By default, idle connections will be held open for 1ms (0.001s) when not + * used. The next request will either reuse the existing connection or will + * automatically create a new underlying connection if this idle time is + * expired. This ensures you always get a "fresh" connection and as such + * should not be confused with a "keepalive" or "heartbeat" mechanism, as + * this will not actively try to probe the connection. You can explicitly + * pass a custom idle timeout value in seconds (or use a negative number to + * not apply a timeout) like this: * * ```php - * $factory->createLazyConnection('localhost?idle=0.1'); + * $factory->createLazyConnection('localhost?idle=10.0'); * ``` * * By default, the connection provides full UTF-8 support (using the diff --git a/src/Io/LazyConnection.php b/src/Io/LazyConnection.php index 49cb9f9..d825dbd 100644 --- a/src/Io/LazyConnection.php +++ b/src/Io/LazyConnection.php @@ -27,7 +27,7 @@ class LazyConnection extends EventEmitter implements ConnectionInterface private $disconnecting; private $loop; - private $idlePeriod = 60.0; + private $idlePeriod = 0.001; private $idleTimer; private $pending = 0; diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 22235d7..2e73bed 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -602,7 +602,7 @@ public function testConnectLazyWithValidAuthWillRunUntilIdleTimerAfterPingEvenWi { $factory = new Factory(); - $uri = $this->getConnectionString() . '?idle=0'; + $uri = $this->getConnectionString(); $connection = $factory->createLazyConnection($uri); $connection->ping(); diff --git a/tests/Io/LazyConnectionTest.php b/tests/Io/LazyConnectionTest.php index fc06ea9..89e75e8 100644 --- a/tests/Io/LazyConnectionTest.php +++ b/tests/Io/LazyConnectionTest.php @@ -218,7 +218,7 @@ public function testQueryWillResolveAndStartTimerWithDefaultIntervalWhenQueryFro $factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base)); $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); - $loop->expects($this->once())->method('addTimer')->with(60.0, $this->anything()); + $loop->expects($this->once())->method('addTimer')->with(0.001, $this->anything()); $connection = new LazyConnection($factory, '', $loop);