2023-02-27 22:36:43 +01:00
|
|
|
package call
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type durationKey struct{}
|
|
|
|
|
|
|
|
var key *durationKey = (*durationKey)(nil)
|
|
|
|
|
2023-07-07 11:15:05 +03:00
|
|
|
// WithTimestamp sets [time.Now()] to the call field in the context
|
|
|
|
// if it's not already set.
|
2023-02-27 22:36:43 +01:00
|
|
|
func WithTimestamp(parent context.Context) context.Context {
|
|
|
|
if parent.Value(key) != nil {
|
|
|
|
return parent
|
|
|
|
}
|
2023-07-07 11:15:05 +03:00
|
|
|
return ResetTimestamp(parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetTimestamp sets [time.Now()] to the call field in the context,
|
|
|
|
// overwriting any previously set call timestamp.
|
|
|
|
func ResetTimestamp(parent context.Context) context.Context {
|
2023-02-27 22:36:43 +01:00
|
|
|
return context.WithValue(parent, key, time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromContext returns the [time.Time] the call hit the api
|
|
|
|
func FromContext(ctx context.Context) (t time.Time) {
|
|
|
|
value := ctx.Value(key)
|
|
|
|
if t, ok := value.(time.Time); ok {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Took returns the time the call took so far
|
|
|
|
func Took(ctx context.Context) time.Duration {
|
|
|
|
start := FromContext(ctx)
|
|
|
|
if start.IsZero() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return time.Since(start)
|
|
|
|
}
|