1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-06 03:54:22 -04:00
v2fly/common/task/task.go

187 lines
3.5 KiB
Go
Raw Normal View History

2018-05-27 07:02:29 -04:00
package task
import (
"context"
2018-08-26 18:11:32 -04:00
"strings"
2018-05-27 07:02:29 -04:00
2018-08-26 18:11:32 -04:00
"v2ray.com/core/common"
2018-05-27 08:42:53 -04:00
"v2ray.com/core/common/signal/semaphore"
2018-05-27 07:02:29 -04:00
)
type Task func() error
2018-08-26 18:11:32 -04:00
type MultiError []error
func (e MultiError) Error() string {
var r strings.Builder
common.Must2(r.WriteString("multierr: "))
for _, err := range e {
common.Must2(r.WriteString(err.Error()))
common.Must2(r.WriteString(" | "))
}
return r.String()
}
2018-05-27 07:02:29 -04:00
type executionContext struct {
ctx context.Context
2018-06-04 08:29:05 -04:00
tasks []Task
2018-05-27 07:02:29 -04:00
onSuccess Task
onFailure Task
}
func (c *executionContext) executeTask() error {
2018-06-04 08:29:05 -04:00
if len(c.tasks) == 0 {
2018-05-27 07:02:29 -04:00
return nil
}
2018-07-15 10:04:02 -04:00
// Reuse current goroutine if we only have one task to run.
if len(c.tasks) == 1 && c.ctx == nil {
return c.tasks[0]()
}
2018-06-04 08:29:05 -04:00
ctx := context.Background()
2018-05-27 07:02:29 -04:00
2018-06-04 08:29:05 -04:00
if c.ctx != nil {
ctx = c.ctx
2018-05-27 07:02:29 -04:00
}
2018-06-04 08:29:05 -04:00
return executeParallel(ctx, c.tasks)
2018-05-27 07:02:29 -04:00
}
func (c *executionContext) run() error {
err := c.executeTask()
if err == nil && c.onSuccess != nil {
return c.onSuccess()
}
if err != nil && c.onFailure != nil {
return c.onFailure()
}
return err
}
type ExecutionOption func(*executionContext)
func WithContext(ctx context.Context) ExecutionOption {
return func(c *executionContext) {
c.ctx = ctx
}
}
func Parallel(tasks ...Task) ExecutionOption {
return func(c *executionContext) {
2018-06-04 08:29:05 -04:00
c.tasks = append(c.tasks, tasks...)
2018-05-27 07:02:29 -04:00
}
}
2018-08-26 18:11:32 -04:00
// Sequential runs all tasks sequentially, and returns the first error encountered.Sequential
// Once a task returns an error, the following tasks will not run.
2018-05-27 07:02:29 -04:00
func Sequential(tasks ...Task) ExecutionOption {
return func(c *executionContext) {
2018-08-26 18:11:32 -04:00
switch len(tasks) {
case 0:
2018-06-07 00:38:45 -04:00
return
2018-08-26 18:11:32 -04:00
case 1:
c.tasks = append(c.tasks, tasks[0])
default:
c.tasks = append(c.tasks, func() error {
return execute(tasks...)
})
2018-06-07 00:38:45 -04:00
}
2018-08-26 18:11:32 -04:00
}
}
2018-06-07 00:38:45 -04:00
2018-08-26 18:11:32 -04:00
func SequentialAll(tasks ...Task) ExecutionOption {
return func(c *executionContext) {
switch len(tasks) {
case 0:
2018-06-07 00:38:45 -04:00
return
2018-08-26 18:11:32 -04:00
case 1:
c.tasks = append(c.tasks, tasks[0])
default:
c.tasks = append(c.tasks, func() error {
var merr MultiError
for _, task := range tasks {
if err := task(); err != nil {
merr = append(merr, err)
}
}
if len(merr) == 0 {
return nil
}
return merr
})
2018-06-07 00:38:45 -04:00
}
2018-05-27 07:02:29 -04:00
}
}
func OnSuccess(task Task) ExecutionOption {
return func(c *executionContext) {
c.onSuccess = task
}
}
func OnFailure(task Task) ExecutionOption {
return func(c *executionContext) {
c.onFailure = task
}
}
2018-08-05 11:56:49 -04:00
func Single(task Task, opts ...ExecutionOption) Task {
return Run(append([]ExecutionOption{Sequential(task)}, opts...)...)
2018-05-27 07:02:29 -04:00
}
func Run(opts ...ExecutionOption) Task {
var c executionContext
for _, opt := range opts {
opt(&c)
}
return func() error {
return c.run()
}
}
// execute runs a list of tasks sequentially, returns the first error encountered or nil if all tasks pass.
func execute(tasks ...Task) error {
for _, task := range tasks {
if err := task(); err != nil {
return err
}
}
return nil
}
// executeParallel executes a list of tasks asynchronously, returns the first error encountered or nil if all tasks pass.
2018-06-04 08:29:05 -04:00
func executeParallel(ctx context.Context, tasks []Task) error {
2018-05-27 07:02:29 -04:00
n := len(tasks)
2018-05-27 08:42:53 -04:00
s := semaphore.New(n)
2018-05-27 07:02:29 -04:00
done := make(chan error, 1)
for _, task := range tasks {
<-s.Wait()
2018-08-05 11:56:49 -04:00
go func(f Task) {
err := f()
if err == nil {
s.Signal()
return
}
select {
case done <- err:
default:
2018-05-27 07:02:29 -04:00
}
}(task)
}
for i := 0; i < n; i++ {
select {
case err := <-done:
return err
2018-06-04 08:29:05 -04:00
case <-ctx.Done():
return ctx.Err()
2018-05-27 07:02:29 -04:00
case <-s.Wait():
}
}
return nil
}