8000 Immediately try next connection when one attempt fails and fix possible race conditions (happy eyeballs) by clue · Pull Request #232 · reactphp/socket · GitHub
[go: up one dir, main page]

Skip to content

Immediately try next connection when one attempt fails and fix possible race conditions (happy eyeballs) #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix possible race condition when connector settles immediately
  • Loading branch information
clue committed May 14, 2020
commit 128a9d1dab1f47c056827425ca3c0416de81f616
5 changes: 3 additions & 2 deletions src/HappyEyeBallsConnectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public function check($resolve, $reject)
$ip = \array_shift($this->connectQueue);

$that = $this;
$that->connectionPromises[$ip] = $this->attemptConnection($ip)->then(function ($connection) use ($that, $ip, $resolve) {
$that->connectionPromises[$ip] = $this->attemptConnection($ip);
$that->connectionPromises[$ip]->then(function ($connection) use ($that, $ip, $resolve) {
unset($that->connectionPromises[$ip]);

$that->cleanUp();
Expand Down Expand Up @@ -180,7 +181,7 @@ public function check($resolve, $reject)

// Allow next connection attempt in 100ms: https://tools.ietf.org/html/rfc8305#section-5
// Only start timer when more IPs are queued or when DNS query is still pending (might add more IPs)
if (\count($this->connectQueue) > 0 || $this->resolved[Message::TYPE_A] === false || $this->resolved[Message::TYPE_AAAA] === false) {
if ($this->nextAttemptTimer === null && (\count($this->connectQueue) > 0 || $this->resolved[Message::TYPE_A] === false || $this->resolved[Message::TYPE_AAAA] === false)) {
$this->nextAttemptTimer = $this->loop->addTimer(self::CONNECTION_ATTEMPT_DELAY, function () use ($that, $resolve, $reject) {
$that->nextAttemptTimer = null;

Expand Down
34 changes: 30 additions & 4 deletions tests/HappyEyeBallsConnectionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,12 @@ public function testConnectWillStartConnectingWithAttemptTimerWhenOnlyIpv6Resolv
$loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer);
$loop->expects($this->once())->method('cancelTimer')->with($timer);

$deferred = new Deferred();
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$connector->expects($this->exactly(2))->method('connect')->withConsecutive(
array('tcp://[::1]:80?hostname=reactphp.org'),
array('tcp://[::2]:80?hostname=reactphp.org')
)->willReturnOnConsecutiveCalls(
$deferred->promise(),
\React\Promise\reject(new \RuntimeException()),
new Promise(function () { })
);

Expand All @@ -287,8 +286,6 @@ public function testConnectWillStartConnectingWithAttemptTimerWhenOnlyIpv6Resolv
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);

$builder->connect();

$deferred->reject(new \RuntimeException());
}

public function testConnectWillStartConnectingAndWillStartNextConnectionWithoutNewAttemptTimerWhenNextAttemptTimerFiresAfterIpv4Rejected()
Expand Down Expand Up @@ -566,4 +563,33 @@ public function testAttemptConnectionWillConnectViaConnectorToGivenIpv6WithAllUr

$builder->attemptConnection('::1');
}

public function testCheckCallsRejectFunctionImmediateWithoutLeavingDanglingPromiseWhenConnectorRejectsImmediately()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('connect')->with('tcp://[::1]:80/path?test=yes&hostname=reactphp.org#start')->willReturn(\React\Promise\reject(new \RuntimeException()));

$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
$resolver->expects($this->never())->method('resolveAll');

$uri = 'tcp://reactphp.org:80/path?test=yes#start';
$host = 'reactphp.org';
$parts = parse_url($uri);

$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);

$ref = new \ReflectionProperty($builder, 'connectQueue');
$ref->setAccessible(true);
$ref->setValue($builder, array('::1'));

$builder->check($this->expectCallableNever(), function () { });

$ref = new \ReflectionProperty($builder, 'connectionPromises');
$ref->setAccessible(true);
$promises = $ref->getValue($builder);

$this->assertEquals(array(), $promises);
}
}
0