Skip to content

support "gen_tcp:connect" options #15

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Version 1.0.17

* Support "gen_tcp" connect options (ie. keepalive, send_timeout, sndbuf, recbuf...)

# Version 1.0.16

* Support OTP-23 on travis

# Version 1.0.15

* Fix warnings
Expand Down
2 changes: 1 addition & 1 deletion src/p1_mysql_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ do_recv(LogFun, RecvPid, SeqNum) when is_function(LogFun);
%% Returns : void() | does not return
%%--------------------------------------------------------------------
init(Host, Port, User, Password, Database, ConnectTimeout, LogFun, Parent, SSLOpts) ->
case p1_mysql_recv:start_link(Host, Port, ConnectTimeout, LogFun, self()) of
case p1_mysql_recv:start_link(Host, Port, ConnectTimeout, LogFun, self(), SSLOpts) of
{ok, RecvPid, Sock0} ->
case mysql_init(Sock0, RecvPid, User, Password, LogFun, SSLOpts) of
{ok, {SockMod, RawSock} = Sock, Version} ->
Expand Down
32 changes: 21 additions & 11 deletions src/p1_mysql_recv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
%%--------------------------------------------------------------------
%% External exports (should only be used by the 'p1_mysql_conn' module)
%%--------------------------------------------------------------------
-export([start_link/5
-export([start_link/5,
start_link/6
]).

-include_lib("kernel/include/inet.hrl").
Expand All @@ -56,6 +57,7 @@
%% Port = integer()
%% LogFun = undefined | function() of arity 3
%% Parent = pid(), process that should get received frames
%% Options = [atom() | {atom{}, any{}}] gen_tcp options
%% Descrip.: Start a process that connects to Host:Port and waits for
%% data. When it has received a MySQL frame, it sends it to
%% Parent and waits for the next frame.
Expand All @@ -65,11 +67,14 @@
%% Socket = term(), gen_tcp socket
%% Reason = atom() | string()
%%--------------------------------------------------------------------
start_link(Host, Port, ConnectTimeout, LogFun, Parent) when is_list(Host),
start_link(Host, Port, ConnectTimeout, LogFun, Parent) ->
start_link(Host, Port, ConnectTimeout, LogFun, Parent, []).

start_link(Host, Port, ConnectTimeout, LogFun, Parent, Options) when is_list(Host),
is_integer(Port) ->
RecvPid =
spawn_link(fun () ->
init(Host, Port, LogFun, Parent)
init(Host, Port, LogFun, Parent, Options)
end),
%% wait for the socket from the spawned pid
receive
Expand Down Expand Up @@ -97,8 +102,8 @@ start_link(Host, Port, ConnectTimeout, LogFun, Parent) when is_list(Host),
%% Descrip.: Connect to Host:Port and then enter receive-loop.
%% Returns : error | never returns
%%--------------------------------------------------------------------
init(Host, Port, LogFun, Parent) ->
case connect(Host, Port) of
init(Host, Port, LogFun, Parent, Options) ->
case connect(Host, Port, Options) of
{ok, Sock} ->
Parent ! {p1_mysql_recv, self(), init, {ok, Sock}},
State = #state{socket = Sock,
Expand Down Expand Up @@ -182,22 +187,27 @@ sendpacket(Parent, Data) ->
%%--------------------------------------------------------------------
%% Connecting stuff
%%--------------------------------------------------------------------
connect(Host, Port) ->
connect(Host, Port, Options) ->
case lookup(Host) of
{ok, AddrsFamilies} ->
do_connect(AddrsFamilies, Port, {error, nxdomain});
do_connect(AddrsFamilies, Port, Options, {error, nxdomain});
{error, _} = Err ->
Err
end.

do_connect([{IP, Family}|AddrsFamilies], Port, _Err) ->
case gen_tcp:connect(IP, Port, [binary, {packet, 0}, Family]) of
do_connect([{IP, Family}|AddrsFamilies], Port, Options, _Err) ->
SupportedOptions = inet:options() -- [binary,packet,inet,inet6],
OtherOpts = lists:filter(fun(Opt) ->
OptKey = case Opt of {K, _} -> K; K -> K end,
lists:member(OptKey, SupportedOptions)
end, Options),
case gen_tcp:connect(IP, Port, [binary, {packet, 0}, Family | OtherOpts]) of
{ok, Sock} ->
{ok, Sock};
{error, _} = Err ->
do_connect(AddrsFamilies, Port, Err)
do_connect(AddrsFamilies, Port, Options, Err)
end;
do_connect([], _Port, Err) ->
do_connect([], _Port, _Options, Err) ->
Err.

lookup(Host) ->
Expand Down