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

Use econf:timeout() instead of econf:pos_int() wherever appropriate

This commit is contained in:
Evgeny Khramtsov 2019-07-17 22:15:56 +03:00
parent c5305c5f9a
commit d718b35d46
11 changed files with 44 additions and 41 deletions

View File

@ -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},

View File

@ -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) ->

View File

@ -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},

View File

@ -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]),

View File

@ -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)},

View File

@ -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}].

View File

@ -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}].

View File

@ -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)}].

View File

@ -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)

View File

@ -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, []}].

View File

@ -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() ->