tortoise icon indicating copy to clipboard operation
tortoise copied to clipboard

Safest way to disconnect redundantly?

Open travisgriggs opened this issue 3 years ago • 1 comments

A second/redundant call to Tortoise.Connection.disconnect/1 creates all kinds of heartache. I tried to inline the two lines of code that make up this API:

  connection_pid = Tortoise.Connection.via_name(connection_id) |> IO.inspect(label: "CONNECTION_PID")
  GenServer.call(connection_pid, :disconnect) |> IO.inspect(label: ":DISCONNECT call")

What I found is that the registry seems to still know about the connection/pid pairing after the disconnect (maybe it eventually goes away?)

But I can't figure how to safely issue a disconnect. The call blows up. And in a way that I can't even seem to get a rescuing hander around it. I can't even figure out how to put together a query that would determine if the client_id is registered or not.

travisgriggs avatar Sep 22 '21 22:09 travisgriggs

I ended up further inlining the via: registry lookup:

  Registry.lookup(Tortoise.Registry, {Tortoise.Connection, connection_id})
    |> case do
      [{pid, nil}] -> GenServer.call(pid, :disconnect)
      _ -> connection_id |> IO.inspect(label: "CONNECTION NOT PRESENT")
    end

This seems kind of roundabout, but it works. I think disconnect() should never fail. So I'll always use this instead of the current disconnect/1 API.

travisgriggs avatar Sep 22 '21 23:09 travisgriggs