Skip to content
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

fix conn_test_period use app env conf #163

Open
wants to merge 3 commits 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: 4 additions & 4 deletions include/emysql.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
port :: number(),
database :: string(),
encoding :: utf8 | latin1 | {utf8, utf8_unicode_ci} | {utf8, utf8_general_ci},
available=queue:new() :: queue(),
locked=gb_trees:empty() :: gb_tree(),
waiting=queue:new() :: queue(),
available=queue:new() :: queue:queue(),
locked=gb_trees:empty() :: gb_tree:tree(),
waiting=queue:new() :: queue:queue(),
start_cmds=[] :: string(),
conn_test_period=0 :: number(),
conn_test_period=emysql_app:conn_test_period() :: number(),
connect_timeout=infinity :: number() | infinity,
warnings=false :: boolean()}).

Expand Down
24 changes: 17 additions & 7 deletions src/emysql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ result_type(#eof_packet{}) -> eof.
-spec as_dict(Result) -> Dict
when
Result :: #result_packet{},
Dict :: dict().
Dict :: dict:dict().
as_dict(Res) -> emysql_conv:as_dict(Res).


Expand Down Expand Up @@ -746,12 +746,22 @@ as_record(Res, Recname, Fields, Fun) -> emysql_conv:as_record(Res, Recname, Fiel
%% @end doc: hd feb 11
%%
monitor_work(Connection0, Timeout, Args) when is_record(Connection0, emysql_connection) ->
Connection = case emysql_conn:need_test_connection(Connection0) of
true ->
emysql_conn:test_connection(Connection0, keep);
false ->
Connection0
end,
Connection = if
Connection0#emysql_connection.alive =:= false ->
case emysql_conn:reset_connection(emysql_conn_mgr:pools(), Connection0, keep) of
NewConn when is_record(NewConn, emysql_connection) ->
NewConn;
{error, FailedReset0} ->
exit({connection_down, {and_conn_reset_failed, FailedReset0}})
end;
true ->
case emysql_conn:need_test_connection(Connection0) of
true ->
emysql_conn:test_connection(Connection0, keep);
false ->
Connection0
end
end,

%% spawn a new process to do work, then monitor that process until
%% it either dies, returns data or times out.
Expand Down
2 changes: 1 addition & 1 deletion src/emysql_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ need_test_connection(Conn) ->
(Conn#emysql_connection.last_test_time + Conn#emysql_connection.test_period < now_seconds()).

now_seconds() ->
{M, S, _} = erlang:now(),
{M, S, _} = os:timestamp(),
M * 1000000 + S.

%%--------------------------------------------------------------------
Expand Down
14 changes: 11 additions & 3 deletions src/emysql_conn_mgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

-include("emysql.hrl").

-record(state, {pools, lockers = dict:new() :: dict()}).
-record(state, {pools, lockers = dict:new() :: dict:dict()}).

%%====================================================================
%% API
Expand Down Expand Up @@ -419,8 +419,16 @@ lock_next_connection(Available ,Locked, Who) ->
end.

connection_locked_at(Conn, MonitorRef) ->
Conn#emysql_connection{locked_at=lists:nth(2, tuple_to_list(now())),
monitor_ref = MonitorRef}.
Conn#emysql_connection{
locked_at = case catch erlang:unique_integer() of
{'EXIT', _} ->
lists:nth(2, tuple_to_list(apply(erlang, now, [])));
V ->
V
end,
monitor_ref = MonitorRef
}.


serve_waiting_pids(Pool) ->
{Waiting, Available, Locked, NewRefs} = serve_waiting_pids(Pool#pool.waiting, Pool#pool.available, Pool#pool.locked, []),
Expand Down