| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\React\Redis; |
| 4 |
|
| 5 |
use Clue\Redis\Protocol\Factory as ProtocolFactory; |
| 6 |
use React\EventLoop\Loop; |
| 7 |
use React\EventLoop\LoopInterface; |
| 8 |
use React\Promise\Deferred; |
| 9 |
use React\Promise\Timer\TimeoutException; |
| 10 |
use React\Socket\ConnectionInterface; |
| 11 |
use React\Socket\Connector; |
| 12 |
use React\Socket\ConnectorInterface; |
| 13 |
|
| 14 |
class Factory |
| 15 |
{ |
| 16 |
/** @var LoopInterface */ |
| 17 |
private $loop; |
| 18 |
|
| 19 |
/** @var ConnectorInterface */ |
| 20 |
private $connector; |
| 21 |
|
| 22 |
/** @var ProtocolFactory */ |
| 23 |
private $protocol; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param ?LoopInterface $loop |
| 27 |
* @param ?ConnectorInterface $connector |
| 28 |
* @param ?ProtocolFactory $protocol (internal, should not usually be passed) |
| 29 |
*/ |
| 30 |
public function __construct($loop = null, $connector = null, $protocol = null) |
| 31 |
{ |
| 32 |
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1 |
| 33 |
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface'); |
| 34 |
} |
| 35 |
if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1 |
| 36 |
throw new \InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface'); |
| 37 |
} |
| 38 |
if ($protocol !== null && !$protocol instanceof ProtocolFactory) { // manual type check to support legacy PHP < 7.1 |
| 39 |
throw new \InvalidArgumentException('Argument #3 ($protocol) expected null|Clue\Redis\Protocol\Factory'); |
| 40 |
} |
| 41 |
|
| 42 |
$this->loop = $loop ?: Loop::get(); |
| 43 |
$this->connector = $connector ?: new Connector(array(), $this->loop); |
| 44 |
$this->protocol = $protocol ?: new ProtocolFactory(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Create Redis client connected to address of given redis instance |
| 49 |
* |
| 50 |
* @param string $uri Redis server URI to connect to |
| 51 |
* @return \React\Promise\PromiseInterface<Client,\Exception> Promise that will |
| 52 |
* be fulfilled with `Client` on success or rejects with `\Exception` on error. |
| 53 |
*/ |
| 54 |
public function createClient($uri) |
| 55 |
{ |
| 56 |
// support `redis+unix://` scheme for Unix domain socket (UDS) paths |
| 57 |
if (preg_match('/^(redis\+unix:\/\/(?:[^:]*:[^@]*@)?)(.+?)?$/', $uri, $match)) { |
| 58 |
$parts = parse_url($match[1] . 'localhost/' . $match[2]); |
| 59 |
} else { |
| 60 |
if (strpos($uri, '://') === false) { |
| 61 |
$uri = 'redis://' . $uri; |
| 62 |
} |
| 63 |
|
| 64 |
$parts = parse_url($uri); |
| 65 |
} |
| 66 |
|
| 67 |
$uri = preg_replace(array('/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'), '$1***$2', $uri); |
| 68 |
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('redis', 'rediss', 'redis+unix'))) { |
| 69 |
return \React\Promise\reject(new \InvalidArgumentException( |
| 70 |
'Invalid Redis URI given (EINVAL)', |
| 71 |
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22 |
| 72 |
)); |
| 73 |
} |
| 74 |
|
| 75 |
$args = array(); |
| 76 |
parse_str(isset($parts['query']) ? $parts['query'] : '', $args); |
| 77 |
|
| 78 |
$authority = $parts['host'] . ':' . (isset($parts['port']) ? $parts['port'] : 6379); |
| 79 |
if ($parts['scheme'] === 'rediss') { |
| 80 |
$authority = 'tls://' . $authority; |
| 81 |
} elseif ($parts['scheme'] === 'redis+unix') { |
| 82 |
$authority = 'unix://' . substr($parts['path'], 1); |
| 83 |
unset($parts['path']); |
| 84 |
} |
| 85 |
$connecting = $this->connector->connect($authority); |
| 86 |
|
| 87 |
$deferred = new Deferred(function ($_, $reject) use ($connecting, $uri) { |
| 88 |
// connection cancelled, start with rejecting attempt, then clean up |
| 89 |
$reject(new \RuntimeException( |
| 90 |
'Connection to ' . $uri . ' cancelled (ECONNABORTED)', |
| 91 |
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103 |
| 92 |
)); |
| 93 |
|
| 94 |
// either close successful connection or cancel pending connection attempt |
| 95 |
$connecting->then(function (ConnectionInterface $connection) { |
| 96 |
$connection->close(); |
| 97 |
}, function () { |
| 98 |
// ignore to avoid reporting unhandled rejection |
| 99 |
}); |
| 100 |
$connecting->cancel(); |
| 101 |
}); |
| 102 |
|
| 103 |
$protocol = $this->protocol; |
| 104 |
$promise = $connecting->then(function (ConnectionInterface $stream) use ($protocol) { |
| 105 |
return new StreamingClient($stream, $protocol->createResponseParser(), $protocol->createSerializer()); |
| 106 |
}, function (\Exception $e) use ($uri) { |
| 107 |
throw new \RuntimeException( |
| 108 |
'Connection to ' . $uri . ' failed: ' . $e->getMessage(), |
| 109 |
$e->getCode(), |
| 110 |
$e |
| 111 |
); |
| 112 |
}); |
| 113 |
|
| 114 |
// use `?password=secret` query or `user:secret@host` password form URL |
| 115 |
$pass = isset($args['password']) ? $args['password'] : (isset($parts['pass']) ? rawurldecode($parts['pass']) : null); |
| 116 |
if (isset($args['password']) || isset($parts['pass'])) { |
| 117 |
$pass = isset($args['password']) ? $args['password'] : rawurldecode($parts['pass']); |
| 118 |
$promise = $promise->then(function (StreamingClient $redis) use ($pass, $uri) { |
| 119 |
return $redis->auth($pass)->then( |
| 120 |
function () use ($redis) { |
| 121 |
return $redis; |
| 122 |
}, |
| 123 |
function (\Exception $e) use ($redis, $uri) { |
| 124 |
$redis->close(); |
| 125 |
|
| 126 |
$const = ''; |
| 127 |
$errno = $e->getCode(); |
| 128 |
if ($errno === 0) { |
| 129 |
$const = ' (EACCES)'; |
| 130 |
$errno = $e->getCode() ?: (defined('SOCKET_EACCES') ? SOCKET_EACCES : 13); |
| 131 |
} |
| 132 |
|
| 133 |
throw new \RuntimeException( |
| 134 |
'Connection to ' . $uri . ' failed during AUTH command: ' . $e->getMessage() . $const, |
| 135 |
$errno, |
| 136 |
$e |
| 137 |
); |
| 138 |
} |
| 139 |
); |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
// use `?db=1` query or `/1` path (skip first slash) |
| 144 |
if (isset($args['db']) || (isset($parts['path']) && $parts['path'] !== '/')) { |
| 145 |
$db = isset($args['db']) ? $args['db'] : substr($parts['path'], 1); |
| 146 |
$promise = $promise->then(function (StreamingClient $redis) use ($db, $uri) { |
| 147 |
return $redis->select($db)->then( |
| 148 |
function () use ($redis) { |
| 149 |
return $redis; |
| 150 |
}, |
| 151 |
function (\Exception $e) use ($redis, $uri) { |
| 152 |
$redis->close(); |
| 153 |
|
| 154 |
$const = ''; |
| 155 |
$errno = $e->getCode(); |
| 156 |
if ($errno === 0 && strpos($e->getMessage(), 'NOAUTH ') === 0) { |
| 157 |
$const = ' (EACCES)'; |
| 158 |
$errno = defined('SOCKET_EACCES') ? SOCKET_EACCES : 13; |
| 159 |
} elseif ($errno === 0) { |
| 160 |
$const = ' (ENOENT)'; |
| 161 |
$errno = defined('SOCKET_ENOENT') ? SOCKET_ENOENT : 2; |
| 162 |
} |
| 163 |
|
| 164 |
throw new \RuntimeException( |
| 165 |
'Connection to ' . $uri . ' failed during SELECT command: ' . $e->getMessage() . $const, |
| 166 |
$errno, |
| 167 |
$e |
| 168 |
); |
| 169 |
} |
| 170 |
); |
| 171 |
}); |
| 172 |
} |
| 173 |
|
| 174 |
$promise->then(array($deferred, 'resolve'), array($deferred, 'reject')); |
| 175 |
|
| 176 |
// use timeout from explicit ?timeout=x parameter or default to PHP's default_socket_timeout (60) |
| 177 |
$timeout = isset($args['timeout']) ? (float) $args['timeout'] : (int) ini_get("default_socket_timeout"); |
| 178 |
if ($timeout < 0) { |
| 179 |
return $deferred->promise(); |
| 180 |
} |
| 181 |
|
| 182 |
return \React\Promise\Timer\timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) use ($uri) { |
| 183 |
if ($e instanceof TimeoutException) { |
| 184 |
throw new \RuntimeException( |
| 185 |
'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds (ETIMEDOUT)', |
| 186 |
defined('SOCKET_ETIMEDOUT') ? SOCKET_ETIMEDOUT : 110 |
| 187 |
); |
| 188 |
} |
| 189 |
throw $e; |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Create Redis client connected to address of given redis instance |
| 195 |
* |
| 196 |
* @param string $target |
| 197 |
* @return Client |
| 198 |
*/ |
| 199 |
public function createLazyClient($target) |
| 200 |
{ |
| 201 |
return new LazyClient($target, $this, $this->loop); |
| 202 |
} |
| 203 |
} |