Use golang's context to cancel query, rather than proactively check a killed/canceled flag
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Enhancement
as title.
pros:
- golang runtime and libraries have adopted the style that `context.Context` is the first parameters in function arguments. We don't need to proactively check the query is killed/canceled frequently. In many functions from official SDK or 3-rd party libraries, invalid context will cause the function return error and break the workflow. In fact, we often forget to proactively check the killed/canceled flag and have caused many problems like https://github.com/pingcap/tidb/issues/58080 https://github.com/pingcap/tidb/issues/56017
- after golang 1.20, we can return specific error to https://pkg.go.dev/context#CancelCauseFunc , so the reasons of https://github.com/pingcap/tidb/blob/0ffac36ec16b0e12a4e3360910bf2da39d1fe523/pkg/util/sqlkiller/sqlkiller.go#L71-L85 can be passed in context way
cons:
- the builtin style of `CancelCauseFunc` requires to use https://pkg.go.dev/context#Cause to find the error, but we are more familiar with `ctx.Err()`. There should be some work to rewrite the usage of `ctx.Err()`
- the builtin https://pkg.go.dev/context#WithCancelCause uses a lock to guard the cancel cause. So if multiple goroutines access it, it's slower than the killed/canceled flag which is an atomic variable. Given these 2 drawbacks, we may want to implement our own `WithCancelCauseFast` function
Contributor guide
Assessment
This issue has not been assessed yet.