jackc / pgx

PostgreSQL driver and toolkit for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Best practice to release acquired connections used with WaitForNotification

aeon3k opened this issue · comments

Hi,
and thanks for this library @jackc that I've been using for a while.

For the first time I'm using WaitForNotification in a for loop inside a go routine that acquire a connection from the pgxpool. I listen to a few channels and it works great.

I manage the interrupt signal to gracefully shutdown the server and close the pool before ending the progam. It used to work fine.

As you mention, *pgxpool.Pool.Close "Blocks until all connections are returned to pool and closed."
Logically, the termination process stales on this very line because of the WaitForNotification loops...

Is there a way to force terminating these remaining connections ? Or is there another way ?

Thanks in advance.

What I do in similar situations is have a top level context that all long running cancellable operations inherit from. The WaitForNotification loop should use a context derived from the top level context. The graceful shutdown should cancel that top level context, and that should trigger shutdowns on all operations.