Detect pid reuse
Mon Jul 6 14:09:38 CEST 2020
I have a bunch of connections (processes) to different instances of the same database (instances are physically separated and don't share data). The connect may be unstable, so the connections are handled with a supervisor: if the connect is lost, the process terminates and supervisor starts a new one.
If I want to send a command to the database, I search for the pid of the corresponding connection (in supervisor's children list). And between the search and actual sending command (i.e. via gen_server:call(Pid,...)), the process may terminate and its pid will be reused by another connection (to different database). So I end up with sending a command to the wrong database (as gen_server:call/3 thinks that the pid is the correct one).
Is there a way to detect that such restart has occurred?
I have an idea to keep some kind of token for the connection, so a process sending a command should pass the token and the connection process must check that the token is right. The code for connection process is 3rd party, so I would like to find some other solution.
More information about the erlang-questions