背景:
我正在利用自定义 LDAP 服务器 package .它使用 Done
请求中的 channel 让处理程序知道 - 比如说客户端断开连接 - 因此处理程序也应该中止。 For example .
由于 Done
channel 是处理取消的旧方式 - 我希望支持取消链接 - 我创建了一个 context.Context
从这个 channel 像这样:
func doneContext(p context.Context, done <-chan bool) (
ctx context.Context, cancel context.CancelFunc,
) {
ctx, cancel = context.WithCancel(p)
go func() {
<-done
cancel() // done closed, so cancel our context
}()
return
}
这假设
done
channel 将被关闭:第一个证明是正确的 - 第二个没有。成功的处理程序调用将 不是 触发
done
channel 要关闭 - 因此我正在泄漏 go-routines。为了解决这个问题 - 因为我取消了自己的
context.Context
在处理程序完成时 - 成功与否,例如// convert the client request's Done channel into a context.Context
ctx, cancel := doneContext(context.Background(), m.Done)
defer cancel() // clean-up context: rain or shine
我更新了
doneContext
像这样去例行公事:go func() {
select {
case <-done:
cancel() // done closed, so cancel our context (like before)
case <-ctx.Done():
// when our context is canceled, recover the go-routine (even if done never closes)
}
}()
问题:
done
进入更现代的context.Context
done
channel ?GC
收集这个 channel ,即使它永远不会关闭? 最佳答案
您无需关闭 channel 。如果没有任何东西引用 channel ,它将被垃圾收集。
您的 channel 逻辑显示正确。
关于go - 从完成 channel 和未关闭 channel 的 GC 生成 context.Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58223267/