| 1 |
<?php |
| 2 |
|
| 3 |
namespace ConnectionManager\Extra\Multiple; |
| 4 |
|
| 5 |
use React\Promise; |
| 6 |
use React\Promise\PromiseInterface; |
| 7 |
|
| 8 |
class ConnectionManagerConcurrent extends ConnectionManagerConsecutive |
| 9 |
{ |
| 10 |
public function connect($uri) |
| 11 |
{ |
| 12 |
$all = array(); |
| 13 |
foreach ($this->managers as $connector) { |
| 14 |
$all []= $connector->connect($uri); |
| 15 |
} |
| 16 |
return Promise\any($all)->then(function ($conn) use ($all) { |
| 17 |
// a connection attempt succeeded |
| 18 |
// => cancel all pending connection attempts |
| 19 |
foreach ($all as $promise) { |
| 20 |
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) { |
| 21 |
$promise->cancel(); |
| 22 |
} |
| 23 |
|
| 24 |
// if promise resolves despite cancellation, immediately close stream |
| 25 |
$promise->then(function ($stream) use ($conn) { |
| 26 |
if ($stream !== $conn) { |
| 27 |
$stream->close(); |
| 28 |
} |
| 29 |
}); |
| 30 |
} |
| 31 |
return $conn; |
| 32 |
}); |
| 33 |
} |
| 34 |
} |