Files
with/ctx.go
Bel LaPointe cc18763944 stead from pg
2026-03-09 09:06:33 -06:00

25 lines
459 B
Go

package with
import (
"context"
"os/signal"
"syscall"
"time"
)
func Context(foo func(context.Context) error) error {
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
defer can()
if err := foo(ctx); err != nil && ctx.Err() == nil {
return err
}
return nil
}
func Timeout(ctx context.Context, d time.Duration, foo func(context.Context) error) error {
ctx, can := context.WithTimeout(ctx, d)
defer can()
return foo(ctx)
}