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

47 lines
811 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)
}
2018-03-14 22:32:10 -04:00
// ExecuteAsync executes a function asynchronously and return its result.
2016-12-29 16:17:12 -05:00
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()
2017-04-17 13:33:22 -04:00
case err := <-c1:
if err != nil {
2016-12-29 16:17:12 -05:00
return err
}
return ErrorOrFinish1(ctx, c2)
2017-04-17 13:33:22 -04:00
case err := <-c2:
if err != nil {
2016-12-29 16:17:12 -05:00
return err
}
return ErrorOrFinish1(ctx, c1)
2016-12-29 16:17:12 -05:00
}
}