我有以下代码:
func Call(ctx context.Context, payload Payload) (Response, error) {
req, err := http.NewRequest(...) // Some code that creates request from payload
ctx, cancel = context.withTimeout(ctx, time.Duration(3) * time.Second)
defer cancel()
return http.DefaultClient.Do(req)
}
Run Code Online (Sandbox Code Playgroud)
如果我没有放入defer cancel()
那里会怎么样? go vet
警告过这个
context.WithTimeout返回的cancel函数应该被调用,而不是被丢弃,以避免上下文泄漏
上下文将如何泄露以及这将产生什么影响?谢谢
Kae*_*dys 29
如果您无法取消上下文,则创建WithCancel或WithTimeout的goroutine将无限期地保留在内存中(直到程序关闭),从而导致内存泄漏.如果你经常这么做,你的记忆就会大大增加.最好的做法是defer cancel()
在通话后立即使用WithCancel()
或WithTimeout()
use*_*000 20
如果使用WithCancel
,goroutine将无限期地保留在内存中。但是,如果您使用WithDeadline
或WithTimeout
不调用取消,goroutine只会被保留到计时器到期。
但这仍然不是最佳实践,最好在完成资源后立即调用取消。