http.CloseNotify是否完全被http.Request.Context().Done()替代了?

huangapple go评论114阅读模式
英文:

Is http.CloseNotify entirely replaced by http.Request.Context().Done()?

问题

Go 1.7在http.Request中添加了Context。它是否完全替代了http.CloseNotify?现在我应该优先使用Context,而不用关心CloseNotify吗?

英文:

Go 1.7 added Context to the http.Request. Does it completely replace http.CloseNotify? Should I prefer that now, and not bother with CloseNotify?

答案1

得分: 23

是的,但只适用于Go 1.8及以上版本。在Go 1.7中存在这些API,但直到Go 1.8中,客户端的断开连接才会导致请求的上下文(Context)被标记为完成(Done),因此在Go 1.7中仍然存在有效使用CloseNotifier的情况。

(来源:我向标准库添加了"context"并且是net/http包的作者。)

英文:

Yes, but only in Go 1.8+. The APIs existed in Go 1.7 but it wasn't until Go 1.8 that the client's disconnection caused the Request's Context to be Done, so in Go 1.7 there was still a valid use of CloseNotifier.

(Source: I added "context" to the standard library and am the author of the net/http package.)

huangapple
  • 本文由 发表于 2017年5月22日 16:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/44107868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定