25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

More now() replacements

This commit is contained in:
Paweł Chmielowski 2015-12-07 16:08:57 +01:00
parent b9f751e099
commit 29db302808
23 changed files with 50 additions and 74 deletions

View File

@ -121,7 +121,7 @@ produce_response(
}) ->
SessionID = if is_binary(ProvidedSessionID),
ProvidedSessionID /= <<"">> -> ProvidedSessionID;
true -> jlib:now_to_utc_string(now())
true -> jlib:now_to_utc_string(p1_time_compat:timestamp())
end,
case Actions of
[] ->

View File

@ -56,7 +56,7 @@
%% Create the anonymous table if at least one virtual host has anonymous features enabled
%% Register to login / logout events
-record(anonymous, {us = {<<"">>, <<"">>} :: {binary(), binary()},
sid = {now(), self()} :: ejabberd_sm:sid()}).
sid = {p1_time_compat:timestamp(), self()} :: ejabberd_sm:sid()}).
start(Host) ->
%% TODO: Check cluster mode

View File

@ -270,8 +270,7 @@ set_password_internal(User, Server, Password) ->
Password).
is_fresh_enough(TimeStampLast, CacheTime) ->
{MegaSecs, Secs, _MicroSecs} = now(),
Now = MegaSecs * 1000000 + Secs,
Now = p1_time_compat:system_time(seconds),
TimeStampLast + CacheTime > Now.
%% @spec (User, Server) -> online | never | mod_last_required | TimeStamp::integer()

View File

