/
workpool.go
89 lines (78 loc) · 1.87 KB
/
workpool.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
/*
* Copyright (C) 2019 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package workpool
import (
"context"
"golang.org/x/sync/semaphore"
"sync"
)
type WorkerPool struct {
maxWorkers int64
sem *semaphore.Weighted
tasks map[string]Task
ids []string
taskNumber int64
completeNumber int64
sync.Mutex
}
func NewWorkPool(max int64) *WorkerPool {
m := semaphore.NewWeighted(max)
ts := make(map[string]Task)
ids := make([]string, 0)
return &WorkerPool{
maxWorkers: max,
sem: m,
tasks: ts,
ids: ids,
}
}
func (wp *WorkerPool) AddTask(task Task) {
wp.taskNumber++
wp.ids = append(wp.ids, task.UUID())
wp.tasks[task.UUID()] = task
}
func (wp *WorkerPool) Top() Task {
if len(wp.ids) == 0 {
return nil
}
id := wp.ids[0]
t := wp.tasks[id]
delete(wp.tasks, id)
wp.ids = wp.ids[1:]
return t
}
func (wp *WorkerPool) Empty() bool {
return len(wp.ids) == 0
}
func (wp *WorkerPool) Poll(ctx context.Context, quit chan int) {
for !wp.Empty() {
t := wp.Top()
if err := wp.sem.Acquire(ctx, 1); err != nil {
break
}
go func() {
defer wp.sem.Release(1)
t.Run()
wp.Mutex.Lock()
wp.completeNumber++
if wp.completeNumber == wp.taskNumber {
quit <- 0
}
wp.Mutex.Unlock()
}()
}
}