-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change implements the happy eyeballs strategy to bnring better support of IPv6 for hackney. It follows the recommendations iof the RFC 8305. https://datatracker.ietf.org/doc/html/rfc8305
- Loading branch information
Showing
11 changed files
with
720 additions
and
42 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,6 @@ Copyright (c) 2009, Erlang Training and Consulting Ltd. | |
Copyright (C) 1998 - 2014, Daniel Stenberg, <[email protected]>, et al. | ||
|
||
*) hackney_trace (C) 2015 under the Erlang Public LicensE | ||
|
||
*) hackney_cidr is based on inet_cidr 1.2.1. vendored for customer purpose. | ||
Copyright (c) 2024, Enki Multimedia , MIT License |
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,72 @@ | ||
-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) -> | ||
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 -> | ||
?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 | ||
end | ||
end. | ||
|
||
getaddrs(Hostname) -> | ||
IP6Addrs = [{Addr, 'inet6'} || Addr <- getbyname(Hostname, 'aaaa')], | ||
IP4Addrs = [{Addr, 'inet'} || Addr <- getbyname(Hostname, '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
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.