Skip to content

Commit ddfc9b2

Browse files
authored
Merge pull request #130 from clue-labs/short-idle
Reduce default idle time to 1ms
2 parents a7274cd + 7e5dc68 commit ddfc9b2

File tree

6 files changed

+14
-21
lines changed

6 files changed

+14
-21
lines changed

README.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ $redis->get('greeting')->then(function (string $greeting) {
8686
$redis->incr('invocation')->then(function (int $n) {
8787
echo 'This is invocation #' . $n . PHP_EOL;
8888
});
89-
90-
// end connection once all pending requests have been resolved
91-
$redis->end();
9289
```
9390

9491
See also the [examples](examples).
@@ -276,18 +273,18 @@ and keeps track of pending commands.
276273
$redis = new Clue\React\Redis\RedisClient('localhost:6379');
277274

278275
$redis->incr('hello');
279-
$redis->end();
280276
```
281277

282278
Besides defining a few methods, this interface also implements the
283279
`EventEmitterInterface` which allows you to react to certain events as documented below.
284280

285-
Internally, this class creates the underlying database connection only on
281+
Internally, this class creates the underlying connection to Redis only on
286282
demand once the first request is invoked on this instance and will queue
287283
all outstanding requests until the underlying connection is ready.
288-
Additionally, it will only keep this underlying connection in an "idle" state
289-
for 60s by default and will automatically close the underlying connection when
290-
it is no longer needed.
284+
This underlying connection will be reused for all requests until it is closed.
285+
By default, idle connections will be held open for 1ms (0.001s) when not used.
286+
The next request will either reuse the existing connection or will automatically
287+
create a new underlying connection if this idle time is expired.
291288

292289
From a consumer side this means that you can start sending commands to the
293290
database right away while the underlying connection may still be
@@ -383,17 +380,17 @@ in seconds (or use a negative number to not apply a timeout) like this:
383380
$redis = new Clue\React\Redis\RedisClient('localhost?timeout=0.5');
384381
```
385382

386-
By default, this method will keep "idle" connections open for 60s and will
387-
then end the underlying connection. The next request after an "idle"
388-
connection ended will automatically create a new underlying connection.
389-
This ensure you always get a "fresh" connection and as such should not be
383+
By default, idle connections will be held open for 1ms (0.001s) when not used.
384+
The next request will either reuse the existing connection or will automatically
385+
create a new underlying connection if this idle time is expired.
386+
This ensures you always get a "fresh" connection and as such should not be
390387
confused with a "keepalive" or "heartbeat" mechanism, as this will not
391388
actively try to probe the connection. You can explicitly pass a custom
392389
idle timeout value in seconds (or use a negative number to not apply a
393390
timeout) like this:
394391

395392
```php
396-
$redis = new Clue\React\Redis\RedisClient('localhost?idle=0.1');
393+
$redis = new Clue\React\Redis\RedisClient('localhost?idle=10.0');
397394
```
398395

399396
If you need custom DNS, proxy or TLS settings, you can explicitly pass a

examples/incr.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@
1515
echo 'Error: ' . $e->getMessage() . PHP_EOL;
1616
exit(1);
1717
});
18-
19-
$redis->end();

examples/publish.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@
1616
echo 'Unable to publish: ' . $e->getMessage() . PHP_EOL;
1717
exit(1);
1818
});
19-
20-
$redis->end();

src/RedisClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class RedisClient extends EventEmitter
4444
private $loop;
4545

4646
/** @var float */
47-
private $idlePeriod = 60.0;
47+
private $idlePeriod = 0.001;
4848

4949
/** @var ?TimerInterface */
5050
private $idleTimer = null;

tests/FunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public function testPingLazy()
5353
/**
5454
* @doesNotPerformAssertions
5555
*/
56-
public function testPingLazyWillNotBlockLoopWhenIdleTimeIsSmall()
56+
public function testPingLazyWillNotBlockLoop()
5757
{
58-
$redis = new RedisClient($this->uri . '?idle=0', null, $this->loop);
58+
$redis = new RedisClient($this->uri, null, $this->loop);
5959

6060
$redis->ping();
6161

tests/RedisClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testPingWillResolveWhenUnderlyingClientResolvesPingAndStartIdleT
6363
$deferred = new Deferred();
6464
$this->factory->expects($this->once())->method('createClient')->willReturn($deferred->promise());
6565

66-
$this->loop->expects($this->once())->method('addTimer')->with(60.0, $this->anything());
66+
$this->loop->expects($this->once())->method('addTimer')->with(0.001, $this->anything());
6767

6868
$promise = $this->redis->ping();
6969
$deferred->resolve($client);

0 commit comments

Comments
 (0)