-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtask_list.go
100 lines (77 loc) · 2.14 KB
/
subtask_list.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
package collector
import (
"sync"
"time"
"github.com/golang/glog"
"github.com/danenmao/pterergate-dtf/dtf/taskmodel"
)
type SubtaskElem struct {
Result *taskmodel.SubtaskResult
InsertTime time.Time
}
const (
MaxSubtaskElemCount = 10000
MaxCountPerTime = 10
)
var s_SubtaskElemList = []*SubtaskElem{}
var s_SubtaskLock sync.RWMutex
func InsertSubtask(result *taskmodel.SubtaskResult) {
s_SubtaskLock.Lock()
defer s_SubtaskLock.Unlock()
// check if exceed the max length
if len(s_SubtaskElemList) >= MaxSubtaskElemCount {
glog.Info("the length of subtask list exceeded the max count")
return
}
// append to the list
subtask := SubtaskElem{Result: result, InsertTime: time.Now()}
s_SubtaskElemList = append(s_SubtaskElemList, &subtask)
}
func InsertSubtaskResults(results []taskmodel.SubtaskResult) {
s_SubtaskLock.Lock()
defer s_SubtaskLock.Unlock()
cap := MaxSubtaskElemCount - len(s_SubtaskElemList)
count := len(results)
if count > cap {
count = cap
}
if count <= 0 {
glog.Info("the length of subtask list exceeded the max count")
return
}
for _, result := range results[0:count] {
subtask := SubtaskElem{Result: &result, InsertTime: time.Now()}
s_SubtaskElemList = append(s_SubtaskElemList, &subtask)
}
}
func InsertSubtaskList(newElems *[]*SubtaskElem) {
s_SubtaskLock.Lock()
defer s_SubtaskLock.Unlock()
if len(s_SubtaskElemList)+len(*newElems) >= MaxSubtaskElemCount {
glog.Warning("too many elements in the list")
return
}
s_SubtaskElemList = append(s_SubtaskElemList, *newElems...)
}
func PopSubtaskList(retList *[]*SubtaskElem) {
s_SubtaskLock.Lock()
defer s_SubtaskLock.Unlock()
totalCount := uint(len(s_SubtaskElemList))
if totalCount <= 0 {
return
}
glog.Info("subtask elem count in list: ", totalCount)
// get the element count
count := 1
if totalCount > uint(gs_RoutineLimit.UpperLimit) {
count = int(totalCount/uint(gs_RoutineLimit.UpperLimit)) + 1
}
// MaxCountPerTime
if count > MaxCountPerTime {
count = MaxCountPerTime
}
getList := s_SubtaskElemList[0:count]
*retList = append(*retList, getList...)
// remove the elements
s_SubtaskElemList = s_SubtaskElemList[count:]
}