| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\React\Redis; |
| 4 |
|
| 5 |
use Clue\Redis\Protocol\Factory as ProtocolFactory; |
| 6 |
use Clue\Redis\Protocol\Model\ErrorReply; |
| 7 |
use Clue\Redis\Protocol\Model\ModelInterface; |
| 8 |
use Clue\Redis\Protocol\Model\MultiBulkReply; |
| 9 |
use Clue\Redis\Protocol\Parser\ParserException; |
| 10 |
use Clue\Redis\Protocol\Parser\ParserInterface; |
| 11 |
use Clue\Redis\Protocol\Serializer\SerializerInterface; |
| 12 |
use Evenement\EventEmitter; |
| 13 |
use React\Promise\Deferred; |
| 14 |
use React\Stream\DuplexStreamInterface; |
| 15 |
|
| 16 |
/** |
| 17 |
* @internal |
| 18 |
*/ |
| 19 |
class StreamingClient extends EventEmitter implements Client |
| 20 |
{ |
| 21 |
private $stream; |
| 22 |
private $parser; |
| 23 |
private $serializer; |
| 24 |
private $requests = array(); |
| 25 |
private $ending = false; |
| 26 |
private $closed = false; |
| 27 |
|
| 28 |
private $subscribed = 0; |
| 29 |
private $psubscribed = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param DuplexStreamInterface $stream |
| 33 |
* @param ?ParserInterface $parser |
| 34 |
* @param ?SerializerInterface $serializer |
| 35 |
*/ |
| 36 |
public function __construct(DuplexStreamInterface $stream, $parser = null, $serializer = null) |
| 37 |
{ |
| 38 |
// manual type checks to support legacy PHP < 7.1 |
| 39 |
assert($parser === null || $parser instanceof ParserInterface); |
| 40 |
assert($serializer === null || $serializer instanceof SerializerInterface); |
| 41 |
|
| 42 |
if ($parser === null || $serializer === null) { |
| 43 |
$factory = new ProtocolFactory(); |
| 44 |
if ($parser === null) { |
| 45 |
$parser = $factory->createResponseParser(); |
| 46 |
} |
| 47 |
if ($serializer === null) { |
| 48 |
$serializer = $factory->createSerializer(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$that = $this; |
| 53 |
$stream->on('data', function($chunk) use ($parser, $that) { |
| 54 |
try { |
| 55 |
$models = $parser->pushIncoming($chunk); |
| 56 |
} catch (ParserException $error) { |
| 57 |
$that->emit('error', array(new \UnexpectedValueException( |
| 58 |
'Invalid data received: ' . $error->getMessage() . ' (EBADMSG)', |
| 59 |
defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG : 77, |
| 60 |
$error |
| 61 |
))); |
| 62 |
$that->close(); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
foreach ($models as $data) { |
| 67 |
try { |
| 68 |
$that->handleMessage($data); |
| 69 |
} catch (\UnderflowException $error) { |
| 70 |
$that->emit('error', array($error)); |
| 71 |
$that->close(); |
| 72 |
return; |
| 73 |
} |
| 74 |
} |
| 75 |
}); |
| 76 |
|
| 77 |
$stream->on('close', array($this, 'close')); |
| 78 |
|
| 79 |
$this->stream = $stream; |
| 80 |
$this->parser = $parser; |
| 81 |
$this->serializer = $serializer; |
| 82 |
} |
| 83 |
|
| 84 |
public function __call($name, $args) |
| 85 |
{ |
| 86 |
$request = new Deferred(); |
| 87 |
$promise = $request->promise(); |
| 88 |
|
| 89 |
$name = strtolower($name); |
| 90 |
|
| 91 |
// special (p)(un)subscribe commands only accept a single parameter and have custom response logic applied |
| 92 |
static $pubsubs = array('subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe'); |
| 93 |
|
| 94 |
if ($this->ending) { |
| 95 |
$request->reject(new \RuntimeException( |
| 96 |
'Connection ' . ($this->closed ? 'closed' : 'closing'). ' (ENOTCONN)', |
| 97 |
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107 |
| 98 |
)); |
| 99 |
} elseif (count($args) !== 1 && in_array($name, $pubsubs)) { |
| 100 |
$request->reject(new \InvalidArgumentException( |
| 101 |
'PubSub commands limited to single argument (EINVAL)', |
| 102 |
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22 |
| 103 |
)); |
| 104 |
} elseif ($name === 'monitor') { |
| 105 |
$request->reject(new \BadMethodCallException( |
| 106 |
'MONITOR command explicitly not supported (ENOTSUP)', |
| 107 |
defined('SOCKET_ENOTSUP') ? SOCKET_ENOTSUP : (defined('SOCKET_EOPNOTSUPP') ? SOCKET_EOPNOTSUPP : 95) |
| 108 |
)); |
| 109 |
} else { |
| 110 |
$this->stream->write($this->serializer->getRequestMessage($name, $args)); |
| 111 |
$this->requests []= $request; |
| 112 |
} |
| 113 |
|
| 114 |
if (in_array($name, $pubsubs)) { |
| 115 |
$that = $this; |
| 116 |
$subscribed =& $this->subscribed; |
| 117 |
$psubscribed =& $this->psubscribed; |
| 118 |
|
| 119 |
$promise->then(function ($array) use ($that, &$subscribed, &$psubscribed) { |
| 120 |
$first = array_shift($array); |
| 121 |
|
| 122 |
// (p)(un)subscribe messages are to be forwarded |
| 123 |
$that->emit($first, $array); |
| 124 |
|
| 125 |
// remember number of (p)subscribe topics |
| 126 |
if ($first === 'subscribe' || $first === 'unsubscribe') { |
| 127 |
$subscribed = $array[1]; |
| 128 |
} else { |
| 129 |
$psubscribed = $array[1]; |
| 130 |
} |
| 131 |
}); |
| 132 |
} |
| 133 |
|
| 134 |
return $promise; |
| 135 |
} |
| 136 |
|
| 137 |
public function handleMessage(ModelInterface $message) |
| 138 |
{ |
| 139 |
if (($this->subscribed !== 0 || $this->psubscribed !== 0) && $message instanceof MultiBulkReply) { |
| 140 |
$array = $message->getValueNative(); |
| 141 |
$first = array_shift($array); |
| 142 |
|
| 143 |
// pub/sub messages are to be forwarded and should not be processed as request responses |
| 144 |
if (in_array($first, array('message', 'pmessage'))) { |
| 145 |
$this->emit($first, $array); |
| 146 |
return; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (!$this->requests) { |
| 151 |
throw new \UnderflowException( |
| 152 |
'Unexpected reply received, no matching request found (ENOMSG)', |
| 153 |
defined('SOCKET_ENOMSG') ? SOCKET_ENOMSG : 42 |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
$request = array_shift($this->requests); |
| 158 |
assert($request instanceof Deferred); |
| 159 |
|
| 160 |
if ($message instanceof ErrorReply) { |
| 161 |
$request->reject($message); |
| 162 |
} else { |
| 163 |
$request->resolve($message->getValueNative()); |
| 164 |
} |
| 165 |
|
| 166 |
if ($this->ending && !$this->requests) { |
| 167 |
$this->close(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
public function end() |
| 172 |
{ |
| 173 |
$this->ending = true; |
| 174 |
|
| 175 |
if (!$this->requests) { |
| 176 |
$this->close(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public function close() |
| 181 |
{ |
| 182 |
if ($this->closed) { |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
$this->ending = true; |
| 187 |
$this->closed = true; |
| 188 |
|
| 189 |
$remoteClosed = $this->stream->isReadable() === false && $this->stream->isWritable() === false; |
| 190 |
$this->stream->close(); |
| 191 |
|
| 192 |
$this->emit('close'); |
| 193 |
|
| 194 |
// reject all remaining requests in the queue |
| 195 |
while ($this->requests) { |
| 196 |
$request = array_shift($this->requests); |
| 197 |
assert($request instanceof Deferred); |
| 198 |
|
| 199 |
if ($remoteClosed) { |
| 200 |
$request->reject(new \RuntimeException( |
| 201 |
'Connection closed by peer (ECONNRESET)', |
| 202 |
defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104 |
| 203 |
)); |
| 204 |
} else { |
| 205 |
$request->reject(new \RuntimeException( |
| 206 |
'Connection closing (ECONNABORTED)', |
| 207 |
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103 |
| 208 |
)); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |