aboutsummaryrefslogtreecommitdiff
path: root/plugin/dnstap/gocontext.go
blob: a8cc2c2b44d48db4584e1b162c494c91ae279c91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package dnstap

import "context"

type contextKey struct{}

var dnstapKey = contextKey{}

// ContextWithTapper returns a new `context.Context` that holds a reference to
// `t`'s Tapper.
func ContextWithTapper(ctx context.Context, t Tapper) context.Context {
	return context.WithValue(ctx, dnstapKey, t)
}

// TapperFromContext returns the `Tapper` previously associated with `ctx`, or
// `nil` if no such `Tapper` could be found.
func TapperFromContext(ctx context.Context) Tapper {
	val := ctx.Value(dnstapKey)
	if sp, ok := val.(Tapper); ok {
		return sp
	}
	return nil
}