Re:Three bugs in the Go MySQL driver
by dullgiulio on Thursday May 21, 2020 @02:14AM
(#23255360)
Of course it is up to the application to decide case-by-case.
Just keep in mind that if your query is started by a user request, but does not need to terminate in the context of the user request that triggered it, you can move the query to a separate goroutine and decouple it completely from the triggering user request.
Also, aborting a query is safe: the transaction gets rolled back, removing unwanted side effects. You cannot do harm by cancelling, bu you can do harm if you don't cancel.
I've been wondering if this is indeed a best practice or something that should be used only when necessary. Should every database query really abort if the connection to the client fails?
In addition, if you follow this advice you now need to check if the returned error is context.Cancelled which warrants an HTTP 400 response.