elixir-ecto / db_connection

Database connection behaviour
http://hexdocs.pm/db_connection/DBConnection.html
306 stars 113 forks source link

Add `:connection_listeners` option to `DBConnection.start_link/2` #228

Closed slashdotdash closed 3 years ago

slashdotdash commented 3 years ago

The :connection_listeners option allows one or more processes to be notified whenever a connection is connected or disconnected. A listener may be a remote or local PID, a local port, a locally registered name, or a tuple in the form of {registered_name, node} for a registered name at another node.

Each listener process may receive the following messages where pid identifies the connection process:

Related to #227.

@josevalim Does this follow what you were initally suggesting?

josevalim commented 3 years ago

@slashdotdash just one minor comment, everything else looks excellent. The biggest question now is if this is enough to solve your original problems. :)

slashdotdash commented 3 years ago

The biggest question now is if this is enough to solve your original problems. :)

Yes, I think so. My goal was to allow a connection to be monitored so when it is connected or disconnected I can handle these state changes elsewhere. It was possible to use the after_connect callback to support the connections, but not disconnects. The connection listeners allows both.

I will look at fixing the failing CI tests on Erlang 19.3 and 20.3 versions.

josevalim commented 3 years ago

:green_heart: :blue_heart: :purple_heart: :yellow_heart: :heart:

slashdotdash commented 3 years ago

@josevalim I did some testing of the new :connection_listeners option and it worked as I had hoped for when simulating a flaky database connection with Postgrex by starting/stopping the Postgres server.

{:connected, #PID<0.297.0>} {:disconnected, #PID<0.297.0>} {:connected, #PID<0.298.0>}