feat: cache OTP_VERSION in persistent_term

This commit is contained in:
Zhongwen Deng 2023-01-28 12:07:50 +08:00
parent 6162f90610
commit 0b19be074c
1 changed files with 12 additions and 2 deletions

View File

@ -401,9 +401,19 @@ compat_windows(Fun) ->
end.
%% @doc Return on which Erlang/OTP the current vm is running.
%% NOTE: This API reads a file, do not use it in critical code paths.
%% The dashboard's /api/nodes endpoint will call this function frequently.
%% we should avoid reading file every time.
%% The OTP version never changes at runtime expect upgrade erts,
%% so we cache it in a persistent term for performance.
get_otp_version() ->
read_otp_version().
case persistent_term:get(emqx_otp_version, undefined) of
undefined ->
OtpVsn = read_otp_version(),
persistent_term:put(emqx_otp_version, OtpVsn),
OtpVsn;
OtpVsn when is_binary(OtpVsn) ->
OtpVsn
end.
read_otp_version() ->
ReleasesDir = filename:join([code:root_dir(), "releases"]),