Reputation: 2176
I have a a GenServer which takes care of my Rabbitmq connection.
The following callback tries to reconnect to the server if connection fails :
def handle_info({:DOWN, _, :process, pid, reason},state) do
Logger.info "Rabbit connection lost"
{:ok, conn} = open_connection()
{:noreply, conn}
end
The GenServer also has a function and callback which returns the current connection pid:
def get_current_connection({:ok, app}) do
{_conn_string , conn, _app} = GenServer.call(app, :get_cuurent_connection})
{:ok,conn}
end
def handle_call({:get_cuurent_connection}, _pid, state) do
{:reply, state , state}
end
What i am tring to do is that once a info of DOWN was accepted (i.e. once the server is in the handle info above) -
The server should be blocked and any call to other functions (for example to the get current connection connection) will wait until the callback is done.
Is there a simple way to do that??
Upvotes: 1
Views: 650
Reputation: 23701
GenServers
ONLY handle one message at a time. While your GenServer is handling the {:DOWN...
message it cannot handle any other messages in its queue. Essentially everything for that server will be "blocked" until you finish handling the message.
If someone were to call get_current_connection
while you were processing the DOWN, control would pass to GenServer.call
and add the request to the message queue and wait for that message to work through the queue. That message CANNOT move until you finish handling the {:DOWN...
message so the caller will essentially be blocked waiting for you to finish re-establishing the connection.
Upvotes: 3