refactor: move tcp keepalive options helper to emqx_utils

This commit is contained in:
Serge Tupchii 2023-06-09 21:43:50 +03:00
parent 7c63f20aa8
commit 87b57112df
2 changed files with 33 additions and 23 deletions

View File

@ -275,28 +275,17 @@ stats(#state{
async_set_keepalive(Idle, Interval, Probes) -> async_set_keepalive(Idle, Interval, Probes) ->
async_set_keepalive(os:type(), self(), Idle, Interval, Probes). async_set_keepalive(os:type(), self(), Idle, Interval, Probes).
async_set_keepalive({unix, linux}, Pid, Idle, Interval, Probes) -> async_set_keepalive(OS, Pid, Idle, Interval, Probes) ->
Options = [ case emqx_utils:tcp_keepalive_opts(OS, Idle, Interval, Probes) of
{keepalive, true}, {ok, Options} ->
{raw, 6, 4, <<Idle:32/native>>}, async_set_socket_options(Pid, Options);
{raw, 6, 5, <<Interval:32/native>>}, {error, {unsupported_os, OS}} ->
{raw, 6, 6, <<Probes:32/native>>} ?SLOG(warning, #{
], msg => "Unsupported operation: set TCP keepalive",
async_set_socket_options(Pid, Options); os => OS
async_set_keepalive({unix, darwin}, Pid, Idle, Interval, Probes) -> }),
Options = [ ok
{keepalive, true}, end.
{raw, 6, 16#10, <<Idle:32/native>>},
{raw, 6, 16#101, <<Interval:32/native>>},
{raw, 6, 16#102, <<Probes:32/native>>}
],
async_set_socket_options(Pid, Options);
async_set_keepalive(OS, _Pid, _Idle, _Interval, _Probes) ->
?SLOG(warning, #{
msg => "Unsupported operation: set TCP keepalive",
os => OS
}),
ok.
%% @doc Set custom socket options. %% @doc Set custom socket options.
%% This API is made async because the call might be originated from %% This API is made async because the call might be originated from

View File

@ -57,7 +57,8 @@
pub_props_to_packet/1, pub_props_to_packet/1,
safe_filename/1, safe_filename/1,
diff_lists/3, diff_lists/3,
merge_lists/3 merge_lists/3,
tcp_keepalive_opts/4
]). ]).
-export([ -export([
@ -488,6 +489,26 @@ safe_to_existing_atom(Atom, _Encoding) when is_atom(Atom) ->
safe_to_existing_atom(_Any, _Encoding) -> safe_to_existing_atom(_Any, _Encoding) ->
{error, invalid_type}. {error, invalid_type}.
-spec tcp_keepalive_opts(term(), non_neg_integer(), non_neg_integer(), non_neg_integer()) ->
{ok, [{keepalive, true} | {raw, non_neg_integer(), non_neg_integer(), binary()}]}
| {error, {unsupported_os, term()}}.
tcp_keepalive_opts({unix, linux}, Idle, Interval, Probes) ->
{ok, [
{keepalive, true},
{raw, 6, 4, <<Idle:32/native>>},
{raw, 6, 5, <<Interval:32/native>>},
{raw, 6, 6, <<Probes:32/native>>}
]};
tcp_keepalive_opts({unix, darwin}, Idle, Interval, Probes) ->
{ok, [
{keepalive, true},
{raw, 6, 16#10, <<Idle:32/native>>},
{raw, 6, 16#101, <<Interval:32/native>>},
{raw, 6, 16#102, <<Probes:32/native>>}
]};
tcp_keepalive_opts(OS, _Idle, _Interval, _Probes) ->
{error, {unsupported_os, OS}}.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Internal Functions %% Internal Functions
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------