Move cancel_timer/1 function into 'misc' module

This commit is contained in:
Holger Weiss 2018-07-17 20:50:58 +02:00
parent 6601f182c4
commit 68c9328a9c
11 changed files with 46 additions and 93 deletions

View File

@ -404,7 +404,7 @@ active(#body{attrs = Attrs, size = Size} = Req, From,
{next_state, active, {next_state, active,
State2#state{shaped_receivers = Q}} State2#state{shaped_receivers = Q}}
catch error:full -> catch error:full ->
cancel_timer(TRef), misc:cancel_timer(TRef),
RID = get_attr(rid, Attrs), RID = get_attr(rid, Attrs),
reply_stop(State1, reply_stop(State1,
#body{http_reason = <<"Too many requests">>, #body{http_reason = <<"Too many requests">>,
@ -552,7 +552,7 @@ handle_sync_event({send_xml, El}, _From, StateName,
State2 = case p1_queue:out(State1#state.shaped_receivers) State2 = case p1_queue:out(State1#state.shaped_receivers)
of of
{{value, {TRef, From, Body}}, Q} -> {{value, {TRef, From, Body}}, Q} ->
cancel_timer(TRef), misc:cancel_timer(TRef),
p1_fsm:send_event(self(), {Body, From}), p1_fsm:send_event(self(), {Body, From}),
State1#state{shaped_receivers = Q}; State1#state{shaped_receivers = Q};
_ -> State1 _ -> State1
@ -1035,12 +1035,8 @@ buf_out(Buf, I, Els) ->
{empty, _} -> buf_out(Buf, 0, Els) {empty, _} -> buf_out(Buf, 0, Els)
end. end.
cancel_timer(TRef) when is_reference(TRef) ->
p1_fsm:cancel_timer(TRef);
cancel_timer(_) -> false.
restart_timer(TRef, Timeout, Msg) -> restart_timer(TRef, Timeout, Msg) ->
cancel_timer(TRef), misc:cancel_timer(TRef),
erlang:start_timer(timer:seconds(Timeout), self(), Msg). erlang:start_timer(timer:seconds(Timeout), self(), Msg).
restart_inactivity_timer(#state{inactivity_timeout = restart_inactivity_timer(#state{inactivity_timeout =
@ -1057,7 +1053,7 @@ restart_inactivity_timer(#state{inactivity_timer =
stop_inactivity_timer(#state{inactivity_timer = TRef} = stop_inactivity_timer(#state{inactivity_timer = TRef} =
State) -> State) ->
cancel_timer(TRef), misc:cancel_timer(TRef),
State#state{inactivity_timer = undefined}. State#state{inactivity_timer = undefined}.
restart_wait_timer(#state{wait_timer = TRef, restart_wait_timer(#state{wait_timer = TRef,
@ -1067,7 +1063,7 @@ restart_wait_timer(#state{wait_timer = TRef,
State#state{wait_timer = NewTRef}. State#state{wait_timer = NewTRef}.
stop_wait_timer(#state{wait_timer = TRef} = State) -> stop_wait_timer(#state{wait_timer = TRef} = State) ->
cancel_timer(TRef), State#state{wait_timer = undefined}. misc:cancel_timer(TRef), State#state{wait_timer = undefined}.
start_shaper_timer(Timeout) -> start_shaper_timer(Timeout) ->
erlang:start_timer(Timeout, self(), shaper_timeout). erlang:start_timer(Timeout, self(), shaper_timeout).

View File

@ -517,11 +517,7 @@ recv_data(Port, TRef, Buf) ->
end. end.
return(Port, TRef, Result) -> return(Port, TRef, Result) ->
case erlang:cancel_timer(TRef) of misc:cancel_timer(TRef),
false ->
receive {timeout, TRef, _} -> ok after 0 -> ok end;
_ -> ok
end,
catch port_close(Port), catch port_close(Port),
Result. Result.
@ -561,7 +557,7 @@ check_captcha(Id, ProvidedKey) ->
case ets:lookup(captcha, Id) of case ets:lookup(captcha, Id) of
[#captcha{pid = Pid, args = Args, key = ValidKey, tref = Tref}] -> [#captcha{pid = Pid, args = Args, key = ValidKey, tref = Tref}] ->
ets:delete(captcha, Id), ets:delete(captcha, Id),
erlang:cancel_timer(Tref), misc:cancel_timer(Tref),
if ValidKey == ProvidedKey -> if ValidKey == ProvidedKey ->
callback(captcha_succeed, Pid, Args), callback(captcha_succeed, Pid, Args),
captcha_valid; captcha_valid;

View File

@ -256,7 +256,7 @@ handle_info({timeout, Timer, _}, StateName,
#state{ping_timer = Timer, ws = {_, WsPid}} = StateData) -> #state{ping_timer = Timer, ws = {_, WsPid}} = StateData) ->
case StateData#state.pong_expected of case StateData#state.pong_expected of
false -> false ->
cancel_timer(StateData#state.ping_timer), misc:cancel_timer(StateData#state.ping_timer),
PingTimer = erlang:start_timer(StateData#state.ping_interval, PingTimer = erlang:start_timer(StateData#state.ping_interval,
self(), []), self(), []),
WsPid ! {ping, <<>>}, WsPid ! {ping, <<>>},
@ -282,10 +282,10 @@ terminate(_Reason, _StateName, StateData) ->
ok. ok.
setup_timers(StateData) -> setup_timers(StateData) ->
cancel_timer(StateData#state.timer), misc:cancel_timer(StateData#state.timer),
Timer = erlang:start_timer(StateData#state.timeout, Timer = erlang:start_timer(StateData#state.timeout,
self(), []), self(), []),
cancel_timer(StateData#state.ping_timer), misc:cancel_timer(StateData#state.ping_timer),
PingTimer = case StateData#state.ping_interval of PingTimer = case StateData#state.ping_interval of
0 -> StateData#state.ping_timer; 0 -> StateData#state.ping_timer;
V -> erlang:start_timer(V, self(), []) V -> erlang:start_timer(V, self(), [])
@ -293,10 +293,6 @@ setup_timers(StateData) ->
StateData#state{timer = Timer, ping_timer = PingTimer, StateData#state{timer = Timer, ping_timer = PingTimer,
pong_expected = false}. pong_expected = false}.
cancel_timer(Timer) ->
erlang:cancel_timer(Timer),
receive {timeout, Timer, _} -> ok after 0 -> ok end.
get_human_html_xmlel() -> get_human_html_xmlel() ->
Heading = <<"ejabberd ", (misc:atom_to_binary(?MODULE))/binary>>, Heading = <<"ejabberd ", (misc:atom_to_binary(?MODULE))/binary>>,
#xmlel{name = <<"html">>, #xmlel{name = <<"html">>,

View File

@ -88,7 +88,7 @@ handle_event({set_alarm, {system_memory_high_watermark, _}}, State) ->
handle_overload(State), handle_overload(State),
{ok, restart_timer(State)}; {ok, restart_timer(State)};
handle_event({clear_alarm, system_memory_high_watermark}, State) -> handle_event({clear_alarm, system_memory_high_watermark}, State) ->
cancel_timer(State#state.tref), misc:cancel_timer(State#state.tref),
{ok, State#state{tref = undefined}}; {ok, State#state{tref = undefined}};
handle_event({set_alarm, {process_memory_high_watermark, Pid}}, State) -> handle_event({set_alarm, {process_memory_high_watermark, Pid}}, State) ->
case proc_stat(Pid, get_app_pids()) of case proc_stat(Pid, get_app_pids()) of
@ -220,23 +220,10 @@ proc_stat(Pid, AppPids) ->
-spec restart_timer(#state{}) -> #state{}. -spec restart_timer(#state{}) -> #state{}.
restart_timer(State) -> restart_timer(State) ->
cancel_timer(State#state.tref), misc:cancel_timer(State#state.tref),
TRef = erlang:start_timer(?CHECK_INTERVAL, self(), handle_overload), TRef = erlang:start_timer(?CHECK_INTERVAL, self(), handle_overload),
State#state{tref = TRef}. State#state{tref = TRef}.
-spec cancel_timer(reference()) -> ok.
cancel_timer(undefined) ->
ok;
cancel_timer(TRef) ->
case erlang:cancel_timer(TRef) of
false ->
receive {timeout, TRef, _} -> ok
after 0 -> ok
end;
_ ->
ok
end.
-spec format_apps(dict:dict()) -> io:data(). -spec format_apps(dict:dict()) -> io:data().
format_apps(Apps) -> format_apps(Apps) ->
AppList = lists:reverse(lists:keysort(2, dict:to_list(Apps))), AppList = lists:reverse(lists:keysort(2, dict:to_list(Apps))),

View File

@ -659,7 +659,7 @@ handle_info({Tag, _Socket, Data}, connecting, S)
{next_state, connecting, S}; {next_state, connecting, S};
handle_info({Tag, _Socket, Data}, wait_bind_response, S) handle_info({Tag, _Socket, Data}, wait_bind_response, S)
when Tag == tcp; Tag == ssl -> when Tag == tcp; Tag == ssl ->
cancel_timer(S#eldap.bind_timer), misc:cancel_timer(S#eldap.bind_timer),
case catch recvd_wait_bind_response(Data, S) of case catch recvd_wait_bind_response(Data, S) of
bound -> dequeue_commands(S); bound -> dequeue_commands(S);
{fail_bind, Reason} -> {fail_bind, Reason} ->
@ -847,14 +847,14 @@ recvd_packet(Pkt, S) ->
if Reason == success; Reason == sizeLimitExceeded -> if Reason == success; Reason == sizeLimitExceeded ->
{Res, Ref} = polish(Result_so_far), {Res, Ref} = polish(Result_so_far),
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
{reply, {reply,
#eldap_search_result{entries = Res, #eldap_search_result{entries = Res,
referrals = Ref}, referrals = Ref},
From, S#eldap{dict = New_dict}}; From, S#eldap{dict = New_dict}};
true -> true ->
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
{reply, {error, Reason}, From, {reply, {error, Reason}, From,
S#eldap{dict = New_dict}} S#eldap{dict = New_dict}}
end; end;
@ -863,37 +863,37 @@ recvd_packet(Pkt, S) ->
{ok, S#eldap{dict = New_dict}}; {ok, S#eldap{dict = New_dict}};
{addRequest, {addResponse, Result}} -> {addRequest, {addResponse, Result}} ->
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
Reply = check_reply(Result, From), Reply = check_reply(Result, From),
{reply, Reply, From, S#eldap{dict = New_dict}}; {reply, Reply, From, S#eldap{dict = New_dict}};
{delRequest, {delResponse, Result}} -> {delRequest, {delResponse, Result}} ->
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
Reply = check_reply(Result, From), Reply = check_reply(Result, From),
{reply, Reply, From, S#eldap{dict = New_dict}}; {reply, Reply, From, S#eldap{dict = New_dict}};
{modifyRequest, {modifyResponse, Result}} -> {modifyRequest, {modifyResponse, Result}} ->
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
Reply = check_reply(Result, From), Reply = check_reply(Result, From),
{reply, Reply, From, S#eldap{dict = New_dict}}; {reply, Reply, From, S#eldap{dict = New_dict}};
{modDNRequest, {modDNResponse, Result}} -> {modDNRequest, {modDNResponse, Result}} ->
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
Reply = check_reply(Result, From), Reply = check_reply(Result, From),
{reply, Reply, From, S#eldap{dict = New_dict}}; {reply, Reply, From, S#eldap{dict = New_dict}};
{bindRequest, {bindResponse, Result}} -> {bindRequest, {bindResponse, Result}} ->
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
Reply = check_bind_reply(Result, From), Reply = check_bind_reply(Result, From),
{reply, Reply, From, S#eldap{dict = New_dict}}; {reply, Reply, From, S#eldap{dict = New_dict}};
{extendedReq, {extendedResp, Result}} -> {extendedReq, {extendedResp, Result}} ->
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
Reply = check_extended_reply(Result, From), Reply = check_extended_reply(Result, From),
{reply, Reply, From, S#eldap{dict = New_dict}}; {reply, Reply, From, S#eldap{dict = New_dict}};
{OtherName, OtherResult} -> {OtherName, OtherResult} ->
New_dict = dict:erase(Id, Dict), New_dict = dict:erase(Id, Dict),
cancel_timer(Timer), misc:cancel_timer(Timer),
{reply, {reply,
{error, {invalid_result, OtherName, OtherResult}}, {error, {invalid_result, OtherName, OtherResult}},
From, S#eldap{dict = New_dict}} From, S#eldap{dict = New_dict}}
@ -968,16 +968,11 @@ check_id(_, _) -> throw({error, wrong_bind_id}).
%% General Helpers %% General Helpers
%%----------------------------------------------------------------------- %%-----------------------------------------------------------------------
cancel_timer(Timer) ->
erlang:cancel_timer(Timer),
receive {timeout, Timer, _} -> ok after 0 -> ok end.
close_and_retry(S, Timeout) -> close_and_retry(S, Timeout) ->
catch (S#eldap.sockmod):close(S#eldap.fd), catch (S#eldap.sockmod):close(S#eldap.fd),
Queue = dict:fold(fun (_Id, Queue = dict:fold(fun (_Id,
[{Timer, Command, From, _Name} | _], Q) -> [{Timer, Command, From, _Name} | _], Q) ->
cancel_timer(Timer), misc:cancel_timer(Timer),
queue:in_r({Command, From}, Q); queue:in_r({Command, From}, Q);
(_, _, Q) -> Q (_, _, Q) -> Q
end, end,

View File

@ -36,7 +36,7 @@
compile_exprs/2, join_atoms/2, try_read_file/1, get_descr/2, compile_exprs/2, join_atoms/2, try_read_file/1, get_descr/2,
css_dir/0, img_dir/0, js_dir/0, msgs_dir/0, sql_dir/0, lua_dir/0, css_dir/0, img_dir/0, js_dir/0, msgs_dir/0, sql_dir/0, lua_dir/0,
read_css/1, read_img/1, read_js/1, read_lua/1, try_url/1, read_css/1, read_img/1, read_js/1, read_lua/1, try_url/1,
intersection/2, format_val/1]). intersection/2, format_val/1, cancel_timer/1]).
%% Deprecated functions %% Deprecated functions
-export([decode_base64/1, encode_base64/1]). -export([decode_base64/1, encode_base64/1]).
@ -308,6 +308,19 @@ format_val(YAML) ->
catch _:_ -> io_lib:format("~p", [YAML]) catch _:_ -> io_lib:format("~p", [YAML])
end. end.
-spec cancel_timer(reference()) -> ok.
cancel_timer(TRef) when is_reference(TRef) ->
case erlang:cancel_timer(TRef) of
false ->
receive {timeout, TRef, _} -> ok
after 0 -> ok
end;
_ ->
ok
end;
cancel_timer(_) ->
ok.
%%%=================================================================== %%%===================================================================
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================

View File

@ -299,7 +299,7 @@ handle_call({use_slot, Slot, Size}, _From,
docroot = DocRoot} = State) -> docroot = DocRoot} = State) ->
case get_slot(Slot, State) of case get_slot(Slot, State) of
{ok, {Size, TRef}} -> {ok, {Size, TRef}} ->
cancel_timer(TRef), misc:cancel_timer(TRef),
NewState = del_slot(Slot, State), NewState = del_slot(Slot, State),
Path = str:join([DocRoot | Slot], <<$/>>), Path = str:join([DocRoot | Slot], <<$/>>),
{reply, {reply,
@ -703,17 +703,6 @@ replace_special_chars(S) ->
yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE; yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE;
yield_content_type(Type) -> Type. yield_content_type(Type) -> Type.
-spec cancel_timer(reference()) -> ok.
cancel_timer(TRef) ->
case erlang:cancel_timer(TRef) of
false ->
receive {timeout, TRef, _} -> ok
after 0 -> ok
end;
_ ->
ok
end.
-spec iq_disco_info(binary(), binary(), binary(), [xdata()]) -> disco_info(). -spec iq_disco_info(binary(), binary(), binary(), [xdata()]) -> disco_info().
iq_disco_info(Host, Lang, Name, AddInfo) -> iq_disco_info(Host, Lang, Name, AddInfo) ->
Form = case gen_mod:get_module_opt(Host, ?MODULE, max_size) of Form = case gen_mod:get_module_opt(Host, ?MODULE, max_size) of

View File

@ -228,7 +228,7 @@ add_timer(JID, Interval, Timers) ->
LJID = jid:tolower(JID), LJID = jid:tolower(JID),
NewTimers = case maps:find(LJID, Timers) of NewTimers = case maps:find(LJID, Timers) of
{ok, OldTRef} -> {ok, OldTRef} ->
cancel_timer(OldTRef), misc:cancel_timer(OldTRef),
maps:remove(LJID, Timers); maps:remove(LJID, Timers);
_ -> Timers _ -> Timers
end, end,
@ -241,19 +241,11 @@ del_timer(JID, Timers) ->
LJID = jid:tolower(JID), LJID = jid:tolower(JID),
case maps:find(LJID, Timers) of case maps:find(LJID, Timers) of
{ok, TRef} -> {ok, TRef} ->
cancel_timer(TRef), misc:cancel_timer(TRef),
maps:remove(LJID, Timers); maps:remove(LJID, Timers);
_ -> Timers _ -> Timers
end. end.
-spec cancel_timer(reference()) -> ok.
cancel_timer(TRef) ->
case erlang:cancel_timer(TRef) of
false ->
receive {timeout, TRef, _} -> ok after 0 -> ok end;
_ -> ok
end.
depends(_Host, _Opts) -> depends(_Host, _Opts) ->
[]. [].

View File

@ -195,7 +195,7 @@ stream_established(_Data, StateData) ->
%% SOCKS5 packets. %% SOCKS5 packets.
handle_info({tcp, _S, Data}, StateName, StateData) handle_info({tcp, _S, Data}, StateName, StateData)
when StateName /= wait_for_activation -> when StateName /= wait_for_activation ->
erlang:cancel_timer(StateData#state.timer), misc:cancel_timer(StateData#state.timer),
TRef = erlang:send_after(?WAIT_TIMEOUT, self(), stop), TRef = erlang:send_after(?WAIT_TIMEOUT, self(), stop),
p1_fsm:send_event(self(), Data), p1_fsm:send_event(self(), Data),
{next_state, StateName, StateData#state{timer = TRef}}; {next_state, StateName, StateData#state{timer = TRef}};
@ -203,7 +203,7 @@ handle_info({tcp, _S, Data}, StateName, StateData)
handle_info({activate, PeerPid, PeerSocket, IJid, TJid}, handle_info({activate, PeerPid, PeerSocket, IJid, TJid},
wait_for_activation, StateData) -> wait_for_activation, StateData) ->
erlang:monitor(process, PeerPid), erlang:monitor(process, PeerPid),
erlang:cancel_timer(StateData#state.timer), misc:cancel_timer(StateData#state.timer),
MySocket = StateData#state.socket, MySocket = StateData#state.socket,
Shaper = StateData#state.shaper, Shaper = StateData#state.shaper,
Host = StateData#state.host, Host = StateData#state.host,

View File

@ -550,8 +550,8 @@ close_socket(#sip_session{socket = SIPSocket}) ->
delete_session(#sip_session{reg_tref = RegTRef, delete_session(#sip_session{reg_tref = RegTRef,
flow_tref = FlowTRef, flow_tref = FlowTRef,
conn_mref = MRef} = Session) -> conn_mref = MRef} = Session) ->
erlang:cancel_timer(RegTRef), misc:cancel_timer(RegTRef),
catch erlang:cancel_timer(FlowTRef), misc:cancel_timer(FlowTRef),
catch erlang:demonitor(MRef, [flush]), catch erlang:demonitor(MRef, [flush]),
mnesia:dirty_delete_object(Session). mnesia:dirty_delete_object(Session).

View File

@ -717,7 +717,7 @@ send(#{mod := Mod} = State, Pkt) ->
-spec restart_pending_timer(state(), non_neg_integer()) -> state(). -spec restart_pending_timer(state(), non_neg_integer()) -> state().
restart_pending_timer(#{mgmt_pending_timer := TRef} = State, NewTimeout) -> restart_pending_timer(#{mgmt_pending_timer := TRef} = State, NewTimeout) ->
cancel_timer(TRef), misc:cancel_timer(TRef),
NewTRef = erlang:start_timer(timer:seconds(NewTimeout), self(), NewTRef = erlang:start_timer(timer:seconds(NewTimeout), self(),
pending_timeout), pending_timeout),
State#{mgmt_pending_timer => NewTRef}; State#{mgmt_pending_timer => NewTRef};
@ -726,22 +726,11 @@ restart_pending_timer(State, _NewTimeout) ->
-spec cancel_ack_timer(state()) -> state(). -spec cancel_ack_timer(state()) -> state().
cancel_ack_timer(#{mgmt_ack_timer := TRef} = State) -> cancel_ack_timer(#{mgmt_ack_timer := TRef} = State) ->
cancel_timer(TRef), misc:cancel_timer(TRef),
maps:remove(mgmt_ack_timer, State); maps:remove(mgmt_ack_timer, State);
cancel_ack_timer(State) -> cancel_ack_timer(State) ->
State. State.
-spec cancel_timer(reference()) -> ok.
cancel_timer(TRef) ->
case erlang:cancel_timer(TRef) of
false ->
receive {timeout, TRef, _} -> ok
after 0 -> ok
end;
_ ->
ok
end.
-spec bounce_message_queue() -> ok. -spec bounce_message_queue() -> ok.
bounce_message_queue() -> bounce_message_queue() ->
receive {route, Pkt} -> receive {route, Pkt} ->