您创建了一个
contex.Context可以取消的,您可以在客户端关闭连接时取消它,但是您不会检查上下文,并且如果取消它,处理程序也不会做任何不同的事情。上下文仅
携带 超时和取消信号,它不具有杀死/终止goroutines的能力或意图。goroutine本身必须监视此类取消信号并对其采取行动。
因此,您看到的是代码的预期输出。
您想要监视的是上下文,如果上下文被取消,则从处理程序中“立即”返回。
当然,如果您正在“睡觉”,则无法同时监视上下文。因此,请改用
time.After(),如本例所示:
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() if cn, ok := w.(http.CloseNotifier); ok { go func(done <-chan struct{}, closed <-chan bool) { select { case <-done: case <-closed: fmt.Println("client cancelled....................!!!!!!!!!") cancel() } }(ctx.Done(), cn.CloseNotify()) } select { case <-time.After(5 * time.Second): fmt.Println("5 seconds elapsed, client didn't close") case <-ctx.Done(): fmt.Println("Context closed, client closed connection?") return } fmt.Fprint(w, "cancellation testing......")})


