forked from clue/reactphp-connection-manager-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionManagerConcurrent.php
More file actions
34 lines (30 loc) · 1.04 KB
/
ConnectionManagerConcurrent.php
File metadata and controls
34 lines (30 loc) · 1.04 KB
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
<?php
namespace ConnectionManager\Extra\Multiple;
use React\Promise;
use React\Promise\PromiseInterface;
class ConnectionManagerConcurrent extends ConnectionManagerConsecutive
{
public function connect($uri)
{
$all = array();
foreach ($this->managers as $connector) {
$all []= $connector->connect($uri);
}
return Promise\any($all)->then(function ($conn) use ($all) {
// a connection attempt succeeded
// => cancel all pending connection attempts
foreach ($all as $promise) {
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
$promise->cancel();
}
// if promise resolves despite cancellation, immediately close stream
$promise->then(function ($stream) use ($conn) {
if ($stream !== $conn) {
$stream->close();
}
});
}
return $conn;
});
}
}