From 29db302808f94697689d3010b3b589f9a00333b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chmielowski?= Date: Mon, 7 Dec 2015 16:08:57 +0100 Subject: [PATCH] More now() replacements --- src/adhoc.erl | 2 +- src/ejabberd_auth_anonymous.erl | 2 +- src/ejabberd_auth_external.erl | 3 +-- src/ejabberd_c2s.erl | 16 ++++++---------- src/ejabberd_http_bind.erl | 11 +++-------- src/ejabberd_riak_sup.erl | 2 +- src/ejabberd_router.erl | 4 ++-- src/ejabberd_s2s.erl | 9 +++++---- src/ejabberd_s2s_out.erl | 5 ++--- src/ejabberd_web_admin.erl | 3 +-- src/extauth.erl | 3 +-- src/mod_fail2ban.erl | 11 +++-------- src/mod_last.erl | 6 +++--- src/mod_mam.erl | 6 +++--- src/mod_muc.erl | 2 +- src/mod_muc_admin.erl | 2 +- src/mod_muc_log.erl | 8 ++++---- src/mod_register.erl | 3 +-- src/mod_sip_proxy.erl | 6 ++---- src/mod_sip_registrar.erl | 4 ++-- src/mod_time.erl | 5 ++--- src/randoms.erl | 2 +- src/shaper.erl | 9 +++------ 23 files changed, 50 insertions(+), 74 deletions(-) diff --git a/src/adhoc.erl b/src/adhoc.erl index a68b54d89..f2bd2dbee 100644 --- a/src/adhoc.erl +++ b/src/adhoc.erl @@ -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 [] -> diff --git a/src/ejabberd_auth_anonymous.erl b/src/ejabberd_auth_anonymous.erl index 7a1d8fe84..ab04534f2 100644 --- a/src/ejabberd_auth_anonymous.erl +++ b/src/ejabberd_auth_anonymous.erl @@ -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 diff --git a/src/ejabberd_auth_external.erl b/src/ejabberd_auth_external.erl index cc3a781ce..393bf1e02 100644 --- a/src/ejabberd_auth_external.erl +++ b/src/ejabberd_auth_external.erl @@ -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() diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index b80abfabe..fbea2a8e3 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -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. diff --git a/src/ejabberd_http_bind.erl b/src/ejabberd_http_bind.erl index a3098779f..194b099d7 100644 --- a/src/ejabberd_http_bind.erl +++ b/src/ejabberd_http_bind.erl @@ -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) -> diff --git a/src/ejabberd_riak_sup.erl b/src/ejabberd_riak_sup.erl index a971a4a32..78d6fc6e7 100644 --- a/src/ejabberd_riak_sup.erl +++ b/src/ejabberd_riak_sup.erl @@ -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, diff --git a/src/ejabberd_router.erl b/src/ejabberd_router.erl index 9daa498ee..81fedfe0f 100644 --- a/src/ejabberd_router.erl +++ b/src/ejabberd_router.erl @@ -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 -> diff --git a/src/ejabberd_s2s.erl b/src/ejabberd_s2s.erl index a52b4a9af..0e51ec044 100644 --- a/src/ejabberd_s2s.erl +++ b/src/ejabberd_s2s.erl @@ -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 diff --git a/src/ejabberd_s2s_out.erl b/src/ejabberd_s2s_out.erl index 6d3b3e99a..6bf928282 100644 --- a/src/ejabberd_s2s_out.erl +++ b/src/ejabberd_s2s_out.erl @@ -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(), []), diff --git a/src/ejabberd_web_admin.erl b/src/ejabberd_web_admin.erl index 9a6aea3af..c4a191eb0 100644 --- a/src/ejabberd_web_admin.erl +++ b/src/ejabberd_web_admin.erl @@ -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; diff --git a/src/extauth.erl b/src/extauth.erl index 64220fcbe..9b84741dc 100644 --- a/src/extauth.erl +++ b/src/extauth.erl @@ -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) -> diff --git a/src/mod_fail2ban.erl b/src/mod_fail2ban.erl index abfd1273f..523cbb3f9 100644 --- a/src/mod_fail2ban.erl +++ b/src/mod_fail2ban.erl @@ -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 -> diff --git a/src/mod_last.erl b/src/mod_last.erl index cab29c427..6cd598a18 100644 --- a/src/mod_last.erl +++ b/src/mod_last.erl @@ -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) -> diff --git a/src/mod_mam.erl b/src/mod_mam.erl index 87ff8e5e1..c0756b6eb 100644 --- a/src/mod_mam.erl +++ b/src/mod_mam.erl @@ -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; diff --git a/src/mod_muc.erl b/src/mod_muc.erl index 867f8d387..385bf54b0 100644 --- a/src/mod_muc.erl +++ b/src/mod_muc.erl @@ -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) -> diff --git a/src/mod_muc_admin.erl b/src/mod_muc_admin.erl index 9df4361ca..97ae3c6ec 100644 --- a/src/mod_muc_admin.erl +++ b/src/mod_muc_admin.erl @@ -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 -> diff --git a/src/mod_muc_log.erl b/src/mod_muc_log.erl index 8f940d318..996ac83a5 100644 --- a/src/mod_muc_log.erl +++ b/src/mod_muc_log.erl @@ -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, <<"
">>), fw(F, <<"
- {_, 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, diff --git a/src/mod_register.erl b/src/mod_register.erl index 21c584469..da0b805e7 100644 --- a/src/mod_register.erl +++ b/src/mod_register.erl @@ -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) diff --git a/src/mod_sip_proxy.erl b/src/mod_sip_proxy.erl index 53b16fd7e..bf297bc88 100644 --- a/src/mod_sip_proxy.erl +++ b/src/mod_sip_proxy.erl @@ -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 = <>, 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 _:_ -> diff --git a/src/mod_sip_registrar.erl b/src/mod_sip_registrar.erl index 4bc68f607..44091ed39 100644 --- a/src/mod_sip_registrar.erl +++ b/src/mod_sip_registrar.erl @@ -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), diff --git a/src/mod_time.erl b/src/mod_time.erl index e11468400..4c2e6e567 100644 --- a/src/mod_time.erl +++ b/src/mod_time.erl @@ -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 = diff --git a/src/randoms.erl b/src/randoms.erl index 950f29fc3..b2e95b256 100644 --- a/src/randoms.erl +++ b/src/randoms.erl @@ -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 diff --git a/src/shaper.erl b/src/shaper.erl index c8140703c..a2fd09dfa 100644 --- a/src/shaper.erl +++ b/src/shaper.erl @@ -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].