Skip to content

Commit e332e7d

Browse files
Acconutmarius
andauthored
handler: Retain context values via context.WithoutCancel (#1342)
Co-authored-by: marius <marius@example.com>
1 parent 834b4d0 commit e332e7d

1 file changed

Lines changed: 2 additions & 7 deletions

File tree

pkg/handler/context.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,13 @@ func (h UnroutedHandler) getContext(w http.ResponseWriter, r *http.Request) *htt
8080
return c
8181
}
8282

83-
func (c httpContext) Value(key any) any {
84-
// We overwrite the Value function to ensure that the values from the request
85-
// context are returned because c.Context does not contain any values.
86-
return c.req.Context().Value(key)
87-
}
88-
8983
// newDelayedContext returns a context that is cancelled with a delay. If the parent context
9084
// is done, the new context will also be cancelled but only after waiting the specified delay.
9185
// Note: The parent context MUST be cancelled or otherwise this will leak resources. In the
9286
// case of http.Request.Context, the net/http package ensures that the context is always cancelled.
9387
func newDelayedContext(parent context.Context, delay time.Duration) context.Context {
94-
ctx, cancel := context.WithCancel(context.Background())
88+
// Use context.WithoutCancel to preserve the values.
89+
ctx, cancel := context.WithCancel(context.WithoutCancel(parent))
9590
go func() {
9691
<-parent.Done()
9792
<-time.After(delay)

0 commit comments

Comments
 (0)