Skip to content

Commit

Permalink
reverseproxy: Close websocket conn if req context cancels
Browse files Browse the repository at this point in the history
This is a recent patch in the Go standard library
  • Loading branch information
mholt committed Jun 11, 2020
1 parent 7211101 commit b3bff13
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion modules/caddyhttp/reverseproxy/streaming.go
Expand Up @@ -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"))
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()
if err != nil {
// p.getErrorHandler()(rw, req, fmt.Errorf("Hijack failed on protocol switch: %v", err))
Expand Down

0 comments on commit b3bff13

Please sign in to comment.