-
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
9 changed files
with
86 additions
and
40 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
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,60 @@ | ||
-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 inet_cidr:is_ipv6(Hostname) of | ||
true -> | ||
gen_tcp:connect(Hostname, Port, [inet6 | Opts], Timeout); | ||
false -> | ||
case inet_cidr:is_ipv4(Hostname) of | ||
true -> | ||
gen_tcp:connect(Hostname, Port, [inet | Opts], Timeout); | ||
false -> | ||
Self = self(), | ||
Addrs = getaddrs(Hostname), | ||
Pid = spawn_link( fun() -> try_connect(Addrs, Port, Opts, Self) end), | ||
MRef = erlang:monitor(process, Pid), | ||
receive | ||
{happy_connect, OK} -> | ||
erlang:demonitor(MRef, [flush]), | ||
OK; | ||
{'DOWN', MRef, _Type, _Pid, _Info} -> | ||
{'error', 'connect_error'} | ||
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 inet_res:getbyname(Hostname, Type) of | ||
{'ok', #hostent{h_addr_list=AddrList}} -> inet_cidr:usort_cidrs(AddrList); | ||
{error, _Reason} -> [] | ||
end. | ||
|
||
try_connect([], _Port, _Opts, ServerPid) -> | ||
ServerPid ! {hackney_happy, {error, nxdomain}}; | ||
try_connect([{IP, Type} | Rest], Port, Opts, ServerPid) -> | ||
case gen_tcp:connect(IP, Port, [Type | Opts], ?TIMEOUT) of | ||
{ok, Socket} = OK -> | ||
ok = gen_tcp:controlling_process(Socket, ServerPid), | ||
ServerPid ! {happy_connect, OK}; | ||
_Error -> | ||
try_connect(Rest, Port, Opts, ServerPid) | ||
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