Skip to content

Commit 2539be1

Browse files
committed
mod_http_upload: Avoid timers from timer module
Use erlang:start_timer/3 instead of timer:send_after/2, as the former is more efficient.
1 parent 4e99305 commit 2539be1

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/mod_http_upload.erl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
-define(SERVICE_REQUEST_TIMEOUT, 5000). % 5 seconds.
3232
-define(CALL_TIMEOUT, 60000). % 1 minute.
33-
-define(SLOT_TIMEOUT, 18000000). % 5 hours.
33+
-define(SLOT_TIMEOUT, timer:hours(5)).
3434
-define(URL_ENC(URL), binary_to_list(misc:url_encode(URL))).
3535
-define(ADDR_TO_STR(IP), ejabberd_config:may_hide_data(misc:ip_to_list(IP))).
3636
-define(STR_TO_INT(Str, B), binary_to_integer(iolist_to_binary(Str), B)).
@@ -298,14 +298,14 @@ handle_call({use_slot, Slot, Size}, _From,
298298
custom_headers = CustomHeaders,
299299
docroot = DocRoot} = State) ->
300300
case get_slot(Slot, State) of
301-
{ok, {Size, Timer}} ->
302-
timer:cancel(Timer),
301+
{ok, {Size, TRef}} ->
302+
cancel_timer(TRef),
303303
NewState = del_slot(Slot, State),
304304
Path = str:join([DocRoot | Slot], <<$/>>),
305305
{reply,
306306
{ok, Path, FileMode, DirMode, GetPrefix, Thumbnail, CustomHeaders},
307307
NewState};
308-
{ok, {_WrongSize, _Timer}} ->
308+
{ok, {_WrongSize, _TRef}} ->
309309
{reply, {error, size_mismatch}, State};
310310
error ->
311311
{reply, {error, invalid_slot}, State}
@@ -347,7 +347,7 @@ handle_info({route, #iq{lang = Lang} = Packet}, State) ->
347347
ejabberd_router:route_error(Packet, Err),
348348
{noreply, State}
349349
end;
350-
handle_info({slot_timed_out, Slot}, State) ->
350+
handle_info({timeout, _TRef, Slot}, State) ->
351351
NewState = del_slot(Slot, State),
352352
{noreply, NewState};
353353
handle_info(Info, State) ->
@@ -642,13 +642,13 @@ create_slot(#state{service_url = ServiceURL},
642642

643643
-spec add_slot(slot(), pos_integer(), state()) -> state().
644644
add_slot(Slot, Size, #state{external_secret = <<>>, slots = Slots} = State) ->
645-
{ok, Timer} = timer:send_after(?SLOT_TIMEOUT, {slot_timed_out, Slot}),
646-
NewSlots = maps:put(Slot, {Size, Timer}, Slots),
645+
TRef = erlang:start_timer(?SLOT_TIMEOUT, self(), Slot),
646+
NewSlots = maps:put(Slot, {Size, TRef}, Slots),
647647
State#state{slots = NewSlots};
648648
add_slot(_Slot, _Size, State) ->
649649
State.
650650

651-
-spec get_slot(slot(), state()) -> {ok, {pos_integer(), timer:tref()}} | error.
651+
-spec get_slot(slot(), state()) -> {ok, {pos_integer(), reference()}} | error.
652652
get_slot(Slot, #state{slots = Slots}) ->
653653
maps:find(Slot, Slots).
654654

@@ -702,6 +702,17 @@ replace_special_chars(S) ->
702702
yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE;
703703
yield_content_type(Type) -> Type.
704704

705+
-spec cancel_timer(reference()) -> ok.
706+
cancel_timer(TRef) ->
707+
case erlang:cancel_timer(TRef) of
708+
false ->
709+
receive {timeout, TRef, _} -> ok
710+
after 0 -> ok
711+
end;
712+
_ ->
713+
ok
714+
end.
715+
705716
-spec iq_disco_info(binary(), binary(), binary(), [xdata()]) -> disco_info().
706717
iq_disco_info(Host, Lang, Name, AddInfo) ->
707718
Form = case gen_mod:get_module_opt(Host, ?MODULE, max_size) of

0 commit comments

Comments
 (0)