25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-20 17:27:00 +01:00

Fix SQL connections leakage

This commit is contained in:
Evgeny Khramtsov 2019-07-30 14:26:11 +03:00
parent 81996b153a
commit 209f5d32e1

View File

@ -343,31 +343,29 @@ connecting(connect, #state{host = Host} = State) ->
end, end,
case ConnectRes of case ConnectRes of
{ok, Ref} -> {ok, Ref} ->
erlang:monitor(process, Ref), try link(Ref) of
lists:foreach( _ ->
fun({{?PREPARE_KEY, _} = Key, _}) -> lists:foreach(
erase(Key); fun({{?PREPARE_KEY, _} = Key, _}) ->
(_) -> erase(Key);
ok (_) ->
end, get()), ok
PendingRequests = end, get()),
p1_queue:dropwhile( PendingRequests =
fun(Req) -> p1_queue:dropwhile(
p1_fsm:send_event(self(), Req), fun(Req) ->
true p1_fsm:send_event(self(), Req),
end, State#state.pending_requests), true
State1 = State#state{db_ref = Ref, end, State#state.pending_requests),
pending_requests = PendingRequests}, State1 = State#state{db_ref = Ref,
State2 = get_db_version(State1), pending_requests = PendingRequests},
{next_state, session_established, State2}; State2 = get_db_version(State1),
{error, Reason} -> {next_state, session_established, State2}
StartInterval = ejabberd_option:sql_start_interval(Host), catch _:Reason ->
?WARNING_MSG("~p connection failed:~n** Reason: ~p~n** " handle_reconnect(Reason, State)
"Retry after: ~B seconds", end;
[State#state.db_type, Reason, {error, Reason} ->
StartInterval div 1000]), handle_reconnect(Reason, State)
p1_fsm:send_event_after(StartInterval, connect),
{next_state, connecting, State}
end; end;
connecting(Event, State) -> connecting(Event, State) ->
?WARNING_MSG("Unexpected event in 'connecting': ~p", ?WARNING_MSG("Unexpected event in 'connecting': ~p",
@ -431,12 +429,8 @@ handle_sync_event(_Event, _From, StateName, State) ->
code_change(_OldVsn, StateName, State, _Extra) -> code_change(_OldVsn, StateName, State, _Extra) ->
{ok, StateName, State}. {ok, StateName, State}.
%% We receive the down signal when we loose the MySQL connection (we are handle_info({'EXIT', _Pid, Reason}, _StateName, State) ->
%% monitoring the connection) handle_reconnect(Reason, State);
handle_info({'DOWN', _MonitorRef, process, _Pid, _Info},
_StateName, State) ->
p1_fsm:send_event(self(), connect),
{next_state, connecting, State};
handle_info(Info, StateName, State) -> handle_info(Info, StateName, State) ->
?WARNING_MSG("Unexpected info in ~p: ~p", ?WARNING_MSG("Unexpected info in ~p: ~p",
[StateName, Info]), [StateName, Info]),
@ -460,6 +454,15 @@ print_state(State) -> State.
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
%%% Internal functions %%% Internal functions
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
handle_reconnect(Reason, #state{host = Host} = State) ->
StartInterval = ejabberd_option:sql_start_interval(Host),
?WARNING_MSG("~p connection failed:~n"
"** Reason: ~p~n"
"** Retry after: ~B seconds",
[State#state.db_type, Reason,
StartInterval div 1000]),
p1_fsm:send_event_after(StartInterval, connect),
{next_state, connecting, State}.
run_sql_cmd(Command, From, State, Timestamp) -> run_sql_cmd(Command, From, State, Timestamp) ->
QueryTimeout = query_timeout(State#state.host), QueryTimeout = query_timeout(State#state.host),