diff options
author | WeidiDeng <[email protected]> | 2024-04-16 01:37:37 +0800 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2024-12-20 09:39:49 -0700 |
commit | b1fb7d813e892733a972467ead6df62f6aa6d186 (patch) | |
tree | 0fa30d8e9f26c2b752682dc608b5666dc07c3a69 | |
parent | 93ec641d5da5c9974dff7b9f6f63b881c0b7a581 (diff) | |
download | caddy-stripe.tar.gz caddy-stripe.zip |
reverseproxy: Wait for both ends of websocket to close (#6175)stripe
-rw-r--r-- | modules/caddyhttp/reverseproxy/reverseproxy.go | 4 | ||||
-rw-r--r-- | modules/caddyhttp/reverseproxy/streaming.go | 6 |
2 files changed, 8 insertions, 2 deletions
diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index 0a286e62d..25e97667d 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -947,7 +947,9 @@ func (h *Handler) finalizeResponse( ) error { // deal with 101 Switching Protocols responses: (WebSocket, h2c, etc) if res.StatusCode == http.StatusSwitchingProtocols { - h.handleUpgradeResponse(logger, rw, req, res) + var wg sync.WaitGroup + h.handleUpgradeResponse(logger, &wg, rw, req, res) + wg.Wait() return nil } diff --git a/modules/caddyhttp/reverseproxy/streaming.go b/modules/caddyhttp/reverseproxy/streaming.go index a517fea4d..57771f368 100644 --- a/modules/caddyhttp/reverseproxy/streaming.go +++ b/modules/caddyhttp/reverseproxy/streaming.go @@ -182,7 +182,7 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, wg *sync.WaitGroup, defer deleteFrontConn() defer deleteBackConn() - spc := switchProtocolCopier{user: conn, backend: backConn} + spc := switchProtocolCopier{user: conn, backend: backConn, wg: wg} // setup the timeout if requested var timeoutc <-chan time.Time @@ -193,6 +193,7 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, wg *sync.WaitGroup, } errc := make(chan error, 1) + wg.Add(2) go spc.copyToBackend(errc) go spc.copyFromBackend(errc) select { @@ -590,16 +591,19 @@ func (m *maxLatencyWriter) stop() { // forth have nice names in stacks. type switchProtocolCopier struct { user, backend io.ReadWriteCloser + wg *sync.WaitGroup } func (c switchProtocolCopier) copyFromBackend(errc chan<- error) { _, err := io.Copy(c.user, c.backend) errc <- err + c.wg.Done() } func (c switchProtocolCopier) copyToBackend(errc chan<- error) { _, err := io.Copy(c.backend, c.user) errc <- err + c.wg.Done() } var streamingBufPool = sync.Pool{ |