Skip to content

Commit 28ccb94

Browse files
authored
Merge pull request #182 from clue-labs/short-idle
Reduce default idle time to 1ms
2 parents 420ad29 + b30a49b commit 28ccb94

File tree

7 files changed

+35
-33
lines changed

7 files changed

+35
-33
lines changed

README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ It is written in pure PHP and does not require any extensions.
4141
This example runs a simple `SELECT` query and dumps all the records from a `book` table:
4242

4343
```php
44+
<?php
45+
46+
require __DIR__ . '/vendor/autoload.php';
47+
4448
$factory = new React\MySQL\Factory();
4549
$connection = $factory->createLazyConnection('user:pass@localhost/bookstore');
4650

@@ -54,8 +58,6 @@ $connection->query('SELECT * FROM book')->then(
5458
echo 'Error: ' . $error->getMessage() . PHP_EOL;
5559
}
5660
);
57-
58-
$connection->quit();
5961
```
6062

6163
See also the [examples](examples).
@@ -202,9 +204,11 @@ This method immediately returns a "virtual" connection implementing the
202204
interface with your MySQL database. Internally, it lazily creates the
203205
underlying database connection only on demand once the first request is
204206
invoked on this instance and will queue all outstanding requests until
205-
the underlying connection is ready. Additionally, it will only keep this
206-
underlying connection in an "idle" state for 60s by default and will
207-
automatically end the underlying connection when it is no longer needed.
207+
the underlying connection is ready. This underlying connection will be
208+
reused for all requests until it is closed. By default, idle connections
209+
will be held open for 1ms (0.001s) when not used. The next request will
210+
either reuse the existing connection or will automatically create a new
211+
underlying connection if this idle time is expired.
208212

209213
From a consumer side this means that you can start sending queries to the
210214
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:
277281
$factory->createLazyConnection('localhost?timeout=0.5');
278282
```
279283

280-
By default, this method will keep "idle" connection open for 60s and will
281-
then end the underlying connection. The next request after an "idle"
282-
connection ended will automatically create a new underlying connection.
283-
This ensure you always get a "fresh" connection and as such should not be
284-
confused with a "keepalive" or "heartbeat" mechanism, as this will not
285-
actively try to probe the connection. You can explicitly pass a custom
286-
idle timeout value in seconds (or use a negative number to not apply a
287-
timeout) like this:
284+
By default, idle connections will be held open for 1ms (0.001s) when not
285+
used. The next request will either reuse the existing connection or will
286+
automatically create a new underlying connection if this idle time is
287+
expired. This ensures you always get a "fresh" connection and as such
288+
should not be confused with a "keepalive" or "heartbeat" mechanism, as
289+
this will not actively try to probe the connection. You can explicitly
290+
pass a custom idle timeout value in seconds (or use a negative number to
291+
not apply a timeout) like this:
288292

289293
```php
290-
$factory->createLazyConnection('localhost?idle=0.1');
294+
$factory->createLazyConnection('localhost?idle=10.0');
291295
```
292296

293297
By default, the connection provides full UTF-8 support (using the

examples/01-query.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,3 @@
2929
// the query was not executed successfully
3030
echo 'Error: ' . $error->getMessage() . PHP_EOL;
3131
});
32-
33-
$connection->quit();

examples/02-query-stream.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,3 @@
2424
$stream->on('close', function () {
2525
echo 'CLOSED' . PHP_EOL;
2626
});
27-
28-
$connection->quit();

src/Factory.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,11 @@ public function createConnection(
276276
* interface with your MySQL database. Internally, it lazily creates the
277277
* underlying database connection only on demand once the first request is
278278
* invoked on this instance and will queue all outstanding requests until
279-
* the underlying connection is ready. Additionally, it will only keep this
280-
* underlying connection in an "idle" state for 60s by default and will
281-
* automatically end the underlying connection when it is no longer needed.
279+
* the underlying connection is ready. This underlying connection will be
280+
* reused for all requests until it is closed. By default, idle connections
281+
* will be held open for 1ms (0.001s) when not used. The next request will
282+
* either reuse the existing connection or will automatically create a new
283+
* underlying connection if this idle time is expired.
282284
*
283285
* From a consumer side this means that you can start sending queries to the
284286
* database right away while the underlying connection may still be
@@ -351,17 +353,17 @@ public function createConnection(
351353
* $factory->createLazyConnection('localhost?timeout=0.5');
352354
* ```
353355
*
354-
* By default, this method will keep "idle" connection open for 60s and will
355-
* then end the underlying connection. The next request after an "idle"
356-
* connection ended will automatically create a new underlying connection.
357-
* This ensure you always get a "fresh" connection and as such should not be
358-
* confused with a "keepalive" or "heartbeat" mechanism, as this will not
359-
* actively try to probe the connection. You can explicitly pass a custom
360-
* idle timeout value in seconds (or use a negative number to not apply a
361-
* timeout) like this:
356+
* By default, idle connections will be held open for 1ms (0.001s) when not
357+
* used. The next request will either reuse the existing connection or will
358+
* automatically create a new underlying connection if this idle time is
359+
* expired. This ensures you always get a "fresh" connection and as such
360+
* should not be confused with a "keepalive" or "heartbeat" mechanism, as
361+
* this will not actively try to probe the connection. You can explicitly
362+
* pass a custom idle timeout value in seconds (or use a negative number to
363+
* not apply a timeout) like this:
362364
*
363365
* ```php
364-
* $factory->createLazyConnection('localhost?idle=0.1');
366+
* $factory->createLazyConnection('localhost?idle=10.0');
365367
* ```
366368
*
367369
* By default, the connection provides full UTF-8 support (using the

src/Io/LazyConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class LazyConnection extends EventEmitter implements ConnectionInterface
2727
private $disconnecting;
2828

2929
private $loop;
30-
private $idlePeriod = 60.0;
30+
private $idlePeriod = 0.001;
3131
private $idleTimer;
3232
private $pending = 0;
3333

tests/FactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ public function testConnectLazyWithValidAuthWillRunUntilIdleTimerAfterPingEvenWi
602602
{
603603
$factory = new Factory();
604604

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

608608
$connection->ping();

tests/Io/LazyConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function testQueryWillResolveAndStartTimerWithDefaultIntervalWhenQueryFro
218218
$factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base));
219219

220220
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
221-
$loop->expects($this->once())->method('addTimer')->with(60.0, $this->anything());
221+
$loop->expects($this->once())->method('addTimer')->with(0.001, $this->anything());
222222

223223
$connection = new LazyConnection($factory, '', $loop);
224224

0 commit comments

Comments
 (0)