[erlang-questions] Is it safe to unlink from gen_fsm on the fly?
Tue Sep 22 19:52:51 CEST 2009
Thanks for your letter! It was very helpful.
On Mon, Sep 21, 2009 at 11:43 PM, Jayson Vantuyl <kagato@REDACTED> wrote:
> Unlinking is bad mostly because you can leak processes. One of the
> advantages of the supervision tree is that you can guarantee that all
> children get killed when restarts happen.
I think it's possible to pass the connection in an (almost) safe
manner having at any time at least one process from the supervision
tree linked to the driver.
There are three functional process: gen_server which is where
SQL-requests are sent to, connector and epgsql driver. Here is
(working but ugly) algorithm showing how to transfer the connection
from the connector to the gen_server in such a way:
1. spawn_link connector process from gen_server
2. Do epgsql:connect() in connector
3. Send the connection from connector to gen_server
4. Link gen_server to the connection
5. Let connector know that gen_server is succesfully linked to the driver
6. Unlink connector from the driver and stop it
The only thing I don't like about it except for complexity is that
there may be some problems if PostgreSQL will crash between say 4 and
> I would recommend having the reconnection process always running, and have
> the gen_server send it an async message every time the connection is down
> and there is a pending request, maybe on a timeout.
Having the reconnection process always running is a brilliant idea
that I missed while thinking about the problem! Thanks a lot for
pointing it out!
There is no need in sending async messages 'reconnect' from
gen_server, because connector is always able to detect problems by
itself (by catching exit signals coming from the epgsql driver) and
send the connection to the gen_server asynchronously.
More information about the erlang-questions