Replies: 1 comment
-
A pgx.Conn is not concurrency safe. You can't use the same connection concurrently from multiple goroutines. What you probably want is a loop on a single goroutine. Something like: for {
func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
notification, err := conn.WaitForNotification(ctx)
if err != nil {
// ...
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = conn.Ping(ctx)
if err != nil {
// ...
}
}()
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We are using pgx lib for LISTEN/NOTIFY mechanism.
We establish a single connection to PG and start the LISTEN and then trigger 2 go routines
GO ROUTINE 1 - responsible for pinging on the same connection to check the health
GO ROUTINE 2 - responsible for waitForNotifications on the same connection to handle notifications
On running, we started seeing waitForNotifications failing with error "conn busy"
When we play around with the order in which we trigger the 2 go routines, we get different results.
Is there something fundamentally wrong that we are doing here?
Beta Was this translation helpful? Give feedback.
All reactions