0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2025-01-20 22:52:58 -05:00

reverseproxy: Close websocket conn if req context cancels

This is a recent patch in the Go standard library
This commit is contained in:
Matthew Holt 2020-06-11 15:25:26 -06:00
parent 7211101c52
commit b3bff13f7d
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -49,7 +49,20 @@ func (h Handler) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request
// p.getErrorHandler()(rw, req, fmt.Errorf("internal error: 101 switching protocols response with non-writable body")) // p.getErrorHandler()(rw, req, fmt.Errorf("internal error: 101 switching protocols response with non-writable body"))
return return
} }
defer backConn.Close()
// adopted from https://github.com/golang/go/commit/8bcf2834afdf6a1f7937390903a41518715ef6f5
backConnCloseCh := make(chan struct{})
go func() {
// Ensure that the cancelation of a request closes the backend.
// See issue https://golang.org/issue/35559.
select {
case <-req.Context().Done():
case <-backConnCloseCh:
}
backConn.Close()
}()
defer close(backConnCloseCh)
conn, brw, err := hj.Hijack() conn, brw, err := hj.Hijack()
if err != nil { if err != nil {
// p.getErrorHandler()(rw, req, fmt.Errorf("Hijack failed on protocol switch: %v", err)) // p.getErrorHandler()(rw, req, fmt.Errorf("Hijack failed on protocol switch: %v", err))