package util import ( "context" "time" ) // WithTimeout returns ctx with the timeout applied, or ctx itself if the // timeout is 0 or less (no limit). Either way the returned cancel function // must be called. func WithTimeout( ctx context.Context, timeout time.Duration, ) (context.Context, context.CancelFunc) { if timeout <= 0 { return ctx, func() { } } return context.WithTimeout(ctx, timeout) }