From b69aa821ebd4ecb6d18366cb99d23496375dd1c4 Mon Sep 17 00:00:00 2001 From: Steve Munene <61874077+nyagamunene@users.noreply.github.com> Date: Tue, 23 Apr 2024 12:01:18 +0300 Subject: [PATCH] MPX-59 - Fix MQTT over WS context cancellation (#61) * Upgraded the context in http handler Signed-off-by: nyagamunene * Created new context for pass function Signed-off-by: nyagamunene * Commented on the new changes Signed-off-by: nyagamunene * Added more commit messages Signed-off-by: nyagamunene --------- Signed-off-by: nyagamunene --- pkg/mqtt/websocket/websocket.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/mqtt/websocket/websocket.go b/pkg/mqtt/websocket/websocket.go index 9456d18..dae77e7 100644 --- a/pkg/mqtt/websocket/websocket.go +++ b/pkg/mqtt/websocket/websocket.go @@ -68,12 +68,15 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - go p.pass(r.Context(), cconn) + go p.pass(cconn) } -func (p Proxy) pass(ctx context.Context, in *websocket.Conn) { +func (p Proxy) pass(in *websocket.Conn) { defer in.Close() - + // Using a new context so as to avoiding infinitely long traces. + // And also avoiding proxy cancellation due to parent context cancellation. + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() dialer := &websocket.Dialer{ Subprotocols: []string{"mqtt"}, }