-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
taskrunner.go
72 lines (60 loc) · 1.79 KB
/
taskrunner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package threading
import (
"errors"
"sync"
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/rescue"
)
// ErrTaskRunnerBusy is the error that indicates the runner is busy.
var ErrTaskRunnerBusy = errors.New("task runner is busy")
// A TaskRunner is used to control the concurrency of goroutines.
type TaskRunner struct {
limitChan chan lang.PlaceholderType
waitGroup sync.WaitGroup
}
// NewTaskRunner returns a TaskRunner.
func NewTaskRunner(concurrency int) *TaskRunner {
return &TaskRunner{
limitChan: make(chan lang.PlaceholderType, concurrency),
}
}
// Schedule schedules a task to run under concurrency control.
func (rp *TaskRunner) Schedule(task func()) {
// Why we add waitGroup first, in case of race condition on starting a task and wait returns.
// For example, limitChan is full, and the task is scheduled to run, but the waitGroup is not added,
// then the wait returns, and the task is then scheduled to run, but caller thinks all tasks are done.
// the same reason for ScheduleImmediately.
rp.waitGroup.Add(1)
rp.limitChan <- lang.Placeholder
go func() {
defer rescue.Recover(func() {
<-rp.limitChan
rp.waitGroup.Done()
})
task()
}()
}
// ScheduleImmediately schedules a task to run immediately under concurrency control.
// It returns ErrTaskRunnerBusy if the runner is busy.
func (rp *TaskRunner) ScheduleImmediately(task func()) error {
// Why we add waitGroup first, check the comment in Schedule.
rp.waitGroup.Add(1)
select {
case rp.limitChan <- lang.Placeholder:
default:
rp.waitGroup.Done()
return ErrTaskRunnerBusy
}
go func() {
defer rescue.Recover(func() {
<-rp.limitChan
rp.waitGroup.Done()
})
task()
}()
return nil
}
// Wait waits all running tasks to be done.
func (rp *TaskRunner) Wait() {
rp.waitGroup.Wait()
}