-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
context.go
49 lines (40 loc) · 1023 Bytes
/
context.go
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
package proxy
import (
"context"
)
type key int
const (
inboundMetaKey key = iota
outboundMetaKey
dialerKey
)
func ContextWithInboundMeta(ctx context.Context, meta *InboundHandlerMeta) context.Context {
return context.WithValue(ctx, inboundMetaKey, meta)
}
func InboundMetaFromContext(ctx context.Context) *InboundHandlerMeta {
v := ctx.Value(inboundMetaKey)
if v == nil {
return nil
}
return v.(*InboundHandlerMeta)
}
func ContextWithOutboundMeta(ctx context.Context, meta *OutboundHandlerMeta) context.Context {
return context.WithValue(ctx, outboundMetaKey, meta)
}
func OutboundMetaFromContext(ctx context.Context) *OutboundHandlerMeta {
v := ctx.Value(outboundMetaKey)
if v == nil {
return nil
}
return v.(*OutboundHandlerMeta)
}
func ContextWithDialer(ctx context.Context, dialer Dialer) context.Context {
return context.WithValue(ctx, dialerKey, dialer)
}
func DialerFromContext(ctx context.Context) Dialer {
v := ctx.Value(dialerKey)
if v == nil {
return nil
}
return v.(Dialer)
}