Parcourir la source

fix: ensure cluster link app can recover from init failure

Fixes EMQX-12868

If initial configuration for cluster link is incorrect, and local
cluster fails to establish a connection, an actor would crash on
startup which will lead to app supervisor crash, and removing of
cluster link config handler. This will make it impossible to remove or
update erroneous cluster link configuration.
Ivan Dyachkov il y a 1 an
Parent
commit
13ac854f45
1 fichiers modifiés avec 11 ajouts et 1 suppressions
  1. 11 1
      apps/emqx_cluster_link/src/emqx_cluster_link_router_syncer.erl

+ 11 - 1
apps/emqx_cluster_link/src/emqx_cluster_link_router_syncer.erl

@@ -170,12 +170,22 @@ start_link_client(Actor, LinkConf) ->
     Options = emqx_cluster_link_config:mk_emqtt_options(LinkConf),
     Options = emqx_cluster_link_config:mk_emqtt_options(LinkConf),
     case emqtt:start_link(refine_client_options(Options, Actor)) of
     case emqtt:start_link(refine_client_options(Options, Actor)) of
         {ok, Pid} ->
         {ok, Pid} ->
-            case emqtt:connect(Pid) of
+            try emqtt:connect(Pid) of
                 {ok, _Props} ->
                 {ok, _Props} ->
                     {ok, Pid};
                     {ok, Pid};
                 Error ->
                 Error ->
                     _ = flush_link_signal(Pid),
                     _ = flush_link_signal(Pid),
                     Error
                     Error
+            catch
+                exit:Reason ->
+                    ?SLOG(error, #{
+                        msg => "failed_to_connect_to_cluster",
+                        reason => Reason,
+                        options => Options,
+                        actor => Actor
+                    }),
+                    _ = flush_link_signal(Pid),
+                    {error, failed_to_connect}
             end;
             end;
         Error ->
         Error ->
             Error
             Error