File tree Expand file tree Collapse file tree 5 files changed +86
-4
lines changed Expand file tree Collapse file tree 5 files changed +86
-4
lines changed Original file line number Diff line number Diff line change 44 branches :
55 - master
66 pull_request :
7+ types : [opened, edited, reopened, synchronize]
78jobs :
89 test :
910 strategy :
Original file line number Diff line number Diff line change 11# pkg
22
3- ![ Project status] ( https://img.shields.io/badge/version-5.6.2 -green.svg )
3+ ![ Project status] ( https://img.shields.io/badge/version-5.7.0 -green.svg )
44[ ![ Build Status] ( https://travis-ci.org/go-playground/pkg.svg?branch=master )] ( https://travis-ci.org/go-playground/pkg )
55[ ![ Coverage Status] ( https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master )] ( https://coveralls.io/github/go-playground/pkg?branch=master )
66[ ![ GoDoc] ( https://godoc.org/github.com/go-playground/pkg?status.svg )] ( https://pkg.go.dev/mod/github.com/go-playground/pkg/v5 )
Original file line number Diff line number Diff line change 1+ package contextext
2+
3+ import (
4+ "context"
5+ "fmt"
6+ "time"
7+ )
8+
9+ var _ context.Context = (* detachedContext )(nil )
10+
11+ type detachedContext struct {
12+ parent context.Context
13+ }
14+
15+ // Detach returns a new context which continues to have access to its parent values but
16+ // is no longer bound/attached to the parents timeouts nor deadlines.
17+ //
18+ // If a nil context is passed in a new Background context will be used as the parent.
19+ //
20+ // This is useful for when you wish to pass along values such as span or logging information but do not want the
21+ // current operation to be cancelled despite what upstream code/callers think.
22+ func Detach (parent context.Context ) detachedContext {
23+ if parent == nil {
24+ return detachedContext {parent : context .Background ()}
25+ }
26+ return detachedContext {parent : parent }
27+ }
28+
29+ func (c detachedContext ) Deadline () (deadline time.Time , ok bool ) {
30+ return
31+ }
32+
33+ func (c detachedContext ) Done () <- chan struct {} {
34+ return nil
35+ }
36+
37+ func (c detachedContext ) Err () error {
38+ return nil
39+ }
40+
41+ func (c detachedContext ) Value (key interface {}) interface {} {
42+ return c .parent .Value (key )
43+ }
44+
45+ func (c detachedContext ) String () string {
46+ return fmt .Sprintf ("%s.Detached" , c .parent )
47+ }
Original file line number Diff line number Diff line change 1+ package contextext
2+
3+ import (
4+ "context"
5+ "testing"
6+ "time"
7+ )
8+
9+ func TestDetach (t * testing.T ) {
10+
11+ key := & struct { name string }{name : "key" }
12+ ctx := context .WithValue (context .Background (), key , 13 )
13+ ctx , cancel := context .WithTimeout (ctx , time .Nanosecond )
14+ cancel () // cancel ensuring context has been canceled
15+
16+ select {
17+ case <- ctx .Done ():
18+ default :
19+ t .Fatal ("expected context to be cancelled" )
20+ }
21+
22+ ctx = Detach (ctx )
23+
24+ select {
25+ case <- ctx .Done ():
26+ t .Fatal ("expected context to be detached from parents cancellation" )
27+ default :
28+ rawValue := ctx .Value (key )
29+ n , ok := rawValue .(int )
30+ if ! ok || n != 13 {
31+ t .Fatalf ("expected integer woth value of 13 but got %v" , rawValue )
32+ }
33+ }
34+ }
Original file line number Diff line number Diff line change 11module github.com/go-playground/pkg/v5
22
3+ go 1.18
4+
35require (
46 github.com/go-playground/assert/v2 v2.2.0
57 github.com/go-playground/form/v4 v4.2.0
6- )
7-
8- go 1.18
8+ )
You can’t perform that action at this time.
0 commit comments