stead from pg
This commit is contained in:
24
ctx.go
Normal file
24
ctx.go
Normal file
@@ -0,0 +1,24 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user