-
Notifications
You must be signed in to change notification settings - Fork 5
/
task_pool.go
186 lines (164 loc) · 4.43 KB
/
task_pool.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package task
import (
"errors"
"sync"
"sync/atomic"
"time"
)
var (
IllegalStateExceptionErr = errors.New("IllegalStateException")
TimeoutExceptionErr = errors.New("TimeoutException")
)
type TaskPool struct {
lock sync.RWMutex
taskPoolShutDownFlag bool
factory *taskFactory
queue []*task
retryQueue *retryQueue
ioWorker *ioWorker
maxTaskNum int
MaxBlockSec int
// wg
retryQueueWait *sync.WaitGroup
taskPoolWait *sync.WaitGroup
ioWorkerWait *sync.WaitGroup
}
func NewTaskPool(c *Config) (*TaskPool, error) {
taskPool := &TaskPool{
lock: sync.RWMutex{},
taskPoolShutDownFlag: false,
maxTaskNum: c.MaxTaskNum,
queue: []*task{},
retryQueueWait: &sync.WaitGroup{},
taskPoolWait: &sync.WaitGroup{},
ioWorkerWait: &sync.WaitGroup{},
}
factory := newTaskFactory(c)
retryQueue := newRetryQueue(taskPool.retryQueueWait)
worker, err := newIoWorker(c.MaxIoWorkerNum, retryQueue, taskPool.ioWorkerWait)
if err != nil {
return nil, err
}
taskPool.factory = factory
taskPool.retryQueue = retryQueue
taskPool.ioWorker = worker
return taskPool, nil
}
func (taskPool *TaskPool) Start() {
taskPool.retryQueueWait.Add(1)
taskPool.taskPoolWait.Add(1)
go taskPool.start()
}
// Limited closing transfer parameter nil, safe closing transfer timeout time, timeout Ms parameter in milliseconds
func (taskPool *TaskPool) Close(timeoutMs int64) error {
startCloseTime := time.Now().Unix()
taskPool.retryQueue.retryQueueShutDownFlag = true
taskPool.retryQueueWait.Wait()
taskPool.taskPoolShutDownFlag = true
for {
taskCount := atomic.LoadInt64(&taskPool.ioWorker.taskCount)
if taskCount != 0 && time.Now().Unix()-startCloseTime <= timeoutMs {
time.Sleep(time.Second)
} else if taskCount == 0 && len(taskPool.queue) == 0 {
return nil
} else if time.Now().Unix()-startCloseTime > timeoutMs {
return IllegalStateExceptionErr
} else {
time.Sleep(time.Second)
}
}
}
func (taskPool *TaskPool) SafeClose() {
taskPool.retryQueue.retryQueueShutDownFlag = true
taskPool.retryQueueWait.Wait()
taskPool.taskPoolShutDownFlag = true
taskPool.taskPoolWait.Wait()
taskPool.ioWorkerWait.Wait()
}
func (taskPool *TaskPool) PushTask(task Task) error {
if err := taskPool.waitTime(); err != nil {
return err
}
taskPool.pushTask(taskPool.factory.produce(task))
return nil
}
func (taskPool *TaskPool) pushTask(task *task) {
defer taskPool.lock.Unlock()
taskPool.lock.Lock()
taskPool.queue = append(taskPool.queue, task)
}
func (taskPool *TaskPool) popTask() *task {
defer taskPool.lock.Unlock()
taskPool.lock.Lock()
task := taskPool.queue[0]
taskPool.queue = taskPool.queue[1:]
return task
}
func (taskPool *TaskPool) taskNum() int {
return 0
}
func (taskPool *TaskPool) start() {
go taskPool.retry()
for {
if len(taskPool.queue) > 0 {
select {
case taskPool.ioWorker.maxIoWorker <- struct{}{}:
taskPool.ioWorkerWait.Add(1)
go taskPool.ioWorker.DoTask(taskPool.popTask())
}
} else {
if !taskPool.taskPoolShutDownFlag {
time.Sleep(100 * time.Millisecond)
} else {
break
}
}
}
taskPool.taskPoolWait.Done()
}
func (taskPool *TaskPool) retry() {
for !taskPool.retryQueue.retryQueueShutDownFlag {
retryTaskList := taskPool.retryQueue.getRetryTaskList()
if retryTaskList == nil {
// If there is nothing to send in the retry queue, just wait for the minimum time that was given to me last time.
time.Sleep(100 * time.Millisecond)
} else {
count := len(retryTaskList)
for i := 0; i < count; i++ {
taskPool.pushTask(retryTaskList[i])
}
}
}
// send task in retry queue to send again
retryTaskList := taskPool.retryQueue.getRetryTaskList()
count := len(retryTaskList)
for i := 0; i < count; i++ {
taskPool.pushTask(retryTaskList[i])
}
taskPool.retryQueue.retryQueueWait.Done()
}
func (taskPool *TaskPool) waitTime() error {
if taskPool.MaxBlockSec > 0 {
for i := 0; i < taskPool.MaxBlockSec; i++ {
if taskPool.taskNum() > taskPool.maxTaskNum {
time.Sleep(time.Second)
} else {
return nil
}
}
return TimeoutExceptionErr
} else if taskPool.MaxBlockSec == 0 {
if taskPool.taskNum() > taskPool.maxTaskNum {
return TimeoutExceptionErr
}
} else if taskPool.MaxBlockSec < 0 {
for {
if taskPool.taskNum() > taskPool.maxTaskNum {
time.Sleep(time.Second)
} else {
return nil
}
}
}
return nil
}