@ -326,7 +326,7 @@ init([{SockMod, Socket}, Opts]) ->
xml_socket = XMLSocket, zlib = Zlib, tls = TLS,
tls_required = StartTLSRequired,
tls_enabled = TLSEnabled, tls_options = TLSOpts,
sid = {now(), self()}, streamid = new_id(),
sid = {p1_time_compat:timestamp(), self()}, streamid = new_id(),
access = Access, shaper = Shaper, ip = IP,
mgmt_state = StreamMgmtState,
mgmt_max_queue = MaxAckQueue,
@ -1011,9 +1011,7 @@ resource_conflict_action(U, S, R) ->
acceptnew -> {accept_resource, R};
closenew -> closenew;
setresource ->
Rnew = iolist_to_binary([randoms:get_string()
| [jlib:integer_to_binary(X)
|| X <- tuple_to_list(now())]]),
Rnew = iolist_to_binary([randoms:get_string(),randoms:get_string(),randoms:get_string()]),
{accept_resource, Rnew}
end.
@ -1041,9 +1039,7 @@ wait_for_bind({xmlstreamelement, El}, StateData) ->
R = case jid:resourceprep(R1) of
error -> error;
<<"">> ->
iolist_to_binary([randoms:get_string()
| [jlib:integer_to_binary(X)
|| X <- tuple_to_list(now())]]);
iolist_to_binary([randoms:get_string(),randoms:get_string(),randoms:get_string()]);
Resource -> Resource
end,
case R of
@ -2047,7 +2043,7 @@ presence_update(From, Packet, StateData) ->
FromUnavail = (StateData#state.pres_last == undefined),
?DEBUG("from unavail = ~p~n", [FromUnavail]),
NewStateData = StateData#state{pres_last = Packet,
pres_timestamp = now()},
pres_timestamp = p1_time_compat:timestamp()},
NewState = if FromUnavail ->
ejabberd_hooks:run(user_available_hook,
NewStateData#state.server,
@ -2791,7 +2787,7 @@ mgmt_queue_add(StateData, El) ->
Num ->
Num + 1
end,
NewQueue = queue:in({NewNum, now(), El}, StateData#state.mgmt_queue),
NewQueue = queue:in({NewNum, p1_time_compat:timestamp(), El}, StateData#state.mgmt_queue),
NewState = StateData#state{mgmt_queue = NewQueue,
mgmt_stanzas_out = NewNum},
check_queue_length(NewState).
@ -3003,7 +2999,7 @@ csi_queue_add(#state{csi_queue = Queue} = StateData, Stanza) ->
true -> csi_queue_add(csi_queue_flush(StateData), Stanza);
false ->
From = xml:get_tag_attr_s(<<"from">>, Stanza),
NewQueue = lists:keystore(From, 1, Queue, {From, now(), Stanza}),
NewQueue = lists:keystore(From, 1, Queue, {From, p1_time_compat:timestamp(), Stanza}),
StateData#state{csi_queue = NewQueue}
end.

View File

@ -233,7 +233,7 @@ process_request(Data, IP, HOpts) ->
(?NS_HTTP_BIND)/binary, "'/>">>};
XmppDomain ->
NXmppDomain = jid:nameprep(XmppDomain),
Sid = p1_sha:sha(term_to_binary({now(), make_ref()})),
Sid = p1_sha:sha(term_to_binary({p1_time_compat:monotonic_time(), make_ref()})),
case start(NXmppDomain, Sid, <<"">>, IP, HOpts) of
{error, _} ->
{500, ?HEADER,
@ -448,7 +448,7 @@ handle_sync_event(#http_put{payload_size =
shaper_timer = NewShaperTimer});
%% HTTP GET: send packets to the client
handle_sync_event({http_get, Rid, Wait, Hold}, From, StateName, StateData) ->
TNow = tnow(),
TNow = p1_time_compat:system_time(micro_seconds),
if (Hold > 0) and
((StateData#state.output == []) or (StateData#state.rid < Rid)) and
((TNow - StateData#state.ctime) < (Wait*1000*1000)) and
@ -606,7 +606,7 @@ process_http_put(#http_put{rid = Rid, attrs = Attrs,
end
end
end,
TNow = tnow(),
TNow = p1_time_compat:system_time(micro_seconds),
LastPoll = if Payload == [] -> TNow;
true -> 0
end,
@ -1185,11 +1185,6 @@ get_max_pause({Host, _}) ->
?MAX_PAUSE);
get_max_pause(_) -> ?MAX_PAUSE.
%% Current time as integer
tnow() ->
{TMegSec, TSec, TMSec} = now(),
(TMegSec * 1000000 + TSec) * 1000000 + TMSec.
check_default_xmlns(#xmlel{name = Name, attrs = Attrs,
children = Els} =
El) ->

View File

@ -141,7 +141,7 @@ get_pids() ->
[ejabberd_riak:get_proc(I) || I <- lists:seq(1, get_pool_size())].
get_random_pid() ->
get_random_pid(now()).
get_random_pid(p1_time_compat:monotonic_time()).
get_random_pid(Term) ->
I = erlang:phash2(Term, get_pool_size()) + 1,

View File

@ -347,8 +347,8 @@ do_route(OrigFrom, OrigTo, OrigPacket) ->
ejabberd_config:get_local_option({domain_balancing,
LDstDomain}, fun(D) when is_atom(D) -> D end)
of
undefined -> now();
random -> now();
undefined -> p1_time_compat:monotonic_time();
random -> p1_time_compat:monotonic_time();
source -> jid:tolower(From);
destination -> jid:tolower(To);
bare_source ->

View File

@ -81,7 +81,7 @@
-record(state, {}).
-record(temporarily_blocked, {host = <<"">> :: binary(),
timestamp = now() :: erlang:timestamp()}).
timestamp :: integer()}).
-type temporarily_blocked() :: #temporarily_blocked{}.
@ -114,9 +114,9 @@ external_host_overloaded(Host) ->
"seconds",
[Host, ?S2S_OVERLOAD_BLOCK_PERIOD]),
mnesia:transaction(fun () ->
Time = p1_time_compat:monotonic_time(),
mnesia:write(#temporarily_blocked{host = Host,
timestamp =
now()})
timestamp = Time})
end).
-spec is_temporarly_blocked(binary()) -> boolean().
@ -125,7 +125,8 @@ is_temporarly_blocked(Host) ->
case mnesia:dirty_read(temporarily_blocked, Host) of
[] -> false;
[#temporarily_blocked{timestamp = T} = Entry] ->
case timer:now_diff(now(), T) of
Diff = p1_time_compat:monotonic_time() - T,
case p1_time_compat:convert_time_unit(Diff, native, micro_seconds) of
N when N > (?S2S_OVERLOAD_BLOCK_PERIOD) * 1000 * 1000 ->
mnesia:dirty_delete_object(Entry), false;
_ -> true

View File

@ -1097,8 +1097,7 @@ get_addr_port(Server) ->
?DEBUG("srv lookup of '~s': ~p~n",
[Server, HEnt#hostent.h_addr_list]),
AddrList = HEnt#hostent.h_addr_list,
{A1, A2, A3} = now(),
random:seed(A1, A2, A3),
random:seed(p1_time_compat:monotonic_time()),
case catch lists:map(fun ({Priority, Weight, Port,
Host}) ->
N = case Weight of
@ -1273,7 +1272,7 @@ wait_before_reconnect(StateData) ->
cancel_timer(StateData#state.timer),
Delay = case StateData#state.delay_to_retry of
undefined_delay ->
{_, _, MicroSecs} = now(), MicroSecs rem 14000 + 1000;
{_, _, MicroSecs} = p1_time_compat:timestamp(), MicroSecs rem 14000 + 1000;
D1 -> lists:min([D1 * 2, get_max_retry_delay()])
end,
Timer = erlang:start_timer(Delay, self(), []),

View File

@ -1722,8 +1722,7 @@ user_parse_query1(Action, User, Server, Query) ->
end.
list_last_activity(Host, Lang, Integral, Period) ->
{MegaSecs, Secs, _MicroSecs} = now(),
TimeStamp = MegaSecs * 1000000 + Secs,
TimeStamp = p1_time_compat:system_time(seconds),
case Period of
<<"all">> -> TS = 0, Days = infinity;
<<"year">> -> TS = TimeStamp - 366 * 86400, Days = 366;

View File

@ -102,8 +102,7 @@ call_port(Server, Msg) ->
receive {eauth, Result} -> Result end.
random_instance(MaxNum) ->
{A1, A2, A3} = now(),
random:seed(A1, A2, A3),
random:seed(p1_time_compat:monotonic_time()),
random:uniform(MaxNum) - 1.
get_instances(Server) ->

View File

@ -65,7 +65,7 @@ c2s_auth_result(false, _User, LServer, {Addr, _Port}) ->
LServer, ?MODULE, c2s_max_auth_failures,
fun(I) when is_integer(I), I > 0 -> I end,
?C2S_MAX_AUTH_FAILURES),
UnbanTS = unban_timestamp(BanLifetime),
UnbanTS = p1_time_compat:system_time(seconds) + BanLifetime,
case ets:lookup(failed_auth, Addr) of
[{Addr, N, _, _}] ->
ets:insert(failed_auth, {Addr, N+1, UnbanTS, MaxFailures});
@ -79,7 +79,7 @@ c2s_auth_result(true, _User, _Server, _AddrPort) ->
check_bl_c2s(_Acc, Addr, Lang) ->
case ets:lookup(failed_auth, Addr) of
[{Addr, N, TS, MaxFailures}] when N >= MaxFailures ->
case TS > now() of
case TS > p1_time_compat:system_time(seconds) of
true ->
IP = jlib:ip_to_list(Addr),
UnbanDate = format_date(
@ -139,7 +139,7 @@ handle_cast(_Msg, State) ->
handle_info(clean, State) ->
?DEBUG("cleaning ~p ETS table", [failed_auth]),
Now = now(),
Now = p1_time_compat:system_time(seconds),
ets:select_delete(
failed_auth,
ets:fun2ms(fun({_, _, UnbanTS, _}) -> UnbanTS =< Now end)),
@ -171,11 +171,6 @@ is_whitelisted(Host, Addr) ->
none),
acl:match_rule(Host, Access, Addr) == allow.
unban_timestamp(BanLifetime) ->
{MegaSecs, MSecs, USecs} = now(),
UnbanSecs = MegaSecs * 1000000 + MSecs + BanLifetime,
{UnbanSecs div 1000000, UnbanSecs rem 1000000, USecs}.
is_loaded_at_other_hosts(Host) ->
lists:any(
fun(VHost) when VHost == Host ->

View File

@ -112,7 +112,7 @@ get_node_uptime() ->
undefined ->
trunc(element(1, erlang:statistics(wall_clock)) / 1000);
Now ->
now_to_seconds(now()) - Now
p1_time_compat:system_time(seconds) - Now
end.
now_to_seconds({MegaSecs, Secs, _MicroSecs}) ->
@ -210,7 +210,7 @@ get_last_iq(IQ, SubEl, LUser, LServer) ->
IQ#iq{type = error,
sub_el = [SubEl, ?ERR_SERVICE_UNAVAILABLE]};
{ok, TimeStamp, Status} ->
TimeStamp2 = now_to_seconds(now()),
TimeStamp2 = p1_time_compat:system_time(seconds),
Sec = TimeStamp2 - TimeStamp,
IQ#iq{type = result,
sub_el =
@ -232,7 +232,7 @@ get_last_iq(IQ, SubEl, LUser, LServer) ->
end.
on_presence_update(User, Server, _Resource, Status) ->
TimeStamp = now_to_seconds(now()),
TimeStamp = p1_time_compat:system_time(seconds),
store_last_info(User, Server, TimeStamp, Status).
store_last_info(User, Server, TimeStamp, Status) ->

View File

@ -45,7 +45,7 @@
-record(archive_msg,
{us = {<<"">>, <<"">>} :: {binary(), binary()} | '$2',
id = <<>> :: binary() | '_',
timestamp = now() :: erlang:timestamp() | '_' | '$1',
timestamp = p1_time_compat:timestamp() :: erlang:timestamp() | '_' | '$1',
peer = {<<"">>, <<"">>, <<"">>} :: ljid() | '_' | '$3',
bare_peer = {<<"">>, <<"">>, <<"">>} :: ljid() | '_' | '$3',
packet = #xmlel{} :: xmlel() | '_',
@ -450,7 +450,7 @@ store_muc(MUCState, Pkt, RoomJID, Peer, Nick) ->
store(Pkt, _, {LUser, LServer}, Type, Peer, Nick, _Dir, mnesia) ->
LPeer = {PUser, PServer, _} = jid:tolower(Peer),
TS = now(),
TS = p1_time_compat:timestamp(),
ID = jlib:integer_to_binary(now_to_usec(TS)),
case mnesia:dirty_write(
#archive_msg{us = {LUser, LServer},
@ -467,7 +467,7 @@ store(Pkt, _, {LUser, LServer}, Type, Peer, Nick, _Dir, mnesia) ->
Err
end;
store(Pkt, LServer, {LUser, LHost}, Type, Peer, Nick, _Dir, odbc) ->
TSinteger = now_to_usec(now()),
TSinteger = p1_time_compat:system_time(micro_seconds),
ID = TS = jlib:integer_to_binary(TSinteger),
SUser = case Type of
chat -> LUser;

View File

@ -864,7 +864,7 @@ flush() -> receive _ -> flush() after 0 -> ok end.
iq_get_unique(From) ->
{xmlcdata,
p1_sha:sha(term_to_binary([From, now(),
p1_sha:sha(term_to_binary([From, p1_time_compat:timestamp(),
randoms:get_string()]))}.
get_nick(ServerHost, Host, From) ->

View File

@ -608,7 +608,7 @@ decide_room({_Room_name, _Host, Room_pid}, Last_allowed) ->
Num_users = length(?DICT:to_list(Room_users)),
History = (S#state.history)#lqueue.queue,
Ts_now = calendar:now_to_universal_time(now()),
Ts_now = calendar:universal_time(),
Ts_uptime = uptime_seconds(),
{Has_hist, Last} = case queue:is_empty(History) of
true ->

View File

@ -348,7 +348,7 @@ add_message_to_log(Nick1, Message, RoomJID, Opts,
Room = get_room_info(RoomJID, Opts),
Nick = htmlize(Nick1, FileFormat),
Nick2 = htmlize_nick(Nick1, FileFormat),
Now = now(),
Now = p1_time_compat:timestamp(),
TimeStamp = case Timezone of
local -> calendar:now_to_local_time(Now);
universal -> calendar:now_to_universal_time(Now)
@ -902,7 +902,7 @@ put_header_script(F) ->
put_room_config(_F, _RoomConfig, _Lang, plaintext) ->
ok;
put_room_config(F, RoomConfig, Lang, _FileFormat) ->
{_, Now2, _} = now(),
{_, Now2, _} = p1_time_compat:timestamp(),
fw(F, <<"<div class=\"rc\">">>),
fw(F,
<<"<div class=\"rct\" onclick=\"sh('a~p');return "
@ -919,7 +919,7 @@ put_room_occupants(_F, _RoomOccupants, _Lang,
ok;
put_room_occupants(F, RoomOccupants, Lang,
_FileFormat) ->
{_, Now2, _} = now(),
{_, Now2, _} = p1_time_compat:timestamp(),
%% htmlize
%% The default behaviour is to ignore the nofollow spam prevention on links
%% (NoFollow=false)
@ -1190,7 +1190,7 @@ get_proc_name(Host) ->
gen_mod:get_module_proc(Host, ?PROCNAME).
calc_hour_offset(TimeHere) ->
TimeZero = calendar:now_to_universal_time(now()),
TimeZero = calendar:universal_time(),
TimeHereHour =
calendar:datetime_to_gregorian_seconds(TimeHere) div
3600,

View File

@ -509,8 +509,7 @@ check_timeout(Source) ->
infinity
end, 600),
if is_integer(Timeout) ->
{MSec, Sec, _USec} = now(),
Priority = -(MSec * 1000000 + Sec),
Priority = -p1_time_compat:system_time(seconds),
CleanPriority = Priority + Timeout,
F = fun () ->
Treap = case mnesia:read(mod_register_ip, treap, write)

View File

@ -299,8 +299,7 @@ add_record_route_and_set_uri(URI, LServer, #sip{hdrs = Hdrs} = Req) ->
case need_record_route(LServer) of
true ->
RR_URI = get_configured_record_route(LServer),
{MSecs, Secs, _} = now(),
TS = list_to_binary(integer_to_list(MSecs*1000000 + Secs)),
TS = list_to_binary(integer_to_list(p1_time_compat:system_time(seconds))),
Sign = make_sign(TS, Hdrs),
User = <<TS/binary, $-, Sign/binary>>,
NewRR_URI = RR_URI#uri{user = User},
@ -341,8 +340,7 @@ is_signed_by_me(TS_Sign, Hdrs) ->
try
[TSBin, Sign] = str:tokens(TS_Sign, <<"-">>),
TS = list_to_integer(binary_to_list(TSBin)),
{MSecs, Secs, _} = now(),
NowTS = MSecs*1000000 + Secs,
NowTS = p1_time_compat:system_time(seconds),
true = (NowTS - TS) =< ?SIGN_LIFETIME,
Sign == make_sign(TSBin, Hdrs)
catch _:_ ->

View File

@ -48,7 +48,7 @@
socket = #sip_socket{} :: #sip_socket{},
call_id = <<"">> :: binary(),
cseq = 0 :: non_neg_integer(),
timestamp = now() :: erlang:timestamp(),
timestamp = p1_time_compat:timestamp() :: erlang:timestamp(),
contact :: {binary(), #uri{}, [{binary(), binary()}]},
flow_tref :: reference(),
reg_tref = make_ref() :: reference(),
@ -242,7 +242,7 @@ register_session(US, SIPSocket, CallID, CSeq, IsOutboundSupported,
socket = SIPSocket,
call_id = CallID,
cseq = CSeq,
timestamp = now(),
timestamp = p1_time_compat:timestamp(),
contact = Contact,
expires = Expires}
end, ContactsWithExpires),

View File

@ -56,9 +56,8 @@ process_local_iq(_From, _To,
set ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
get ->
Now = now(),
Now_universal = calendar:now_to_universal_time(Now),
Now_local = calendar:now_to_local_time(Now),
Now_universal = calendar:universal_time(),
Now_local = calendar:universal_time_to_local_time(Now_universal),
{UTC, UTC_diff} = jlib:timestamp_to_iso(Now_universal,
utc),
Seconds_diff =

View File

@ -35,7 +35,7 @@ start() ->
register(random_generator, spawn(randoms, init, [])).
init() ->
{A1, A2, A3} = now(), random:seed(A1, A2, A3), loop().
random:seed(p1_time_compat:monotonic_time()), loop().
loop() ->
receive

View File

@ -107,7 +107,7 @@ new(Name) ->
new1(none) -> none;
new1(MaxRate) ->
#maxrate{maxrate = MaxRate, lastrate = 0.0,
lasttime = now_to_usec(now())}.
lasttime = p1_time_compat:system_time(micro_seconds)}.
-spec update(shaper(), integer()) -> {shaper(), integer()}.
@ -115,7 +115,7 @@ update(none, _Size) -> {none, 0};
update(#maxrate{} = State, Size) ->
MinInterv = 1000 * Size /
(2 * State#maxrate.maxrate - State#maxrate.lastrate),
Interv = (now_to_usec(now()) - State#maxrate.lasttime) /
Interv = (p1_time_compat:system_time(micro_seconds) - State#maxrate.lasttime) /
1000,
?DEBUG("State: ~p, Size=~p~nM=~p, I=~p~n",
[State, Size, MinInterv, Interv]),
@ -123,7 +123,7 @@ update(#maxrate{} = State, Size) ->
1 + trunc(MinInterv - Interv);
true -> 0
end,
NextNow = now_to_usec(now()) + Pause * 1000,
NextNow = p1_time_compat:system_time(micro_seconds) + Pause * 1000,
{State#maxrate{lastrate =
(State#maxrate.lastrate +
1000000 * Size / (NextNow - State#maxrate.lasttime))
@ -141,8 +141,5 @@ transform_options({OptName, Name, none}, Opts) when OptName == shaper ->
transform_options(Opt, Opts) ->
[Opt|Opts].
now_to_usec({MSec, Sec, USec}) ->
(MSec * 1000000 + Sec) * 1000000 + USec.
opt_type(shaper) -> fun (V) -> V end;
opt_type(_) -> [shaper].