1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-04 09:17:32 -05:00
v2fly/common/signal/exec.go

46 lines
745 B
Go
Raw Normal View History

2016-12-29 16:17:12 -05:00
package signal
import (
"context"
)
2016-12-29 16:17:12 -05:00
func executeAndFulfill(f func() error, done chan<- error) {
err := f()
if err != nil {
done <- err
}
close(done)
}
func ExecuteAsync(f func() error) <-chan error {
done := make(chan error, 1)
go executeAndFulfill(f, done)
return done
}
func ErrorOrFinish1(ctx context.Context, c <-chan error) error {
select {
case <-ctx.Done():
return ctx.Err()
case err := <-c:
return err
}
}
func ErrorOrFinish2(ctx context.Context, c1, c2 <-chan error) error {
2016-12-29 16:17:12 -05:00
select {
case <-ctx.Done():
return ctx.Err()
2016-12-29 16:17:12 -05:00
case err, failed := <-c1:
if failed {
return err
}
return ErrorOrFinish1(ctx, c2)
2016-12-29 16:17:12 -05:00
case err, failed := <-c2:
if failed {
return err
}
return ErrorOrFinish1(ctx, c1)
2016-12-29 16:17:12 -05:00
}
}