1 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊
讀取請(qǐng)求正文以檢測(cè)客戶端何時(shí)關(guān)閉連接:
func handler(w http.ResponseWriter, r *http.Request) {
go func(done <-chan struct{}) {
<-done
fmt.Println("message", "client connection has gone away, request got cancelled")
}(r.Context().Done())
io.Copy(ioutil.Discard, r.Body) // <-- read the body
time.Sleep(30 * time.Second)
fmt.Fprintf(w, "Hi there, I love %s!\n", r.URL.Path[1:])
}
net/http 服務(wù)器通過(guò)讀取連接來(lái)檢查關(guān)閉的連接。在應(yīng)用程序開(kāi)始讀取請(qǐng)求正文(如果有)之前,不會(huì)開(kāi)始讀取。
- 1 回答
- 0 關(guān)注
- 118 瀏覽
添加回答
舉報(bào)