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

fix(sysk): fix probe testing bugs for syskeeper

firest 2 лет назад
Родитель
Сommit
cd90b93550

+ 13 - 2
apps/emqx_bridge_syskeeper/src/emqx_bridge_syskeeper_proxy_server.erl

@@ -67,11 +67,17 @@ on_start(
         {tcp_options, [{mode, binary}, {reuseaddr, true}, {nodelay, true}]}
     ],
     MFArgs = {?MODULE, start_link, [maps:with([handshake_timeout], Config)]},
-    ok = emqx_resource:allocate_resource(InstanceId, listen_on, ListenOn),
 
+    %% Since the esockd only supports atomic name and we don't want to introduce a new atom per each instance
+    %% when the port is same for two instance/connector, them will reference to a same esockd listener
+    %% to prevent the failed one dealloctes the listener which created by a earlier instance
+    %% we need record only when the listen is successed
     case esockd:open(?MODULE, ListenOn, Options, MFArgs) of
         {ok, _} ->
+            ok = emqx_resource:allocate_resource(InstanceId, listen_on, ListenOn),
             {ok, #{listen_on => ListenOn}};
+        {error, {already_started, _}} ->
+            {error, eaddrinuse};
         Error ->
             Error
     end.
@@ -83,7 +89,12 @@ on_stop(InstanceId, _State) ->
     }),
     case emqx_resource:get_allocated_resources(InstanceId) of
         #{listen_on := ListenOn} ->
-            esockd:close(?MODULE, ListenOn);
+            case esockd:close(?MODULE, ListenOn) of
+                {error, not_found} ->
+                    ok;
+                Result ->
+                    Result
+            end;
         _ ->
             ok
     end.

+ 4 - 1
apps/emqx_connector/src/emqx_connector_resource.erl

@@ -229,7 +229,10 @@ create_dry_run(Type, Conf0, Callback) ->
     TypeBin = bin(Type),
     TypeAtom = safe_atom(Type),
     %% We use a fixed name here to avoid creating an atom
-    TmpName = iolist_to_binary([?TEST_ID_PREFIX, TypeBin, ":", <<"probedryrun">>]),
+    %% to avoid potential race condition, the resource id should be unique
+    UID = integer_to_binary(erlang:unique_integer([monotonic, positive])),
+    TmpName =
+        iolist_to_binary([?TEST_ID_PREFIX, TypeBin, ":", <<"probedryrun">>, UID]),
     TmpPath = emqx_utils:safe_filename(TmpName),
     Conf1 = maps:without([<<"name">>], Conf0),
     RawConf = #{<<"connectors">> => #{TypeBin => #{<<"temp_name">> => Conf1}}},