kubernetes/vendor/github.com/jonboulle/clockwork/context.go
Davanum Srinivas 7f8210e33f
Update to last versions of some very infrequently updated repos
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
2024-10-14 17:43:29 -04:00

25 lines
790 B
Go

package clockwork
import (
"context"
)
// contextKey is private to this package so we can ensure uniqueness here. This
// type identifies context values provided by this package.
type contextKey string
// keyClock provides a clock for injecting during tests. If absent, a real clock should be used.
var keyClock = contextKey("clock") // clockwork.Clock
// AddToContext creates a derived context that references the specified clock.
func AddToContext(ctx context.Context, clock Clock) context.Context {
return context.WithValue(ctx, keyClock, clock)
}
// FromContext extracts a clock from the context. If not present, a real clock is returned.
func FromContext(ctx context.Context) Clock {
if clock, ok := ctx.Value(keyClock).(Clock); ok {
return clock
}
return NewRealClock()
}