summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/reverseproxy/streaming.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-06-11 15:25:26 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-06-11 15:25:26 -0600
commitb3bff13f7d3635c5c51f71b9e4598d71deab4585 (patch)
tree9188699123ccedcd93748377ac739869cf90bd0c /modules/caddyhttp/reverseproxy/streaming.go
parent7211101c528dfaf483d1dc826f836a98008a4c9b (diff)
reverseproxy: Close websocket conn if req context cancels
This is a recent patch in the Go standard library
Diffstat (limited to 'modules/caddyhttp/reverseproxy/streaming.go')
-rw-r--r--modules/caddyhttp/reverseproxy/streaming.go15
1 files changed, 14 insertions, 1 deletions
diff --git a/modules/caddyhttp/reverseproxy/streaming.go b/modules/caddyhttp/reverseproxy/streaming.go
index 170afe8..8a7c6f7 100644
--- a/modules/caddyhttp/reverseproxy/streaming.go
+++ b/modules/caddyhttp/reverseproxy/streaming.go
@@ -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))