Просмотр исходного кода

refactor: move tcp keepalive options helper to emqx_utils

Serge Tupchii 2 лет назад
Родитель
Сommit
87b57112df
2 измененных файлов с 33 добавлено и 23 удалено
  1. 11 22
      apps/emqx/src/emqx_connection.erl
  2. 22 1
      apps/emqx_utils/src/emqx_utils.erl

+ 11 - 22
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, <<Idle:32/native>>},
-        {raw, 6, 5, <<Interval:32/native>>},
-        {raw, 6, 6, <<Probes:32/native>>}
-    ],
-    async_set_socket_options(Pid, Options);
-async_set_keepalive({unix, darwin}, Pid, Idle, Interval, Probes) ->
-    Options = [
-        {keepalive, true},
-        {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.
+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

+ 22 - 1
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, <<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
 %%------------------------------------------------------------------------------