diff --git a/README.md b/README.md index 696e10e4..13d2efa1 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Improvements and changes compared to `wooga/eredis`: * Support of TLS introduced in Redis 6 * Changed API: `start_link` takes a proplist with options * Correction regarding chunked error responses +* Correction regarding termination of reconnect loop process * Dialyzer corrections * Elvis code formatting * Improved test coverage diff --git a/src/eredis_client.erl b/src/eredis_client.erl index 7545af73..56caef63 100644 --- a/src/eredis_client.erl +++ b/src/eredis_client.erl @@ -460,7 +460,9 @@ maybe_reconnect(Reason, #state{queue = Queue} = State) -> error_logger:error_msg("eredis: Re-establishing connection to ~p:~p due to ~p", [State#state.host, State#state.port, Reason]), Self = self(), - spawn_link(fun() -> reconnect_loop(Self, State) end), + spawn_link(fun() -> process_flag(trap_exit, true), + reconnect_loop(Self, State) + end), %% tell all of our clients what has happened. reply_all({error, Reason}, Queue), @@ -474,20 +476,24 @@ maybe_reconnect(Reason, #state{queue = Queue} = State) -> %% successfully issuing the auth and select calls. When we have a %% connection, give the socket to the redis client. reconnect_loop(Client, #state{reconnect_sleep=ReconnectSleep, transport=Transport}=State) -> - timer:sleep(ReconnectSleep), - case catch(connect(State)) of - {ok, #state{socket = Socket}} -> - Client ! {connection_ready, Socket}, - Transport:controlling_process(Socket, Client), - Msgs = get_all_messages([]), - [Client ! M || M <- Msgs]; - {error, _Reason} -> - reconnect_loop(Client, State); - %% Something bad happened when connecting, like Redis might be - %% loading the dataset and we got something other than 'OK' in - %% auth or select - _ -> - reconnect_loop(Client, State) + receive + {'EXIT', _, Reason} -> exit(Reason) + after + ReconnectSleep -> + case catch(connect(State)) of + {ok, #state{socket = Socket}} -> + Client ! {connection_ready, Socket}, + Transport:controlling_process(Socket, Client), + Msgs = get_all_messages([]), + [Client ! M || M <- Msgs]; + {error, _Reason} -> + reconnect_loop(Client, State); + %% Something bad happened when connecting, like Redis might be + %% loading the dataset and we got something other than 'OK' in + %% auth or select + _ -> + reconnect_loop(Client, State) + end end. read_database(undefined) ->