Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 158
Fix cancelling happy eyeballs when IPv6 resolution is pending#311
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -65,9 +65,8 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector, | ||
| public function connect() | ||
| { | ||
| $timer = null; | ||
| $that = $this; | ||
| return new Promise\Promise(function ($resolve, $reject) use ($that, &$timer) { | ||
| return new Promise\Promise(function ($resolve, $reject) use ($that) { | ||
| $lookupResolve = function ($type) use ($that, $resolve, $reject) { | ||
| return function (array $ips) use ($that, $type, $resolve, $reject) { | ||
| unset($that->resolverPromises[$type]); | ||
| @@ -83,36 +82,36 @@ public function connect() | ||
| }; | ||
| $that->resolverPromises[Message::TYPE_AAAA] = $that->resolve(Message::TYPE_AAAA, $reject)->then($lookupResolve(Message::TYPE_AAAA)); | ||
| $that->resolverPromises[Message::TYPE_A] = $that->resolve(Message::TYPE_A, $reject)->then(function (array $ips) use ($that, &$timer) { | ||
| $that->resolverPromises[Message::TYPE_A] = $that->resolve(Message::TYPE_A, $reject)->then(function (array $ips) use ($that) { | ||
| // happy path: IPv6 has resolved already (or could not resolve), continue with IPv4 addresses | ||
| if ($that->resolved[Message::TYPE_AAAA] === true || !$ips) { | ||
| return $ips; | ||
| } | ||
| // Otherwise delay processing IPv4 lookup until short timer passes or IPv6 resolves in the meantime | ||
| $deferred = new Promise\Deferred(); | ||
| $deferred = new Promise\Deferred(function () use (&$ips) { | ||
| // discard all IPv4 addresses if cancelled | ||
| $ips = array(); | ||
| }); | ||
| $timer = $that->loop->addTimer($that::RESOLUTION_DELAY, function () use ($deferred, $ips) { | ||
| $deferred->resolve($ips); | ||
| }); | ||
| $that->resolverPromises[Message::TYPE_AAAA]->then(function () use ($that, $timer, $deferred, $ips) { | ||
| $that->resolverPromises[Message::TYPE_AAAA]->then(function () use ($that, $timer, $deferred, &$ips) { | ||
WyriHaximus marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| $that->loop->cancelTimer($timer); | ||
| $deferred->resolve($ips); | ||
| }); | ||
| return $deferred->promise(); | ||
| })->then($lookupResolve(Message::TYPE_A)); | ||
| }, function ($_, $reject) use ($that, &$timer) { | ||
| }, function ($_, $reject) use ($that) { | ||
| $reject(new \RuntimeException( | ||
| 'Connection to ' . $that->uri . ' cancelled' . (!$that->connectionPromises ? ' during DNS lookup' : '') . ' (ECONNABORTED)', | ||
| \defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103 | ||
| )); | ||
| $_ = $reject = null; | ||
| $that->cleanUp(); | ||
| if ($timer instanceof TimerInterface) { | ||
| $that->loop->cancelTimer($timer); | ||
| } | ||
| }); | ||
| } | ||
| @@ -247,13 +246,15 @@ public function cleanUp() | ||
| // clear list of outstanding IPs to avoid creating new connections | ||
| $this->connectQueue = array(); | ||
| // cancel pending connection attempts | ||
| foreach ($this->connectionPromises as $connectionPromise) { | ||
| if ($connectionPromise instanceof PromiseInterface && \method_exists($connectionPromise, 'cancel')) { | ||
| $connectionPromise->cancel(); | ||
| } | ||
| } | ||
| foreach ($this->resolverPromises as $resolverPromise) { | ||
| // cancel pending DNS resolution (cancel IPv4 first in case it is awaiting IPv6 resolution delay) | ||
| foreach (\array_reverse($this->resolverPromises) as $resolverPromise) { | ||
| if ($resolverPromise instanceof PromiseInterface && \method_exists($resolverPromise, 'cancel')) { | ||
| $resolverPromise->cancel(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.