Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
reverseproxy: Close websocket conn if req context cancels
This is a recent patch in the Go standard library
Matthew Holt committed
Jun 11, 2020 at 15:25 UTC
b3bff13f7d3635c5c51f71b9e4598d71deab4585
1 file changed
+14
-1
modules/caddyhttp/reverseproxy/streaming.go
+14
-1
@@ -49,7 +49,20 @@ func (h Handler) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request
49
// p.getErrorHandler()(rw, req, fmt.Errorf("internal error: 101 switching protocols response with non-writable body"))
50
return
51
}
52
- defer backConn.Close()
52
+
53
+ // adopted from https://github.com/golang/go/commit/8bcf2834afdf6a1f7937390903a41518715ef6f5
54
+ backConnCloseCh := make(chan struct{})
55
+ go func() {
56
+ // Ensure that the cancelation of a request closes the backend.
57
+ // See issue https://golang.org/issue/35559.
58
+ select {
59
+ case <-req.Context().Done():
60
+ case <-backConnCloseCh:
61
+ }
62
+ backConn.Close()
63
+ }()
64
+ defer close(backConnCloseCh)
65
+
66
conn, brw, err := hj.Hijack()
67
if err != nil {
68
// p.getErrorHandler()(rw, req, fmt.Errorf("Hijack failed on protocol switch: %v", err))