gum/internal/timeout/context.go
2024-12-09 14:30:35 -03:00

15 lines
318 B
Go

package timeout
import (
"context"
"time"
)
// Context setup a new context that times out if the given timeout is > 0.
func Context(timeout time.Duration) (context.Context, context.CancelFunc) {
ctx := context.Background()
if timeout == 0 {
return ctx, func() {}
}
return context.WithTimeout(ctx, timeout)
}