@@ -34,30 +34,33 @@ func WithHandlerContext(ctx context.Context) context.Context {
34
34
return context .WithValue (ctx , handlerCtxKey , & handlerCtx {})
35
35
}
36
36
37
- // InHandlerContext returns true if the given context is a handler context where [AddHandlerLinks] and [HandlerLinks]
37
+ // IsHandlerContext returns true if the given context is a handler context where [AddHandlerLinks] and [HandlerLinks]
38
38
// can be called. It will only return true when called from an [Operation] handler Start method or from a [Handler]
39
39
// StartOperation method.
40
40
//
41
41
// NOTE: Experimental
42
- func InHandlerContext (ctx context.Context ) bool {
42
+ func IsHandlerContext (ctx context.Context ) bool {
43
43
return ctx .Value (handlerCtxKey ) != nil
44
44
}
45
45
46
46
// HandlerLinks retrieves the attached links on the given handler context. The returned slice should not be mutated.
47
- // The context provided must be the context passed to the handler or this method will panic, [InHandlerContext ] can be
47
+ // The context provided must be the context passed to the handler or this method will panic, [IsHandlerContext ] can be
48
48
// used to verify the context is valid.
49
49
//
50
50
// NOTE: Experimental
51
51
func HandlerLinks (ctx context.Context ) []Link {
52
52
hctx := ctx .Value (handlerCtxKey ).(* handlerCtx )
53
53
hctx .mu .Lock ()
54
- defer hctx .mu .Unlock ()
55
- return hctx .links
54
+ links := hctx .links
55
+ hctx .mu .Unlock ()
56
+ cpy := make ([]Link , len (links ))
57
+ copy (cpy , links )
58
+ return cpy
56
59
}
57
60
58
61
// AddHandlerLinks associates links with the current operation to be propagated back to the caller. This method
59
62
// Can be called from an [Operation] handler Start method or from a [Handler] StartOperation method. The context
60
- // provided must be the context passed to the handler or this method will panic, [InHandlerContext ] can be used to
63
+ // provided must be the context passed to the handler or this method will panic, [IsHandlerContext ] can be used to
61
64
// verify the context is valid. This method may be called multiple times for a given handler, each call appending
62
65
// additional links. Links will only be attached on successful responses.
63
66
//
@@ -71,7 +74,7 @@ func AddHandlerLinks(ctx context.Context, links ...Link) {
71
74
72
75
// SetHandlerLinks associates links with the current operation to be propagated back to the caller. This method
73
76
// Can be called from an [Operation] handler Start method or from a [Handler] StartOperation method. The context
74
- // provided must be the context passed to the handler or this method will panic, [InHandlerContext ] can be used to
77
+ // provided must be the context passed to the handler or this method will panic, [IsHandlerContext ] can be used to
75
78
// verify the context is valid. This method replaces any previously associated links, it is recommended to use
76
79
// [AddHandlerLinks] to avoid accidental override. Links will only be attached on successful responses.
77
80
//
0 commit comments