25 lines
459 B
Go
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)
|
|
}
|