-
Notifications
You must be signed in to change notification settings - Fork 427
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
improve connection to remote #737
Open
benoitc
wants to merge
13
commits into
master
Choose a base branch
from
happy_eyeballs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
55f2a67
rbreaking change: prefer IPV6
benoitc aa2b370
handle no_proxy env variable
benoitc 560d266
fix no_proxy env parsing
benoitc bed3ae3
fix url parsing, handle IPs
benoitc 420d855
getbyname: fix return when can't parse the address
benoitc 033b181
fix IP parsing
benoitc c719c78
handle localhost
benoitc 1ec3761
fix export
benoitc 231f0c2
remove undefined func
benoitc cf298cd
don't stop app at the endof the test
benoitc 8a45b03
match localhost directly
benoitc 5c0f6e5
address already parsed
benoitc 782bc86
fix dialyzer warnings
benoitc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
-module(hackney_happy). | ||
|
||
-export([connect/3, connect/4]). | ||
|
||
-include("hackney_internal.hrl"). | ||
-include_lib("kernel/include/inet.hrl"). | ||
|
||
-define(TIMEOUT, 250). | ||
-define(CONNECT_TIMEOUT, 5000). | ||
|
||
connect(Hostname, Port, Opts) -> | ||
connect(Hostname, Port, Opts, ?CONNECT_TIMEOUT). | ||
|
||
connect(Hostname, Port, Opts, Timeout) -> | ||
do_connect(parse_address(Hostname), Port, Opts, Timeout). | ||
|
||
do_connect(Hostname, Port, Opts, Timeout) when is_tuple(Hostname) -> | ||
case hackney_cidr:is_ipv6(Hostname) of | ||
true -> | ||
?report_debug("connect using IPv6", [{hostname, Hostname}, {port, Port}]), | ||
gen_tcp:connect(Hostname, Port, [inet6 | Opts], Timeout); | ||
false -> | ||
case hackney_cidr:is_ipv4(Hostname) of | ||
true -> | ||
?report_debug("connect using IPv4", [{hostname, Hostname}, {port, Port}]), | ||
gen_tcp:connect(Hostname, Port, [inet | Opts], Timeout); | ||
false -> | ||
{error, nxdomain} | ||
end | ||
end; | ||
do_connect(Hostname, Port, Opts, Timeout) -> | ||
?report_debug("happy eyeballs, try to connect using IPv6", [{hostname, Hostname}, {port, Port}]), | ||
Self = self(), | ||
Addrs = getaddrs(Hostname), | ||
Pid = spawn_link( fun() -> try_connect(Addrs, Port, Opts, Self, {error, nxdomain}) end), | ||
MRef = erlang:monitor(process, Pid), | ||
receive | ||
{happy_connect, OK} -> | ||
erlang:demonitor(MRef, [flush]), | ||
OK; | ||
{'DOWN', MRef, _Type, _Pid, Info} -> | ||
{'error', {'connect_error', Info}} | ||
after Timeout -> | ||
erlang:demonitor(MRef, [flush]), | ||
{error, connect_timeout} | ||
end. | ||
|
||
-spec parse_address(inet:ip_address() | binary() | string()) -> inet:ip_address() | string(). | ||
parse_address(IPTuple) when is_tuple(IPTuple) -> IPTuple; | ||
parse_address(IPBin) when is_binary(IPBin) -> | ||
parse_address(binary_to_list(IPBin)); | ||
%% IPv6 string with brackets | ||
parse_address("[" ++ IPString) -> | ||
parse_address(lists:sublist(IPString, length(IPString) - 1)); | ||
parse_address(IPString) -> | ||
case inet:parse_address(IPString) of | ||
{ok, IP} -> IP; | ||
{error, _} -> IPString | ||
end. | ||
|
||
-spec getaddrs(string()) -> [{inet:ip_address(), 'inet6' | 'inet'}]. | ||
getaddrs("localhost") -> | ||
[{{0,0,0,0,0,0,0,1}, 'inet6'}, {{127,0,0,1}, 'inet'}]; | ||
getaddrs(Name) -> | ||
IP6Addrs = [{Addr, 'inet6'} || Addr <- getbyname(Name, 'aaaa')], | ||
IP4Addrs = [{Addr, 'inet'} || Addr <- getbyname(Name, 'a')], | ||
IP6Addrs ++ IP4Addrs. | ||
|
||
getbyname(Hostname, Type) -> | ||
case (catch inet_res:getbyname(Hostname, Type)) of | ||
{'ok', #hostent{h_addr_list=AddrList}} -> lists:usort(AddrList); | ||
{error, _Reason} -> []; | ||
Else -> | ||
%% ERLANG 22 has an issue when g matching somee DNS server messages | ||
?report_debug("DNS error", [{hostname, Hostname} | ||
,{type, Type} | ||
,{error, Else}]), | ||
[] | ||
end. | ||
|
||
try_connect([], _Port, _Opts, ServerPid, LastError) -> | ||
?report_trace("happy eyeball: failed to connect", [{error, LastError}]), | ||
ServerPid ! {hackney_happy, LastError}; | ||
try_connect([{IP, Type} | Rest], Port, Opts, ServerPid, _LastError) -> | ||
?report_trace("try to connect", [{ip, IP}, {type, Type}]), | ||
case gen_tcp:connect(IP, Port, [Type | Opts], ?TIMEOUT) of | ||
{ok, Socket} = OK -> | ||
?report_trace("success to connect", [{ip, IP}, {type, Type}]), | ||
ok = gen_tcp:controlling_process(Socket, ServerPid), | ||
ServerPid ! {happy_connect, OK}; | ||
Error -> | ||
try_connect(Rest, Port, Opts, ServerPid, Error) | ||
end. |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋 @benoitc
I wonder if it would make sense to "intersperse" the addresses here like [ipv6, ipv4, ipv6, ipv4, etc.]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sort of like this: https://github.com/ruslandoga/happy_tcp/blob/afbf889d566e6de743f66978f44b831987acbd6e/lib/happy_tcp.ex#L61-L81