]> granicus.if.org Git - ejabberd/commitdiff
Move cancel_timer/1 function into 'misc' module
authorHolger Weiss <holger@zedat.fu-berlin.de>
Tue, 17 Jul 2018 18:50:58 +0000 (20:50 +0200)
committerHolger Weiss <holger@zedat.fu-berlin.de>
Tue, 17 Jul 2018 18:50:58 +0000 (20:50 +0200)
src/ejabberd_bosh.erl
src/ejabberd_captcha.erl
src/ejabberd_http_ws.erl
src/ejabberd_system_monitor.erl
src/eldap.erl
src/misc.erl
src/mod_http_upload.erl
src/mod_ping.erl
src/mod_proxy65_stream.erl
src/mod_sip_registrar.erl
src/mod_stream_mgmt.erl

index cf83fbe5c45bbcf9875b023df223fd8eb58605fc..dcb1943e3cc8724d6e9c0d733acbe3586865c6c3 100644 (file)
@@ -404,7 +404,7 @@ active(#body{attrs = Attrs, size = Size} = Req, From,
                    {next_state, active,
                     State2#state{shaped_receivers = Q}}
            catch error:full ->
-                 cancel_timer(TRef),
+                 misc:cancel_timer(TRef),
                  RID = get_attr(rid, Attrs),
                  reply_stop(State1,
                             #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)
                       of
                     {{value, {TRef, From, Body}}, Q} ->
-                        cancel_timer(TRef),
+                        misc:cancel_timer(TRef),
                         p1_fsm:send_event(self(), {Body, From}),
                         State1#state{shaped_receivers = Q};
                     _ -> State1
@@ -1035,12 +1035,8 @@ buf_out(Buf, I, Els) ->
       {empty, _} -> buf_out(Buf, 0, Els)
     end.
 
-cancel_timer(TRef) when is_reference(TRef) ->
-    p1_fsm:cancel_timer(TRef);
-cancel_timer(_) -> false.
-
 restart_timer(TRef, Timeout, Msg) ->
-    cancel_timer(TRef),
+    misc:cancel_timer(TRef),
     erlang:start_timer(timer:seconds(Timeout), self(), Msg).
 
 restart_inactivity_timer(#state{inactivity_timeout =
@@ -1057,7 +1053,7 @@ restart_inactivity_timer(#state{inactivity_timer =
 
 stop_inactivity_timer(#state{inactivity_timer = TRef} =
                          State) ->
-    cancel_timer(TRef),
+    misc:cancel_timer(TRef),
     State#state{inactivity_timer = undefined}.
 
 restart_wait_timer(#state{wait_timer = TRef,
@@ -1067,7 +1063,7 @@ restart_wait_timer(#state{wait_timer = TRef,
     State#state{wait_timer = NewTRef}.
 
 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) ->
     erlang:start_timer(Timeout, self(), shaper_timeout).
index a0c731c9a34fa88bc731ffb00b2f6504f7aea1d3..f366b41753fecfbcabec2ec4b5ab5834b1136a54 100644 (file)
@@ -517,11 +517,7 @@ recv_data(Port, TRef, Buf) ->
     end.
 
 return(Port, TRef, Result) ->
-    case erlang:cancel_timer(TRef) of
-      false ->
-         receive {timeout, TRef, _} -> ok after 0 -> ok end;
-      _ -> ok
-    end,
+    misc:cancel_timer(TRef),
     catch port_close(Port),
     Result.
 
@@ -561,7 +557,7 @@ check_captcha(Id, ProvidedKey) ->
     case ets:lookup(captcha, Id) of
        [#captcha{pid = Pid, args = Args, key = ValidKey, tref = Tref}] ->
            ets:delete(captcha, Id),
-           erlang:cancel_timer(Tref),
+           misc:cancel_timer(Tref),
            if ValidKey == ProvidedKey ->
                    callback(captcha_succeed, Pid, Args),
                    captcha_valid;
index 669041c6625cb3d2143131ffcc84c29379a99b6f..a9d98b882fd7a69e6eacbf7fbf36242ccc2f4a92 100644 (file)
@@ -256,7 +256,7 @@ handle_info({timeout, Timer, _}, StateName,
            #state{ping_timer = Timer, ws = {_, WsPid}} = StateData) ->
     case StateData#state.pong_expected of
         false ->
-            cancel_timer(StateData#state.ping_timer),
+            misc:cancel_timer(StateData#state.ping_timer),
             PingTimer = erlang:start_timer(StateData#state.ping_interval,
                                            self(), []),
             WsPid ! {ping, <<>>},
@@ -282,10 +282,10 @@ terminate(_Reason, _StateName, StateData) ->
     ok.
 
 setup_timers(StateData) ->
-    cancel_timer(StateData#state.timer),
+    misc:cancel_timer(StateData#state.timer),
     Timer = erlang:start_timer(StateData#state.timeout,
                                self(), []),
-    cancel_timer(StateData#state.ping_timer),
+    misc:cancel_timer(StateData#state.ping_timer),
     PingTimer = case StateData#state.ping_interval of
                     0 -> StateData#state.ping_timer;
                     V -> erlang:start_timer(V, self(), [])
@@ -293,10 +293,6 @@ setup_timers(StateData) ->
      StateData#state{timer = Timer, ping_timer = PingTimer,
                      pong_expected = false}.
 
-cancel_timer(Timer) ->
-    erlang:cancel_timer(Timer),
-    receive {timeout, Timer, _} -> ok after 0 -> ok end.
-
 get_human_html_xmlel() ->
     Heading = <<"ejabberd ", (misc:atom_to_binary(?MODULE))/binary>>,
     #xmlel{name = <<"html">>,
index 65aad8d7feb3cafd835c7521276e8ba646bb34b5..cf50209fb1df4009edef0035e16a231d5dd96ec2 100644 (file)
@@ -88,7 +88,7 @@ handle_event({set_alarm, {system_memory_high_watermark, _}}, State) ->
     handle_overload(State),
     {ok, restart_timer(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}};
 handle_event({set_alarm, {process_memory_high_watermark, Pid}}, State) ->
     case proc_stat(Pid, get_app_pids()) of
@@ -220,23 +220,10 @@ proc_stat(Pid, AppPids) ->
 
 -spec restart_timer(#state{}) -> #state{}.
 restart_timer(State) ->
-    cancel_timer(State#state.tref),
+    misc:cancel_timer(State#state.tref),
     TRef = erlang:start_timer(?CHECK_INTERVAL, self(), handle_overload),
     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().
 format_apps(Apps) ->
     AppList = lists:reverse(lists:keysort(2, dict:to_list(Apps))),
index fc3b8a29d1a73f507a19c6b1b63e279f0f7d0469..760f2557bd9a70e6b6a93dc2fc7b189975b6c054 100644 (file)
@@ -659,7 +659,7 @@ handle_info({Tag, _Socket, Data}, connecting, S)
     {next_state, connecting, S};
 handle_info({Tag, _Socket, Data}, wait_bind_response, S)
     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
       bound -> dequeue_commands(S);
       {fail_bind, Reason} ->
@@ -847,14 +847,14 @@ recvd_packet(Pkt, S) ->
                         if Reason == success; Reason == sizeLimitExceeded ->
                                {Res, Ref} = polish(Result_so_far),
                                New_dict = dict:erase(Id, Dict),
-                               cancel_timer(Timer),
+                               misc:cancel_timer(Timer),
                                {reply,
                                 #eldap_search_result{entries = Res,
                                                      referrals = Ref},
                                 From, S#eldap{dict = New_dict}};
                            true ->
                                New_dict = dict:erase(Id, Dict),
-                               cancel_timer(Timer),
+                               misc:cancel_timer(Timer),
                                {reply, {error, Reason}, From,
                                 S#eldap{dict = New_dict}}
                         end;
@@ -863,37 +863,37 @@ recvd_packet(Pkt, S) ->
                         {ok, S#eldap{dict = New_dict}};
                     {addRequest, {addResponse, Result}} ->
                         New_dict = dict:erase(Id, Dict),
-                        cancel_timer(Timer),
+                        misc:cancel_timer(Timer),
                         Reply = check_reply(Result, From),
                         {reply, Reply, From, S#eldap{dict = New_dict}};
                     {delRequest, {delResponse, Result}} ->
                         New_dict = dict:erase(Id, Dict),
-                        cancel_timer(Timer),
+                        misc:cancel_timer(Timer),
                         Reply = check_reply(Result, From),
                         {reply, Reply, From, S#eldap{dict = New_dict}};
                     {modifyRequest, {modifyResponse, Result}} ->
                         New_dict = dict:erase(Id, Dict),
-                        cancel_timer(Timer),
+                        misc:cancel_timer(Timer),
                         Reply = check_reply(Result, From),
                         {reply, Reply, From, S#eldap{dict = New_dict}};
                     {modDNRequest, {modDNResponse, Result}} ->
                         New_dict = dict:erase(Id, Dict),
-                        cancel_timer(Timer),
+                        misc:cancel_timer(Timer),
                         Reply = check_reply(Result, From),
                         {reply, Reply, From, S#eldap{dict = New_dict}};
                     {bindRequest, {bindResponse, Result}} ->
                         New_dict = dict:erase(Id, Dict),
-                        cancel_timer(Timer),
+                        misc:cancel_timer(Timer),
                         Reply = check_bind_reply(Result, From),
                         {reply, Reply, From, S#eldap{dict = New_dict}};
                     {extendedReq, {extendedResp, Result}} ->
                         New_dict = dict:erase(Id, Dict),
-                        cancel_timer(Timer),
+                        misc:cancel_timer(Timer),
                         Reply = check_extended_reply(Result, From),
                         {reply, Reply, From, S#eldap{dict = New_dict}};
                     {OtherName, OtherResult} ->
                         New_dict = dict:erase(Id, Dict),
-                        cancel_timer(Timer),
+                        misc:cancel_timer(Timer),
                         {reply,
                          {error, {invalid_result, OtherName, OtherResult}},
                          From, S#eldap{dict = New_dict}}
@@ -968,16 +968,11 @@ check_id(_, _) -> throw({error, wrong_bind_id}).
 %% General Helpers
 %%-----------------------------------------------------------------------
 
-cancel_timer(Timer) ->
-    erlang:cancel_timer(Timer),
-    receive {timeout, Timer, _} -> ok after 0 -> ok end.
-
-
 close_and_retry(S, Timeout) ->
     catch (S#eldap.sockmod):close(S#eldap.fd),
     Queue = dict:fold(fun (_Id,
                           [{Timer, Command, From, _Name} | _], Q) ->
-                             cancel_timer(Timer),
+                             misc:cancel_timer(Timer),
                              queue:in_r({Command, From}, Q);
                          (_, _, Q) -> Q
                      end,
index abd0691eeb7120ce744c88f9dfa15ce712eb1b98..5ef71c41b95b8879e521dc9347e90c24ca23f3e0 100644 (file)
@@ -36,7 +36,7 @@
         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,
         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
 -export([decode_base64/1, encode_base64/1]).
@@ -308,6 +308,19 @@ format_val(YAML) ->
     catch _:_ -> io_lib:format("~p", [YAML])
     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
 %%%===================================================================
index 6db8bd256348c673757a5211bb7c2c19364c899f..09fd66d3e1b23914d980a3928f2a52eb96ece15b 100644 (file)
@@ -299,7 +299,7 @@ handle_call({use_slot, Slot, Size}, _From,
                   docroot = DocRoot} = State) ->
     case get_slot(Slot, State) of
        {ok, {Size, TRef}} ->
-           cancel_timer(TRef),
+           misc:cancel_timer(TRef),
            NewState = del_slot(Slot, State),
            Path = str:join([DocRoot | Slot], <<$/>>),
            {reply,
@@ -703,17 +703,6 @@ replace_special_chars(S) ->
 yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_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().
 iq_disco_info(Host, Lang, Name, AddInfo) ->
     Form = case gen_mod:get_module_opt(Host, ?MODULE, max_size) of
index 6df9aa65a26ea802b3bfb2091b8f4773ad8f673a..88146b14b3b457cdda1e5e3db5eafa6f22a05c3f 100644 (file)
@@ -228,7 +228,7 @@ add_timer(JID, Interval, Timers) ->
     LJID = jid:tolower(JID),
     NewTimers = case maps:find(LJID, Timers) of
       {ok, OldTRef} ->
-                     cancel_timer(OldTRef),
+                     misc:cancel_timer(OldTRef),
           maps:remove(LJID, Timers);
       _ -> Timers
                end,
@@ -241,19 +241,11 @@ del_timer(JID, Timers) ->
     LJID = jid:tolower(JID),
     case maps:find(LJID, Timers) of
       {ok, TRef} ->
-         cancel_timer(TRef),
+         misc:cancel_timer(TRef),
     maps:remove(LJID, Timers);
       _ -> Timers
     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) ->
     [].
 
index 0646e28c86b5513a3b841bf0bdf3f0c76b061cd5..4e7aa0334c9fc99b84e7e7ccd4b3b6c28e8d75f6 100644 (file)
@@ -195,7 +195,7 @@ stream_established(_Data, StateData) ->
 %% SOCKS5 packets.
 handle_info({tcp, _S, Data}, StateName, StateData)
     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),
     p1_fsm:send_event(self(), Data),
     {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},
            wait_for_activation, StateData) ->
     erlang:monitor(process, PeerPid),
-    erlang:cancel_timer(StateData#state.timer),
+    misc:cancel_timer(StateData#state.timer),
     MySocket = StateData#state.socket,
     Shaper = StateData#state.shaper,
     Host = StateData#state.host,
index d7b959435393964ede74605dd07404725691d42e..dfb9a50f0e804e35ae0e502203e8491cc0a9e427 100644 (file)
@@ -550,8 +550,8 @@ close_socket(#sip_session{socket = SIPSocket}) ->
 delete_session(#sip_session{reg_tref = RegTRef,
                            flow_tref = FlowTRef,
                            conn_mref = MRef} = Session) ->
-    erlang:cancel_timer(RegTRef),
-    catch erlang:cancel_timer(FlowTRef),
+    misc:cancel_timer(RegTRef),
+    misc:cancel_timer(FlowTRef),
     catch erlang:demonitor(MRef, [flush]),
     mnesia:dirty_delete_object(Session).
 
index a37b6538c140490e8678412c16b4d805d9e6a770..f897a2794f85e5bd86696a3b3ac3b5309011038b 100644 (file)
@@ -717,7 +717,7 @@ send(#{mod := Mod} = State, Pkt) ->
 
 -spec restart_pending_timer(state(), non_neg_integer()) -> state().
 restart_pending_timer(#{mgmt_pending_timer := TRef} = State, NewTimeout) ->
-    cancel_timer(TRef),
+    misc:cancel_timer(TRef),
     NewTRef = erlang:start_timer(timer:seconds(NewTimeout), self(),
                                 pending_timeout),
     State#{mgmt_pending_timer => NewTRef};
@@ -726,22 +726,11 @@ restart_pending_timer(State, _NewTimeout) ->
 
 -spec cancel_ack_timer(state()) -> state().
 cancel_ack_timer(#{mgmt_ack_timer := TRef} = State) ->
-    cancel_timer(TRef),
+    misc:cancel_timer(TRef),
     maps:remove(mgmt_ack_timer, State);
 cancel_ack_timer(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.
 bounce_message_queue() ->
     receive {route, Pkt} ->