1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-12-26 03:55:26 -05:00

task engine

This commit is contained in:
Darien Raymond
2016-12-28 23:42:32 +01:00
parent 7d31c0641b
commit 5ff2b3453a
2 changed files with 124 additions and 38 deletions

41
common/task/task.go Normal file
View File

@@ -0,0 +1,41 @@
package task
import (
"sync"
)
type Task interface {
Execute() error
}
type ParallelExecutor struct {
sync.Mutex
tasks sync.WaitGroup
errors []error
}
func (pe *ParallelExecutor) track(err error) {
if err == nil {
return
}
pe.Lock()
pe.errors = append(pe.errors, err)
pe.Unlock()
}
func (pe *ParallelExecutor) Execute(task Task) {
pe.tasks.Add(1)
go func() {
pe.track(task.Execute())
pe.tasks.Done()
}()
}
func (pe *ParallelExecutor) Wait() {
pe.tasks.Wait()
}
func (pe *ParallelExecutor) Errors() []error {
return pe.errors
}