diff options
Diffstat (limited to 'vendor/react/socket/src/TimeoutConnector.php')
-rw-r--r-- | vendor/react/socket/src/TimeoutConnector.php | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/vendor/react/socket/src/TimeoutConnector.php b/vendor/react/socket/src/TimeoutConnector.php new file mode 100644 index 0000000..332369f --- /dev/null +++ b/vendor/react/socket/src/TimeoutConnector.php @@ -0,0 +1,51 @@ +<?php + +namespace React\Socket; + +use React\EventLoop\Loop; +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 = null) + { + $this->connector = $connector; + $this->timeout = $timeout; + $this->loop = $loop ?: Loop::get(); + } + + 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 (ETIMEDOUT)', + \defined('SOCKET_ETIMEDOUT') ? \SOCKET_ETIMEDOUT : 110 + ); + } + + throw $e; + }; + } +} |