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
<?php
namespace React\Socket;
use React\EventLoop\LoopInterface;
use React\Promise\Timer;
use React\Promise\Timer\TimeoutException;
final class TimeoutConnector implements ConnectorInterface
{
private $connector;
private $timeout;
private $loop;
public function __construct(ConnectorInterface $connector, $timeout, LoopInterface $loop)
{
$this->connector = $connector;
$this->timeout = $timeout;
$this->loop = $loop;
}
public function connect($uri)
{
return Timer\timeout($this->connector->connect($uri), $this->timeout, $this->loop)->then(null, self::handler($uri));
}
/**
* Creates a static rejection handler that reports a proper error message in case of a timeout.
*
* This uses a private static helper method to ensure this closure is not
* bound to this instance and the exception trace does not include a
* reference to this instance and its connector stack as a result.
*
* @param string $uri
* @return callable
*/
private static function handler($uri)
{
return function (\Exception $e) use ($uri) {
if ($e instanceof TimeoutException) {
throw new \RuntimeException(
'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds',
\defined('SOCKET_ETIMEDOUT') ? \SOCKET_ETIMEDOUT : 0
);
}
throw $e;
};
}
}