From c7b4c06a909699445534bb8517e998ea9fd1de9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jan=20Niemier?= Date: Wed, 26 Feb 2025 09:21:27 +0100 Subject: [PATCH] fix: do not handle errors during connection This is potential source of a problem, as if there are is error in 2nd or 3rd clause of `with` then we are leaking connections which in the end may result in the connections exhaustion. Instead we should fail loudly that there is something wrong with the connections. It should also help with debugging potential problems. --- lib/cluster/strategy/postgres.ex | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/cluster/strategy/postgres.ex b/lib/cluster/strategy/postgres.ex index 471c7463..41177640 100644 --- a/lib/cluster/strategy/postgres.ex +++ b/lib/cluster/strategy/postgres.ex @@ -52,24 +52,19 @@ defmodule Cluster.Strategy.Postgres do end def handle_continue(:connect, state) do - with {:ok, conn} <- Postgrex.start_link(state.meta.opts.()), - {:ok, conn_notif} <- Postgrex.Notifications.start_link(state.meta.opts.()), - {_, _} <- Postgrex.Notifications.listen(conn_notif, state.config[:channel_name]) do - Logger.info(state.topology, "Connected to Postgres database") - - meta = %{ - state.meta - | conn: conn, - conn_notif: conn_notif, - heartbeat_ref: heartbeat(0) - } - - {:noreply, put_in(state.meta, meta)} - else - reason -> - Logger.error(state.topology, "Failed to connect to Postgres: #{inspect(reason)}") - {:noreply, state} - end + {:ok, conn} = Postgrex.start_link(state.meta.opts.()) + {:ok, conn_notif} = Postgrex.Notifications.start_link(state.meta.opts.()) + {_, _} = Postgrex.Notifications.listen(conn_notif, state.config[:channel_name]) + Logger.info(state.topology, "Connected to Postgres database") + + meta = %{ + state.meta + | conn: conn, + conn_notif: conn_notif, + heartbeat_ref: heartbeat(0) + } + + {:noreply, put_in(state.meta, meta)} end def handle_info(:heartbeat, state) do