task.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package task
  2. import (
  3. "context"
  4. "v2ray.com/core/common/signal/semaphore"
  5. )
  6. type Task func() error
  7. type executionContext struct {
  8. ctx context.Context
  9. tasks []Task
  10. onSuccess Task
  11. onFailure Task
  12. }
  13. func (c *executionContext) executeTask() error {
  14. if len(c.tasks) == 0 {
  15. return nil
  16. }
  17. // Reuse current goroutine if we only have one task to run.
  18. if len(c.tasks) == 1 && c.ctx == nil {
  19. return c.tasks[0]()
  20. }
  21. ctx := context.Background()
  22. if c.ctx != nil {
  23. ctx = c.ctx
  24. }
  25. return executeParallel(ctx, c.tasks)
  26. }
  27. func (c *executionContext) run() error {
  28. err := c.executeTask()
  29. if err == nil && c.onSuccess != nil {
  30. return c.onSuccess()
  31. }
  32. if err != nil && c.onFailure != nil {
  33. return c.onFailure()
  34. }
  35. return err
  36. }
  37. type ExecutionOption func(*executionContext)
  38. func WithContext(ctx context.Context) ExecutionOption {
  39. return func(c *executionContext) {
  40. c.ctx = ctx
  41. }
  42. }
  43. func Parallel(tasks ...Task) ExecutionOption {
  44. return func(c *executionContext) {
  45. c.tasks = append(c.tasks, tasks...)
  46. }
  47. }
  48. func Sequential(tasks ...Task) ExecutionOption {
  49. return func(c *executionContext) {
  50. if len(tasks) == 0 {
  51. return
  52. }
  53. if len(tasks) == 1 {
  54. c.tasks = append(c.tasks, tasks[0])
  55. return
  56. }
  57. c.tasks = append(c.tasks, func() error {
  58. return execute(tasks...)
  59. })
  60. }
  61. }
  62. func OnSuccess(task Task) ExecutionOption {
  63. return func(c *executionContext) {
  64. c.onSuccess = task
  65. }
  66. }
  67. func OnFailure(task Task) ExecutionOption {
  68. return func(c *executionContext) {
  69. c.onFailure = task
  70. }
  71. }
  72. func Single(task Task, opts ...ExecutionOption) Task {
  73. return Run(append([]ExecutionOption{Sequential(task)}, opts...)...)
  74. }
  75. func Run(opts ...ExecutionOption) Task {
  76. var c executionContext
  77. for _, opt := range opts {
  78. opt(&c)
  79. }
  80. return func() error {
  81. return c.run()
  82. }
  83. }
  84. // execute runs a list of tasks sequentially, returns the first error encountered or nil if all tasks pass.
  85. func execute(tasks ...Task) error {
  86. for _, task := range tasks {
  87. if err := task(); err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. // executeParallel executes a list of tasks asynchronously, returns the first error encountered or nil if all tasks pass.
  94. func executeParallel(ctx context.Context, tasks []Task) error {
  95. n := len(tasks)
  96. s := semaphore.New(n)
  97. done := make(chan error, 1)
  98. for _, task := range tasks {
  99. <-s.Wait()
  100. go func(f Task) {
  101. err := f()
  102. if err == nil {
  103. s.Signal()
  104. return
  105. }
  106. select {
  107. case done <- err:
  108. default:
  109. }
  110. }(task)
  111. }
  112. for i := 0; i < n; i++ {
  113. select {
  114. case err := <-done:
  115. return err
  116. case <-ctx.Done():
  117. return ctx.Err()
  118. case <-s.Wait():
  119. }
  120. }
  121. return nil
  122. }