Fix context passing (#2681)

This commit is contained in:
Stefan Budeanu
2019-03-13 14:08:33 -04:00
committed by Miek Gieben
parent 26e4026ec1
commit f798d18bdd
3 changed files with 26 additions and 8 deletions

View File

@@ -0,0 +1,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
}