forked from clue/reactphp-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisClient.php
More file actions
392 lines (350 loc) · 12.7 KB
/
RedisClient.php
File metadata and controls
392 lines (350 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
namespace Clue\React\Redis;
use Clue\React\Redis\Io\Factory;
use Clue\React\Redis\Io\StreamingClient;
use Evenement\EventEmitter;
use React\EventLoop\Loop;
use React\Promise\PromiseInterface;
use React\Socket\ConnectorInterface;
use React\Stream\Util;
use function React\Promise\reject;
/**
* Simple interface for executing redis commands
*
* @event error(Exception $error)
* @event close()
*
* @event message($channel, $message)
* @event subscribe($channel, $numberOfChannels)
* @event unsubscribe($channel, $numberOfChannels)
*
* @event pmessage($pattern, $channel, $message)
* @event psubscribe($channel, $numberOfChannels)
* @event punsubscribe($channel, $numberOfChannels)
*/
class RedisClient extends EventEmitter
{
/** @var string */
private $uri;
/** @var Factory */
private $factory;
/** @var bool */
private $closed = false;
/** @var ?PromiseInterface<StreamingClient> */
private $promise = null;
/** @var float */
private $idlePeriod = 0.001;
/** @var ?\React\EventLoop\TimerInterface */
private $idleTimer = null;
/** @var int */
private $pending = 0;
/** @var array<string,bool> */
private $subscribed = [];
/** @var array<string,bool> */
private $psubscribed = [];
/**
* @param string $uri
* @param ?ConnectorInterface $connector
* @throws \InvalidArgumentException if $uri is not a valid Redis URI
*/
public function __construct(string $uri, ?ConnectorInterface $connector = null)
{
// support `redis+unix://` scheme for Unix domain socket (UDS) paths
if (preg_match('/^(redis\+unix:\/\/(?:[^@]*@)?)(.+)$/', $uri, $match)) {
$parts = parse_url($match[1] . 'localhost/' . $match[2]);
} else {
if (strpos($uri, '://') === false) {
$uri = 'redis://' . $uri;
}
$parts = parse_url($uri);
}
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], ['redis', 'rediss', 'redis+unix'])) {
$uri = (string) preg_replace(['/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'], '$1***$2', $uri);
throw new \InvalidArgumentException(
'Invalid Redis URI "' . $uri . '" (EINVAL)',
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22
);
}
$args = [];
\parse_str($parts['query'] ?? '', $args);
if (isset($args['idle'])) {
$this->idlePeriod = (float)$args['idle'];
}
$this->uri = $uri;
$this->factory = new Factory($connector);
}
/**
* The `__clone()` method is a magic method in PHP that is called
* automatically when a `RedisClient` instance is being cloned:
*
* ```php
* $original = new Clue\React\Redis\RedisClient($uri);
* $redis = clone $original;
* ```
*
* This method ensures the cloned client is created in a "fresh" state and
* any connection state is reset on the clone, matching how a new instance
* would start after returning from its constructor. Accordingly, the clone
* will always start in an unconnected and unclosed state, with no event
* listeners attached and ready to accept commands. Invoking any of the
* [commands](#commands) will establish a new connection as usual:
*
* ```php
* $redis = clone $original;
* $redis->set('name', 'Alice');
* ```
*
* This can be especially useful if the original connection is used for a
* [PubSub subscription](#pubsub) or when using blocking commands or similar
* and you need a control connection that is not affected by any of this.
* Both instances will not be directly affected by any operations performed,
* for example you can [`close()`](#close) either instance without also
* closing the other. Similarly, you can also clone a fresh instance from a
* closed state or overwrite a dead connection:
*
* ```php
* $redis->close();
* $redis = clone $redis;
* $redis->set('name', 'Alice');
* ```
*
* @return void
* @throws void
*/
public function __clone()
{
$this->closed = false;
$this->promise = null;
$this->idleTimer = null;
$this->pending = 0;
$this->subscribed = [];
$this->psubscribed = [];
$this->removeAllListeners();
}
/**
* @return PromiseInterface<StreamingClient>
*/
private function client(): PromiseInterface
{
if ($this->promise !== null) {
return $this->promise;
}
return $this->promise = $this->factory->createClient($this->uri)->then(function (StreamingClient $redis) {
// connection completed => remember only until closed
$redis->on('close', function () {
$this->promise = null;
// forward unsubscribe/punsubscribe events when underlying connection closes
$n = count($this->subscribed);
foreach ($this->subscribed as $channel => $_) {
$this->emit('unsubscribe', [$channel, --$n]);
}
$n = count($this->psubscribed);
foreach ($this->psubscribed as $pattern => $_) {
$this->emit('punsubscribe', [$pattern, --$n]);
}
$this->subscribed = $this->psubscribed = [];
if ($this->idleTimer !== null) {
Loop::cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
});
// keep track of all channels and patterns this connection is subscribed to
$redis->on('subscribe', function (string $channel) {
$this->subscribed[$channel] = true;
});
$redis->on('psubscribe', function (string $pattern) {
$this->psubscribed[$pattern] = true;
});
$redis->on('unsubscribe', function (string $channel) {
unset($this->subscribed[$channel]);
});
$redis->on('punsubscribe', function (string $pattern) {
unset($this->psubscribed[$pattern]);
});
Util::forwardEvents(
$redis,
$this,
[
'message',
'subscribe',
'unsubscribe',
'pmessage',
'psubscribe',
'punsubscribe',
]
);
return $redis;
}, function (\Throwable $e) {
assert($e instanceof \Exception);
// connection failed => discard connection attempt
$this->promise = null;
throw $e;
});
}
/**
* Invoke the given command and return a Promise that will be resolved when the command has been replied to
*
* This is a magic method that will be invoked when calling any redis
* command on this instance. See also `RedisClient::callAsync()`.
*
* @param string $name
* @param list<string|int|float> $args
* @return PromiseInterface<mixed>
* @see self::callAsync()
*/
public function __call(string $name, array $args): PromiseInterface
{
return $this->callAsync($name, ...$args);
}
/**
* Invoke a Redis command.
*
* For example, the [`GET` command](https://redis.io/commands/get) can be invoked
* like this:
*
* ```php
* $redis->callAsync('GET', 'name')->then(function (?string $name): void {
* echo 'Name: ' . ($name ?? 'Unknown') . PHP_EOL;
* }, function (Throwable $e): void {
* echo 'Error: ' . $e->getMessage() . PHP_EOL;
* });
* ```
*
* The `string $command` parameter can be any valid Redis command. All
* [Redis commands](https://redis.io/commands/) are available through this
* method. As an alternative, you may also use the magic
* [`__call()` method](#__call), but note that not all static analysis tools
* may understand this magic method. Listing all available commands is out
* of scope here, please refer to the
* [Redis command reference](https://redis.io/commands).
*
* The optional `string|int|float ...$args` parameter can be used to pass
* any additional arguments that some Redis commands may require or support.
* Values get passed directly to Redis, with any numeric values converted
* automatically since Redis only works with `string` arguments internally:
*
* ```php
* $redis->callAsync('SET', 'name', 'Alice', 'EX', 600);
* ```
*
* This method supports async operation and returns a [Promise](#promises)
* that eventually *fulfills* with its *results* on success or *rejects*
* with an `Exception` on error. See also [promises](#promises) for more
* details.
*
* @param string $command
* @param string|int|float ...$args
* @return PromiseInterface<mixed>
* @throws \TypeError if given $args are invalid
*/
public function callAsync(string $command, ...$args): PromiseInterface
{
$args = \array_map(function ($value): string {
/** @var mixed $value */
if (\is_string($value)) {
return $value;
} elseif (\is_int($value) || \is_float($value)) {
return \var_export($value, true);
} else {
throw new \TypeError('Argument must be of type string|int|float, ' . (\is_object($value) ? \get_class($value) : \gettype($value)) . ' given');
}
}, $args);
if ($this->closed) {
return reject(new \RuntimeException(
'Connection closed (ENOTCONN)',
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
));
}
return $this->client()->then(function (StreamingClient $redis) use ($command, $args): PromiseInterface {
$this->awake();
return $redis->callAsync($command, ...$args)->then(
function ($result) {
$this->idle();
return $result;
},
function (\Throwable $e) {
\assert($e instanceof \Exception);
$this->idle();
throw $e;
}
);
});
}
/**
* end connection once all pending requests have been replied to
*
* @return void
* @uses self::close() once all replies have been received
* @see self::close() for closing the connection immediately
*/
public function end(): void
{
if ($this->promise === null) {
$this->close();
}
if ($this->closed) {
return;
}
$this->client()->then(function (StreamingClient $redis) {
$redis->on('close', function () {
$this->close();
});
$redis->end();
});
}
/**
* close connection immediately
*
* This will emit the "close" event.
*
* @return void
* @see self::end() for closing the connection once the client is idle
*/
public function close(): void
{
if ($this->closed) {
return;
}
$this->closed = true;
// either close active connection or cancel pending connection attempt
if ($this->promise !== null) {
$this->promise->then(function (StreamingClient $redis) {
$redis->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
if ($this->promise !== null) {
$this->promise->cancel();
$this->promise = null;
}
}
if ($this->idleTimer !== null) {
Loop::cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
$this->emit('close');
$this->removeAllListeners();
}
private function awake(): void
{
++$this->pending;
if ($this->idleTimer !== null) {
Loop::cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
}
private function idle(): void
{
--$this->pending;
if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) {
$this->idleTimer = Loop::addTimer($this->idlePeriod, function () {
assert($this->promise instanceof PromiseInterface);
$this->promise->then(function (StreamingClient $redis) {
$redis->close();
});
$this->promise = null;
$this->idleTimer = null;
});
}
}
}