| 1 |
<?php |
| 2 |
|
| 3 |
namespace ConnectionManager\Extra; |
| 4 |
|
| 5 |
use React\Socket\ConnectorInterface; |
| 6 |
use React\EventLoop\LoopInterface; |
| 7 |
use React\Promise\Timer; |
| 8 |
|
| 9 |
class ConnectionManagerTimeout implements ConnectorInterface |
| 10 |
{ |
| 11 |
private $connectionManager; |
| 12 |
private $timeout; |
| 13 |
private $loop; |
| 14 |
|
| 15 |
public function __construct(ConnectorInterface $connectionManager, $timeout, LoopInterface $loop) |
| 16 |
{ |
| 17 |
$this->connectionManager = $connectionManager; |
| 18 |
$this->timeout = $timeout; |
| 19 |
$this->loop = $loop; |
| 20 |
} |
| 21 |
|
| 22 |
public function connect($uri) |
| 23 |
{ |
| 24 |
$promise = $this->connectionManager->connect($uri); |
| 25 |
|
| 26 |
return Timer\timeout($promise, $this->timeout, $this->loop)->then(null, function ($e) use ($promise) { |
| 27 |
// connection successfully established but timeout already expired => close successful connection |
| 28 |
$promise->then(function ($connection) { |
| 29 |
$connection->end(); |
| 30 |
}); |
| 31 |
|
| 32 |
throw $e; |
| 33 |
}); |
| 34 |
} |
| 35 |
} |