-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
87 lines (71 loc) · 1.82 KB
/
item.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
package metric
import "sync"
type SecondItem struct {
second int
items *[]*Item
itemsLocker sync.Locker
itemNewSize int
itemEndSize int
}
func (this_ *SecondItem) getAndCleanItems() (items *[]*Item) {
this_.itemsLocker.Lock()
defer this_.itemsLocker.Unlock()
items = this_.items
size := len(*items)
this_.itemNewSize -= size
this_.itemEndSize -= size
this_.items = &[]*Item{}
return
}
func (this_ *SecondItem) newItem(startTime int64) (item *Item) {
item = &Item{
secondItem: this_,
StartTime: startTime,
}
this_.itemsLocker.Lock()
defer this_.itemsLocker.Unlock()
this_.itemNewSize++
return
}
func (this_ *SecondItem) addItem(item *Item) {
this_.itemsLocker.Lock()
defer this_.itemsLocker.Unlock()
this_.itemNewSize++
this_.itemEndSize++
*this_.items = append(*this_.items, item)
return
}
func (this_ *SecondItem) endItem(item *Item) {
this_.itemsLocker.Lock()
defer this_.itemsLocker.Unlock()
this_.itemEndSize++
*this_.items = append(*this_.items, item)
return
}
type Item struct {
secondItem *SecondItem
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
Success bool `json:"success"`
Extend interface{} `json:"extend"`
UseTime int `json:"useTime"`
}
func (this_ *Item) End(useTime int, endTime int64, err error) {
this_.Success = err == nil
this_.UseTime = useTime
this_.EndTime = endTime
this_.secondItem.endItem(this_)
}
type ItemList []*Item
// Len 实现sort.Interface接口的获取元素数量方法
func (m *ItemList) Len() int {
return len(*m)
}
// Less 实现sort.Interface接口的比较元素方法
func (m *ItemList) Less(i, j int) bool {
return (*m)[i].UseTime < (*m)[j].UseTime
}
// Swap 实现sort.Interface接口的交换元素方法
func (m *ItemList) Swap(i, j int) {
(*m)[i], (*m)[j] = (*m)[j], (*m)[i]
}