nickva commented on code in PR #5601:
URL: https://github.com/apache/couchdb/pull/5601#discussion_r2216377711


##########
src/nouveau/src/nouveau_gun.erl:
##########
@@ -36,20 +36,32 @@
 
 -define(NOUVEAU_HOST_HEADER, nouveau_host_header).
 
+-record(state, {
+    enabled,
+    url
+}).
+
 start_link() ->
     gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
 
 host_header() ->
     persistent_term:get(?NOUVEAU_HOST_HEADER).
 
 init(_) ->
+    Enabled = nouveau:enabled(),
     URL = nouveau_util:nouveau_url(),
-    case start_gun(URL) of
-        {ok, _PoolPid} ->
-            ok = config:listen_for_changes(?MODULE, URL),
-            {ok, nil};
-        {error, Reason} ->
-            {error, Reason}
+    State = #state{enabled = Enabled, url = URL},
+    ok = config:listen_for_changes(?MODULE, State),
+    if
+        Enabled ->
+            case start_gun(URL) of
+                {ok, _PoolPid} ->
+                    {ok, nil};
+                {error, Reason} ->
+                    {error, Reason}
+            end;
+        true ->
+            {ok, nil}

Review Comment:
   Since we have to run basically the same logic in init and on reconfigure 
maybe just have a single `maybe_start()` or `reconfigure()` function that keeps 
track of the `StartedUrl` and on reconfigure we check:
   ```erlang
   case {Enabled, StartedUrl} of
      {true, undefined} -> start(Url), StartedUrl = Url;
      {true, Url} -> ok;
      {false, undefined} -> ok
      {true,  _} -> stop(), start(Url), StartedUrl = Url;
      {false, _} -> stop(), StartedUrl = undefined;
      
      {false, _} ok
   }
    ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to