From 07b186d8460f00bdb514f66285810d953581ff2a Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Fri, 25 Apr 2025 14:52:23 +0200 Subject: [PATCH] feat: add middleware for handling requests without cancel context Introduced `GTagMiddlewarWithoutCancel` and `MPv2MiddlewarWithoutCancel` to allow handling of requests with contexts where cancellation signals are ignored. This improves flexibility for specific use cases where cancel propagation is not desired. --- pkg/client/gtagmiddleware.go | 7 +++++++ pkg/client/mpv2middleware.go | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/pkg/client/gtagmiddleware.go b/pkg/client/gtagmiddleware.go index d0a667d..378178d 100644 --- a/pkg/client/gtagmiddleware.go +++ b/pkg/client/gtagmiddleware.go @@ -1,6 +1,7 @@ package client import ( + "context" "net/http" "strings" @@ -58,6 +59,12 @@ func GTagMiddlewarClientID(next GTagHandler) GTagHandler { } } +func GTagMiddlewarWithoutCancel(next GTagHandler) GTagHandler { + return func(r *http.Request, payload *gtag.Payload) error { + return next(r.WithContext(context.WithoutCancel(r.Context())), payload) + } +} + func GTagMiddlewarSessionID(measurementID string) GTagMiddleware { measurementID = strings.Split(measurementID, "-")[1] return func(next GTagHandler) GTagHandler { diff --git a/pkg/client/mpv2middleware.go b/pkg/client/mpv2middleware.go index 5670d99..500dd02 100644 --- a/pkg/client/mpv2middleware.go +++ b/pkg/client/mpv2middleware.go @@ -1,6 +1,7 @@ package client import ( + "context" "net/http" "strings" @@ -47,6 +48,12 @@ func MPv2MiddlewarDebugMode(next MPv2Handler) MPv2Handler { } } +func MPv2MiddlewarWithoutCancel(next MPv2Handler) MPv2Handler { + return func(r *http.Request, payload *mpv2.Payload[any]) error { + return next(r.WithContext(context.WithoutCancel(r.Context())), payload) + } +} + func MPv2MiddlewareUserID(cookieName string) MPv2Middleware { return func(next MPv2Handler) MPv2Handler { return func(r *http.Request, payload *mpv2.Payload[any]) error {