-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
RemoteExecutor.php
133 lines (106 loc) · 3.69 KB
/
RemoteExecutor.php
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
<?php
namespace Amp\Redis;
use Amp\ByteStream\StreamException;
use Amp\Deferred;
use Amp\Promise;
use Amp\Socket;
use function Amp\asyncCall;
use function Amp\call;
use function Amp\delay;
final class RemoteExecutor implements QueryExecutor
{
/** @var Deferred[] */
private $queue = [];
/** @var Config */
private $config;
/** @var int */
private $database;
/** @var Promise|null */
private $connect;
/** @var Socket\Connector */
private $connector;
public function __construct(Config $config, ?Socket\Connector $connector = null)
{
$this->config = $config;
$this->database = $config->getDatabase();
$this->connector = $connector ?? Socket\connector();
}
/**
* @param string[] $args
* @param callable $transform
*
* @return Promise
*/
public function execute(array $args, callable $transform = null): Promise
{
return call(function () use ($args, $transform) {
$command = \strtolower($args[0] ?? '');
$connectPromise = $this->connect();
if ($command === 'quit') {
$this->connect = null;
}
/** @var RespSocket $resp */
$resp = yield $connectPromise;
$response = yield $this->enqueue($resp, ...$args);
if ($command === 'select') {
$this->database = (int) $args[1];
}
return $transform ? $transform($response) : $response;
});
}
private function enqueue(RespSocket $resp, string... $args): Promise
{
return call(function () use ($resp, $args) {
$deferred = new Deferred;
$this->queue[] = $deferred;
$resp->reference();
try {
yield $resp->write(...$args);
} catch (Socket\SocketException | StreamException $exception) {
throw new SocketException($exception);
}
return $deferred->promise();
});
}
private function connect(): Promise
{
if ($this->connect) {
return $this->connect;
}
return $this->connect = call(function () {
try {
/** @var RespSocket $resp */
$resp = yield connect($this->config->withDatabase($this->database), $this->connector);
} catch (\Throwable $connectException) {
yield delay(0); // ensure $this->connect is already assigned above in case of immediate failure
$this->connect = null;
throw $connectException;
}
asyncCall(function () use ($resp) {
try {
while ([$response] = yield $resp->read()) {
$deferred = \array_shift($this->queue);
if (!$this->queue) {
$resp->unreference();
}
if ($response instanceof \Throwable) {
$deferred->fail($response);
} else {
$deferred->resolve($response);
}
}
throw new SocketException('Socket to redis instance (' . $this->config->getConnectUri() . ') closed unexpectedly');
} catch (\Throwable $error) {
$queue = $this->queue;
$this->queue = [];
$this->connect = null;
while ($queue) {
$deferred = \array_shift($queue);
$deferred->fail($error);
}
}
});
return $resp;
});
}
}