rethinkdb / rethinkdb-go

Go language driver for RethinkDB
Apache License 2.0
1.65k stars 182 forks source link

Contexts not working properly in certain scenarios #500

Open Gerrit91 opened 2 years ago

Gerrit91 commented 2 years ago

Describe the bug

To describe the bug, I'd like to look at the following "database outage" scenario:

What can then happen is:

If I conclude correctly from the code, in the connection pool there is a mutex used while distributing queries to a connection (to prevent concurrent creation of a new connection?). I guess in my scenario it takes more time to create a connection (because the connection has gone bad it needs recreation) than requests are coming in. So, Go routines will queue up waiting for the mutex (until the database connection is re-established, which stops this behavior). In the logs of the application I eventually see the connection refused error from this driver.

This shows that using mutexes has some disadvantages for these kind of scenarios because they cannot be left even when a context is provided. From my perspective, instead, the implementation should utilize something like go-lock or a construct using channels from which on the one hand the routines can be informed when the connection is ready and on the other hand a message from a context can be retrieved.

Maybe one or the other will stumble upon the same problem and this helps to better understand the observed behavior.

To Reproduce

Expected behavior The queries to the database are cancelled by the context and do not queue up.

Screenshots

Screenshot from 2022-04-07 15-07-05

--> As soon as the DB server is shutdown, go routines are queueing up (depends on the workload how quickly)

profile004

--> This is a bit complex as it's created with pprof for a real microservice, but the important information is at the bottom: go routines are queuing up in the conn function of the connection pool

System info