mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 11:27:42 +08:00
23 lines
350 B
Go
23 lines
350 B
Go
package util
|
|
|
|
import "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
|
|
|
|
func ContextDo(ctx context.Context, f func() error) error {
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
select {
|
|
case <-ctx.Done():
|
|
case ch <- f():
|
|
}
|
|
}()
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case val := <-ch:
|
|
return val
|
|
}
|
|
return nil
|
|
}
|