From 87b57112df361588c4fb26bde4dd31a19e6ef30e Mon Sep 17 00:00:00 2001 From: Serge Tupchii Date: Fri, 9 Jun 2023 21:43:50 +0300 Subject: [PATCH] refactor: move tcp keepalive options helper to emqx_utils --- apps/emqx/src/emqx_connection.erl | 33 ++++++++++-------------------- apps/emqx_utils/src/emqx_utils.erl | 23 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/apps/emqx/src/emqx_connection.erl b/apps/emqx/src/emqx_connection.erl index 385c20393..1172460ac 100644 --- a/apps/emqx/src/emqx_connection.erl +++ b/apps/emqx/src/emqx_connection.erl @@ -275,28 +275,17 @@ stats(#state{ async_set_keepalive(Idle, Interval, Probes) -> async_set_keepalive(os:type(), self(), Idle, Interval, Probes). -async_set_keepalive({unix, linux}, Pid, Idle, Interval, Probes) -> - Options = [ - {keepalive, true}, - {raw, 6, 4, <>}, - {raw, 6, 5, <>}, - {raw, 6, 6, <>} - ], - async_set_socket_options(Pid, Options); -async_set_keepalive({unix, darwin}, Pid, Idle, Interval, Probes) -> - Options = [ - {keepalive, true}, - {raw, 6, 16#10, <>}, - {raw, 6, 16#101, <>}, - {raw, 6, 16#102, <>} - ], - 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. +async_set_keepalive(OS, Pid, Idle, Interval, Probes) -> + case emqx_utils:tcp_keepalive_opts(OS, Idle, Interval, Probes) of + {ok, Options} -> + async_set_socket_options(Pid, Options); + {error, {unsupported_os, OS}} -> + ?SLOG(warning, #{ + msg => "Unsupported operation: set TCP keepalive", + os => OS + }), + ok + end. %% @doc Set custom socket options. %% This API is made async because the call might be originated from diff --git a/apps/emqx_utils/src/emqx_utils.erl b/apps/emqx_utils/src/emqx_utils.erl index c7888cd36..53ed37dee 100644 --- a/apps/emqx_utils/src/emqx_utils.erl +++ b/apps/emqx_utils/src/emqx_utils.erl @@ -57,7 +57,8 @@ pub_props_to_packet/1, safe_filename/1, diff_lists/3, - merge_lists/3 + merge_lists/3, + tcp_keepalive_opts/4 ]). -export([ @@ -488,6 +489,26 @@ safe_to_existing_atom(Atom, _Encoding) when is_atom(Atom) -> safe_to_existing_atom(_Any, _Encoding) -> {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, <>}, + {raw, 6, 5, <>}, + {raw, 6, 6, <>} + ]}; +tcp_keepalive_opts({unix, darwin}, Idle, Interval, Probes) -> + {ok, [ + {keepalive, true}, + {raw, 6, 16#10, <>}, + {raw, 6, 16#101, <>}, + {raw, 6, 16#102, <>} + ]}; +tcp_keepalive_opts(OS, _Idle, _Interval, _Probes) -> + {error, {unsupported_os, OS}}. + %%------------------------------------------------------------------------------ %% Internal Functions %%------------------------------------------------------------------------------