Skip to content

Commit 941acf6

Browse files
committed
fix: don't double-close hijacked connections
1 parent 3c264de commit 941acf6

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

proxy/websocket.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ func newWebsocket(cfg *websocketConfig) (*Websocket, error) {
6262
}
6363

6464
p.frontend = &fasthttp.Server{
65-
ConnState: p.upstreamConnectionChanged,
66-
Handler: p.receive,
67-
Logger: logutils.FasthttpLogger(l),
68-
Name: cfg.name,
69-
ReadTimeout: 5 * time.Second,
70-
WriteTimeout: 5 * time.Second,
65+
ConnState: p.upstreamConnectionChanged,
66+
Handler: p.receive,
67+
KeepHijackedConns: true, // we close them explicitly
68+
Logger: logutils.FasthttpLogger(l),
69+
Name: cfg.name,
70+
ReadTimeout: 5 * time.Second,
71+
WriteTimeout: 5 * time.Second,
7172
}
7273

7374
if cfg.proxy.TLSCertificate != "" && cfg.proxy.TLSKey != "" {
@@ -306,15 +307,24 @@ func (p *Websocket) websocket(ctx *fasthttp.RequestCtx) func(frontend *websocket
306307
)
307308
}
308309

309-
err = p.closeWebsocket(pump.backend, reason)
310-
l.Info("Closed backend connection",
311-
zap.Error(err),
312-
)
310+
if err := p.closeWebsocket(pump.backend, reason); err == nil {
311+
l.Info("Closed backend connection")
312+
} else {
313+
l.Warn("Failed to close backend connection",
314+
zap.Error(err),
315+
)
316+
}
313317

314-
err = p.closeWebsocket(pump.frontend, reason)
315-
l.Info("Closed frontend connection",
316-
zap.Error(err),
317-
)
318+
if err = p.closeWebsocket(pump.frontend, reason); err == nil {
319+
l.Info("Closed frontend connection")
320+
metrics.FrontendConnectionsClosedCount.Add(context.TODO(), 1, otelapi.WithAttributes(
321+
attribute.KeyValue{Key: "proxy", Value: attribute.StringValue(p.cfg.name)},
322+
))
323+
} else {
324+
l.Warn("Failed to close frontend connection",
325+
zap.Error(err),
326+
)
327+
}
318328

319329
p.mxConnections.Lock()
320330
delete(p.pumps, addr)

0 commit comments

Comments
 (0)