THRIFT-3820 Erlang: Detect OTP >= 18 to use new time correction
erlang:now/0 is deprecated BIF.
See the "Time and Time Correction in Erlang" chapter of the ERTS User's Guide for more information.
This closes #1000
diff --git a/lib/erl/rebar.config.script b/lib/erl/rebar.config.script
new file mode 100644
index 0000000..c733823
--- /dev/null
+++ b/lib/erl/rebar.config.script
@@ -0,0 +1,7 @@
+Def0 = case not erlang:is_builtin(erlang, monotonic_time, 0) of
+ true -> [];
+ false -> [{d, time_correction}]
+ end,
+Defs = Def0,
+lists:keystore(erl_opts, 1, CONFIG,
+ {erl_opts, proplists:get_value(erl_opts, CONFIG, []) ++ Defs}).
diff --git a/lib/erl/src/thrift_reconnecting_client.erl b/lib/erl/src/thrift_reconnecting_client.erl
index 6731eab..468c38b 100644
--- a/lib/erl/src/thrift_reconnecting_client.erl
+++ b/lib/erl/src/thrift_reconnecting_client.erl
@@ -115,9 +115,9 @@
_From,
State=#state{ client = Client } ) ->
- Start = now(),
+ Timer = timer_fun(),
Result = ( catch thrift_client:call( Client, Op, Args) ),
- Time = timer:now_diff( now(), Start ),
+ Time = Timer(),
case Result of
{ C, { ok, Reply } } ->
@@ -217,6 +217,21 @@
false -> Backoff
end.
+-ifdef(time_correction).
+timer_fun() ->
+ T1 = erlang:monotonic_time(),
+ fun() ->
+ T2 = erlang:monotonic_time(),
+ erlang:convert_time_unit(T2 - T1, native, micro_seconds)
+ end.
+-else.
+timer_fun() ->
+ T1 = erlang:now(),
+ fun() ->
+ T2 = erlang:now(),
+ timer:now_diff(T2, T1)
+ end.
+-endif.
incr_stats( Op, Result, Time,
State = #state{ op_cnt_dict = OpCntDict,