forked from TrueCloudLab/distribution
9c88801a12
Back in the before time, the best practices surrounding usage of Context weren't quite worked out. We defined our own type to make usage easier. As this packaged was used elsewhere, it make it more and more challenging to integrate with the forked `Context` type. Now that it is available in the standard library, we can just use that one directly. To make usage more consistent, we now use `dcontext` when referring to the distribution context package. Signed-off-by: Stephen J Day <stephen.day@docker.com>
25 lines
716 B
Go
25 lines
716 B
Go
package context
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// Since looks up key, which should be a time.Time, and returns the duration
|
|
// since that time. If the key is not found, the value returned will be zero.
|
|
// This is helpful when inferring metrics related to context execution times.
|
|
func Since(ctx context.Context, key interface{}) time.Duration {
|
|
if startedAt, ok := ctx.Value(key).(time.Time); ok {
|
|
return time.Since(startedAt)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// GetStringValue returns a string value from the context. The empty string
|
|
// will be returned if not found.
|
|
func GetStringValue(ctx context.Context, key interface{}) (value string) {
|
|
if valuev, ok := ctx.Value(key).(string); ok {
|
|
value = valuev
|
|
}
|
|
return value
|
|
}
|