Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace React\Socket;
4
 
5
use React\EventLoop\LoopInterface;
6
use React\Promise\Timer;
7
use React\Promise\Timer\TimeoutException;
8
 
9
final class TimeoutConnector implements ConnectorInterface
10
{
11
    private $connector;
12
    private $timeout;
13
    private $loop;
14
 
15
    public function __construct(ConnectorInterface $connector, $timeout, LoopInterface $loop)
16
    {
17
        $this->connector = $connector;
18
        $this->timeout = $timeout;
19
        $this->loop = $loop;
20
    }
21
 
22
    public function connect($uri)
23
    {
24
        return Timer\timeout($this->connector->connect($uri), $this->timeout, $this->loop)->then(null, self::handler($uri));
25
    }
26
 
27
    /**
28
     * Creates a static rejection handler that reports a proper error message in case of a timeout.
29
     *
30
     * This uses a private static helper method to ensure this closure is not
31
     * bound to this instance and the exception trace does not include a
32
     * reference to this instance and its connector stack as a result.
33
     *
34
     * @param string $uri
35
     * @return callable
36
     */
37
    private static function handler($uri)
38
    {
39
        return function (\Exception $e) use ($uri) {
40
            if ($e instanceof TimeoutException) {
41
                throw new \RuntimeException(
42
                    'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds',
43
                    \defined('SOCKET_ETIMEDOUT') ? \SOCKET_ETIMEDOUT : 0
44
                );
45
            }
46
 
47
            throw $e;
48
        };
49
    }
50
}