-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_context.go
More file actions
73 lines (64 loc) · 2.12 KB
/
request_context.go
File metadata and controls
73 lines (64 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package options
import "context"
// RequestContext holds both request-scoped options and log-scoped fields
// in a single context value, reducing context.WithValue allocations from 2 to 1.
// Both fields are eagerly initialized to avoid data races from concurrent lazy init.
type RequestContext struct {
opts *Options
logFields *Options
}
var requestContextKey contextKey = "ColdBrewRequestContext"
// RequestContextFromContext retrieves the RequestContext from ctx.
// Returns nil if not present.
func RequestContextFromContext(ctx context.Context) *RequestContext {
if rc, ok := ctx.Value(requestContextKey).(*RequestContext); ok {
return rc
}
return nil
}
// getOrCreateRequestContext returns the existing RequestContext or creates one.
// Both opts and logFields are eagerly allocated so that concurrent access
// from multiple goroutines sharing the same context is safe without
// additional synchronization on the RequestContext itself.
func getOrCreateRequestContext(ctx context.Context) (context.Context, *RequestContext) {
if rc := RequestContextFromContext(ctx); rc != nil {
return ctx, rc
}
rc := &RequestContext{
opts: &Options{},
logFields: &Options{},
}
return context.WithValue(ctx, requestContextKey, rc), rc
}
// Opts returns the Options for request-scoped key-value pairs.
func (rc *RequestContext) Opts() *Options {
return rc.opts
}
// LogFields returns the Options used for log fields.
func (rc *RequestContext) LogFields() *Options {
return rc.logFields
}
// AddToLogFields adds a key-value pair to the log fields stored in ctx.
// If ctx is nil, context.Background() is used.
func AddToLogFields(ctx context.Context, key string, value any) context.Context {
if ctx == nil {
ctx = context.Background()
}
if key == "" {
return ctx
}
ctx, rc := getOrCreateRequestContext(ctx)
rc.logFields.Add(key, value)
return ctx
}
// LogFieldsFromContext retrieves the log fields Options from context.
// Returns nil if not present.
func LogFieldsFromContext(ctx context.Context) *Options {
if ctx == nil {
return nil
}
if rc := RequestContextFromContext(ctx); rc != nil {
return rc.logFields
}
return nil
}