Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce default idle time to 1ms #130

Merged
merged 1 commit into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ $redis->get('greeting')->then(function (string $greeting) {
$redis->incr('invocation')->then(function (int $n) {
echo 'This is invocation #' . $n . PHP_EOL;
});

// end connection once all pending requests have been resolved
$redis->end();
```

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

$redis->incr('hello');
$redis->end();
```

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

Internally, this class creates the underlying database connection only on
Internally, this class creates the underlying connection to Redis 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 close the underlying connection when
it is no longer needed.
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 commands to the
database right away while the underlying connection may still be
Expand Down Expand Up @@ -383,17 +380,17 @@ in seconds (or use a negative number to not apply a timeout) like this:
$redis = new Clue\React\Redis\RedisClient('localhost?timeout=0.5');
```

By default, this method will keep "idle" connections 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
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
$redis = new Clue\React\Redis\RedisClient('localhost?idle=0.1');
$redis = new Clue\React\Redis\RedisClient('localhost?idle=10.0');
```

If you need custom DNS, proxy or TLS settings, you can explicitly pass a
Expand Down
2 changes: 0 additions & 2 deletions examples/incr.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@
echo 'Error: ' . $e->getMessage() . PHP_EOL;
exit(1);
});

$redis->end();
2 changes: 0 additions & 2 deletions examples/publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@
echo 'Unable to publish: ' . $e->getMessage() . PHP_EOL;
exit(1);
});

$redis->end();
2 changes: 1 addition & 1 deletion src/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RedisClient extends EventEmitter
private $loop;

/** @var float */
private $idlePeriod = 60.0;
private $idlePeriod = 0.001;

/** @var ?TimerInterface */
private $idleTimer = null;
Expand Down
4 changes: 2 additions & 2 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function testPingLazy()
/**
* @doesNotPerformAssertions
*/
public function testPingLazyWillNotBlockLoopWhenIdleTimeIsSmall()
public function testPingLazyWillNotBlockLoop()
{
$redis = new RedisClient($this->uri . '?idle=0', null, $this->loop);
$redis = new RedisClient($this->uri, null, $this->loop);

$redis->ping();

Expand Down
2 changes: 1 addition & 1 deletion tests/RedisClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testPingWillResolveWhenUnderlyingClientResolvesPingAndStartIdleT
$deferred = new Deferred();
$this->factory->expects($this->once())->method('createClient')->willReturn($deferred->promise());

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

$promise = $this->redis->ping();
$deferred->resolve($client);
Expand Down