From d718b35d462e8096de7cc711591020cea23050d2 Mon Sep 17 00:00:00 2001 From: Evgeny Khramtsov Date: Wed, 17 Jul 2019 22:15:56 +0300 Subject: [PATCH] Use econf:timeout() instead of econf:pos_int() wherever appropriate --- src/ejabberd_bosh.erl | 4 ++-- src/ejabberd_cluster.erl | 2 +- src/ejabberd_options.erl | 12 ++++++------ src/ejabberd_s2s_out.erl | 8 ++++---- src/mod_bosh.erl | 8 ++++---- src/mod_fail2ban.erl | 20 ++++++++++++-------- src/mod_ping.erl | 11 +++++------ src/mod_pres_counter.erl | 6 +++--- src/mod_register.erl | 2 +- src/mod_sip.erl | 8 ++++---- src/mod_sip_registrar.erl | 4 ++-- 11 files changed, 44 insertions(+), 41 deletions(-) diff --git a/src/ejabberd_bosh.erl b/src/ejabberd_bosh.erl index 73a82ab66..d19e7aa3c 100644 --- a/src/ejabberd_bosh.erl +++ b/src/ejabberd_bosh.erl @@ -286,7 +286,7 @@ init([#body{attrs = Attrs}, IP, SID]) -> case ejabberd_c2s:start(?MODULE, Socket, [{receiver, self()}|Opts]) of {ok, C2SPid} -> ejabberd_c2s:accept(C2SPid), - Inactivity = mod_bosh_opt:max_inactivity(XMPPDomain), + Inactivity = mod_bosh_opt:max_inactivity(XMPPDomain) div 1000, MaxConcat = mod_bosh_opt:max_concat(XMPPDomain), ShapedReceivers = buf_new(XMPPDomain, ?MAX_SHAPED_REQUESTS_QUEUE_LEN), State = #state{host = XMPPDomain, sid = SID, ip = IP, @@ -330,7 +330,7 @@ wait_for_session(#body{attrs = Attrs} = Req, From, Wait == 0, Hold == 0 -> erlang:timestamp(); true -> undefined end, - MaxPause = mod_bosh_opt:max_pause(State#state.host), + MaxPause = mod_bosh_opt:max_pause(State#state.host) div 1000, Resp = #body{attrs = [{sid, State#state.sid}, {wait, Wait}, {ver, ?BOSH_VERSION}, {polling, ?DEFAULT_POLLING}, diff --git a/src/ejabberd_cluster.erl b/src/ejabberd_cluster.erl index d91f1fae0..c238efc10 100644 --- a/src/ejabberd_cluster.erl +++ b/src/ejabberd_cluster.erl @@ -160,7 +160,7 @@ subscribe(Proc) -> %%% gen_server API %%%=================================================================== init([]) -> - Ticktime = ejabberd_option:net_ticktime(), + Ticktime = ejabberd_option:net_ticktime() div 1000, Nodes = ejabberd_option:cluster_nodes(), _ = net_kernel:set_net_ticktime(Ticktime), lists:foreach(fun(Node) -> diff --git a/src/ejabberd_options.erl b/src/ejabberd_options.erl index 16d3f7ba5..505c20fd7 100644 --- a/src/ejabberd_options.erl +++ b/src/ejabberd_options.erl @@ -215,7 +215,7 @@ opt_type(modules) -> opt_type(negotiation_timeout) -> econf:timeout(second); opt_type(net_ticktime) -> - econf:pos_int(); + econf:timeout(second); opt_type(new_sql_schema) -> econf:bool(); opt_type(oauth_access) -> @@ -277,7 +277,7 @@ opt_type(redis_queue_type) -> opt_type(redis_server) -> econf:string(); opt_type(registration_timeout) -> - econf:pos_int(infinity); + econf:timeout(second, infinity); opt_type(resource_conflict) -> econf:enum([setresource, closeold, closenew, acceptnew]); opt_type(riak_cacertfile) -> @@ -319,7 +319,7 @@ opt_type(s2s_dns_retries) -> opt_type(s2s_dns_timeout) -> econf:timeout(second, infinity); opt_type(s2s_max_retry_delay) -> - econf:pos_int(); + econf:timeout(second); opt_type(s2s_protocol_options) -> econf:and_then( econf:list(econf:binary(), [unique]), @@ -536,7 +536,7 @@ options() -> {max_fsm_queue, undefined}, {modules, []}, {negotiation_timeout, timer:seconds(30)}, - {net_ticktime, 60}, + {net_ticktime, timer:seconds(60)}, {new_sql_schema, ?USE_NEW_SQL_SCHEMA_DEFAULT}, {oauth_access, none}, {oauth_cache_life_time, @@ -568,7 +568,7 @@ options() -> {redis_queue_type, fun(Host) -> ejabberd_config:get_option({queue_type, Host}) end}, {redis_server, "localhost"}, - {registration_timeout, 600}, + {registration_timeout, timer:seconds(600)}, {resource_conflict, acceptnew}, {riak_cacertfile, nil}, {riak_password, nil}, @@ -594,7 +594,7 @@ options() -> {s2s_dhfile, undefined}, {s2s_dns_retries, 2}, {s2s_dns_timeout, timer:seconds(10)}, - {s2s_max_retry_delay, 300}, + {s2s_max_retry_delay, timer:seconds(300)}, {s2s_protocol_options, undefined}, {s2s_queue_type, fun(Host) -> ejabberd_config:get_option({queue_type, Host}) end}, diff --git a/src/ejabberd_s2s_out.erl b/src/ejabberd_s2s_out.erl index 074952df0..57d8a6267 100644 --- a/src/ejabberd_s2s_out.erl +++ b/src/ejabberd_s2s_out.erl @@ -137,11 +137,11 @@ process_auth_result(#{server := LServer, remote_server := RServer} = State, Delay = get_delay(), ?WARNING_MSG("Failed to establish outbound s2s connection ~s -> ~s: " "authentication failed; bouncing for ~p seconds", - [LServer, RServer, Delay]), + [LServer, RServer, Delay div 1000]), State1 = State#{on_route => bounce, stop_reason => Reason}, State2 = close(State1), State3 = bounce_queue(State2), - xmpp_stream_out:set_timeout(State3, timer:seconds(Delay)); + xmpp_stream_out:set_timeout(State3, Delay); process_auth_result(State, true) -> State. @@ -156,10 +156,10 @@ process_closed(#{server := LServer, remote_server := RServer} = State, Delay = get_delay(), ?WARNING_MSG("Failed to establish outbound s2s connection ~s -> ~s: ~s; " "bouncing for ~p seconds", - [LServer, RServer, format_error(Reason), Delay]), + [LServer, RServer, format_error(Reason), Delay div 1000]), State1 = State#{on_route => bounce}, State2 = bounce_queue(State1), - xmpp_stream_out:set_timeout(State2, timer:seconds(Delay)). + xmpp_stream_out:set_timeout(State2, Delay). handle_unexpected_info(State, Info) -> ?WARNING_MSG("Unexpected info: ~p", [Info]), diff --git a/src/mod_bosh.erl b/src/mod_bosh.erl index 5ed3d5e8d..9803e941c 100644 --- a/src/mod_bosh.erl +++ b/src/mod_bosh.erl @@ -164,9 +164,9 @@ mod_opt_type(json) -> mod_opt_type(max_concat) -> econf:pos_int(unlimited); mod_opt_type(max_inactivity) -> - econf:pos_int(); + econf:timeout(second); mod_opt_type(max_pause) -> - econf:pos_int(); + econf:timeout(second); mod_opt_type(prebind) -> econf:bool(); mod_opt_type(queue_type) -> @@ -187,8 +187,8 @@ mod_opt_type(cache_life_time) -> mod_options(Host) -> [{json, false}, {max_concat, unlimited}, - {max_inactivity, 30}, - {max_pause, 120}, + {max_inactivity, timer:seconds(30)}, + {max_pause, timer:seconds(120)}, {prebind, false}, {ram_db_type, ejabberd_config:default_ram_db(Host, ?MODULE)}, {queue_type, ejabberd_option:queue_type(Host)}, diff --git a/src/mod_fail2ban.erl b/src/mod_fail2ban.erl index 1647f69bb..9bf5ebfeb 100644 --- a/src/mod_fail2ban.erl +++ b/src/mod_fail2ban.erl @@ -64,7 +64,7 @@ c2s_auth_result(#{ip := {Addr, _}, lserver := LServer} = State, {false, _}, _Use false -> BanLifetime = mod_fail2ban_opt:c2s_auth_ban_lifetime(LServer), MaxFailures = mod_fail2ban_opt:c2s_max_auth_failures(LServer), - UnbanTS = erlang:system_time(second) + BanLifetime, + UnbanTS = current_time() + BanLifetime, Attempts = case ets:lookup(failed_auth, Addr) of [{Addr, N, _, _}] -> ets:insert(failed_auth, @@ -90,7 +90,7 @@ c2s_auth_result(#{ip := {Addr, _}} = State, true, _User) -> c2s_stream_started(#{ip := {Addr, _}} = State, _) -> case ets:lookup(failed_auth, Addr) of [{Addr, N, TS, MaxFailures}] when N >= MaxFailures -> - case TS > erlang:system_time(second) of + case TS > current_time() of true -> log_and_disconnect(State, N, TS); false -> @@ -145,7 +145,7 @@ handle_cast(_Msg, State) -> handle_info(clean, State) -> ?DEBUG("Cleaning ~p ETS table", [failed_auth]), - Now = erlang:system_time(second), + Now = current_time(), ets:select_delete( failed_auth, ets:fun2ms(fun({_, _, UnbanTS, _}) -> UnbanTS =< Now end)), @@ -215,7 +215,7 @@ unban(Net, Mask) -> log_and_disconnect(#{ip := {Addr, _}, lang := Lang} = State, Attempts, UnbanTS) -> IP = misc:ip_to_list(Addr), UnbanDate = format_date( - calendar:now_to_universal_time(seconds_to_now(UnbanTS))), + calendar:now_to_universal_time(msec_to_now(UnbanTS))), Format = ?T("Too many (~p) failed authentications " "from this IP address (~s). The address " "will be unblocked at ~s UTC"), @@ -230,8 +230,9 @@ is_whitelisted(Host, Addr) -> Access = mod_fail2ban_opt:access(Host), acl:match_rule(Host, Access, Addr) == allow. --spec seconds_to_now(non_neg_integer()) -> erlang:timestamp(). -seconds_to_now(Secs) -> +-spec msec_to_now(pos_integer()) -> erlang:timestamp(). +msec_to_now(MSecs) -> + Secs = MSecs div 1000, {Secs div 1000000, Secs rem 1000000, 0}. -spec format_date(calendar:datetime()) -> iolist(). @@ -239,14 +240,17 @@ format_date({{Year, Month, Day}, {Hour, Minute, Second}}) -> io_lib:format("~2..0w:~2..0w:~2..0w ~2..0w.~2..0w.~4..0w", [Hour, Minute, Second, Day, Month, Year]). +current_time() -> + erlang:system_time(millisecond). + mod_opt_type(access) -> econf:acl(); mod_opt_type(c2s_auth_ban_lifetime) -> - econf:pos_int(); + econf:timeout(second); mod_opt_type(c2s_max_auth_failures) -> econf:pos_int(). mod_options(_Host) -> [{access, none}, - {c2s_auth_ban_lifetime, 3600}, %% one hour + {c2s_auth_ban_lifetime, timer:hours(1)}, {c2s_max_auth_failures, 20}]. diff --git a/src/mod_ping.erl b/src/mod_ping.erl index 1d926708b..cacab4106 100644 --- a/src/mod_ping.erl +++ b/src/mod_ping.erl @@ -55,7 +55,7 @@ -record(state, {host :: binary(), send_pings :: boolean(), - ping_interval :: non_neg_integer(), + ping_interval :: pos_integer(), ping_ack_timeout :: undefined | non_neg_integer(), timeout_action :: none | kill, timers :: timers()}). @@ -233,7 +233,7 @@ unregister_iq_handlers(Host) -> gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_PING), gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PING). --spec add_timer(jid(), non_neg_integer(), timers()) -> timers(). +-spec add_timer(jid(), pos_integer(), timers()) -> timers(). add_timer(JID, Interval, Timers) -> LJID = jid:tolower(JID), NewTimers = case maps:find(LJID, Timers) of @@ -242,8 +242,7 @@ add_timer(JID, Interval, Timers) -> maps:remove(LJID, Timers); _ -> Timers end, - TRef = erlang:start_timer(Interval * 1000, self(), - {ping, JID}), + TRef = erlang:start_timer(Interval, self(), {ping, JID}), maps:put(LJID, TRef, NewTimers). -spec del_timer(jid(), timers()) -> timers(). @@ -260,7 +259,7 @@ depends(_Host, _Opts) -> []. mod_opt_type(ping_interval) -> - econf:pos_int(); + econf:timeout(second); mod_opt_type(ping_ack_timeout) -> econf:timeout(second); mod_opt_type(send_pings) -> @@ -269,7 +268,7 @@ mod_opt_type(timeout_action) -> econf:enum([none, kill]). mod_options(_Host) -> - [{ping_interval, 60}, + [{ping_interval, timer:minutes(1)}, {ping_ack_timeout, undefined}, {send_pings, false}, {timeout_action, none}]. diff --git a/src/mod_pres_counter.erl b/src/mod_pres_counter.erl index 9c50eb104..8c9f2ae1d 100644 --- a/src/mod_pres_counter.erl +++ b/src/mod_pres_counter.erl @@ -80,7 +80,7 @@ check_packet(Acc, _, _, _) -> update(Server, JID, Dir) -> StormCount = mod_pres_counter_opt:count(Server), TimeInterval = mod_pres_counter_opt:interval(Server), - TimeStamp = erlang:system_time(second), + TimeStamp = erlang:system_time(millisecond), case read(Dir) of undefined -> write(Dir, @@ -125,7 +125,7 @@ write(K, V) -> put({pres_counter, K}, V). mod_opt_type(count) -> econf:pos_int(); mod_opt_type(interval) -> - econf:pos_int(). + econf:timeout(second). mod_options(_) -> - [{count, 5}, {interval, 60}]. + [{count, 5}, {interval, timer:seconds(60)}]. diff --git a/src/mod_register.erl b/src/mod_register.erl index 9ac3f6c06..73fd3ecf6 100644 --- a/src/mod_register.erl +++ b/src/mod_register.erl @@ -429,7 +429,7 @@ check_timeout(undefined) -> true; check_timeout(Source) -> Timeout = ejabberd_option:registration_timeout(), if is_integer(Timeout) -> - Priority = -erlang:system_time(second), + Priority = -erlang:system_time(millisecond), CleanPriority = Priority + Timeout, F = fun () -> Treap = case mnesia:read(mod_register_ip, treap, write) diff --git a/src/mod_sip.erl b/src/mod_sip.erl index a939b17b4..e500d809e 100644 --- a/src/mod_sip.erl +++ b/src/mod_sip.erl @@ -327,9 +327,9 @@ is_my_host(LServer) -> mod_opt_type(always_record_route) -> econf:bool(); mod_opt_type(flow_timeout_tcp) -> - econf:pos_int(); + econf:timeout(second); mod_opt_type(flow_timeout_udp) -> - econf:pos_int(); + econf:timeout(second); mod_opt_type(record_route) -> econf:sip_uri(); mod_opt_type(routes) -> @@ -356,8 +356,8 @@ mod_options(Host) -> host = Host, params = [{<<"lr">>, <<>>}]}, [{always_record_route, true}, - {flow_timeout_tcp, 120}, - {flow_timeout_udp, 29}, + {flow_timeout_tcp, timer:seconds(120)}, + {flow_timeout_udp, timer:seconds(29)}, {record_route, Route}, {routes, [Route]}, {via, []}]. diff --git a/src/mod_sip_registrar.erl b/src/mod_sip_registrar.erl index 0ff2623f0..6b682e030 100644 --- a/src/mod_sip_registrar.erl +++ b/src/mod_sip_registrar.erl @@ -494,9 +494,9 @@ need_ob_hdrs(Contacts, _IsOutboundSupported = true) -> get_flow_timeout(LServer, #sip_socket{type = Type}) -> case Type of udp -> - mod_sip_opt:flow_timeout_udp(LServer); + mod_sip_opt:flow_timeout_udp(LServer) div 1000; _ -> - mod_sip_opt:flow_timeout_tcp(LServer) + mod_sip_opt:flow_timeout_tcp(LServer) div 1000 end. update_table() -> -- 2.40.0