Skip to content

Commit

Permalink
Merge pull request #182 from clue-labs/short-idle
Browse files Browse the repository at this point in the history
Reduce default idle time to 1ms
  • Loading branch information
SimonFrings authored Nov 7, 2023
2 parents 420ad29 + b30a49b commit 28ccb94
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 33 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

require __DIR__ . '/vendor/autoload.php';

$factory = new React\MySQL\Factory();
$connection = $factory->createLazyConnection('user:pass@localhost/bookstore');

Expand All @@ -54,8 +58,6 @@ $connection->query('SELECT * FROM book')->then(
echo 'Error: ' . $error->getMessage() . PHP_EOL;
}
);

$connection->quit();
```

See also the [examples](examples).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/01-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@
// the query was not executed successfully
echo 'Error: ' . $error->getMessage() . PHP_EOL;
});

$connection->quit();
2 changes: 0 additions & 2 deletions examples/02-query-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@
$stream->on('close', function () {
echo 'CLOSED' . PHP_EOL;
});

$connection->quit();
26 changes: 14 additions & 12 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Io/LazyConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public function testConnectLazyWithValidAuthWillRunUntilIdleTimerAfterPingEvenWi
{
$factory = new Factory();

$uri = $this->getConnectionString() . '?idle=0';
$uri = $this->getConnectionString();
$connection = $factory->createLazyConnection($uri);

$connection->ping();
Expand Down
2 changes: 1 addition & 1 deletion tests/Io/LazyConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 28ccb94

Please sign in to comment.