-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
emitQueue.go
160 lines (144 loc) · 2.74 KB
/
emitQueue.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
package utils
import (
"container/list"
"github.com/Sora233/DDBOT/lsp/concern_type"
"go.uber.org/atomic"
"sync"
"time"
)
type EmitE struct {
Id interface{}
Type concern_type.Type
}
func NewEmitE(id interface{}, t concern_type.Type) *EmitE {
return &EmitE{
Id: id,
Type: t,
}
}
type EmitQueue struct {
TimeInterval time.Duration
stopped atomic.Bool
stop chan interface{}
eqlist *list.List
eqlistCur *list.Element
emitChan chan<- *EmitE
waitTimer *time.Timer
cond *sync.Cond
wg sync.WaitGroup
}
func (q *EmitQueue) Add(e *EmitE) {
if e == nil {
return
}
q.cond.L.Lock()
defer q.cond.L.Unlock()
if q.eqlist.Len() == 0 {
q.eqlist.PushBack(e)
} else {
var found = false
for head := q.eqlist.Front(); head != nil; head = head.Next() {
if headE := head.Value.(*EmitE); headE.Id == e.Id {
headE.Type = headE.Type.Add(e.Type)
found = true
break
}
}
if !found {
q.eqlist.PushBack(e)
}
}
if q.eqlist.Len() == 1 {
q.eqlistCur = q.eqlist.Front()
q.cond.Signal()
}
}
func (q *EmitQueue) Update(e *EmitE) {
if e == nil {
return
}
q.cond.L.Lock()
defer q.cond.L.Unlock()
for head := q.eqlist.Front(); head != nil; head = head.Next() {
headE := head.Value.(*EmitE)
if headE.Id != e.Id {
continue
}
headE.Type = e.Type
}
}
func (q *EmitQueue) Delete(id interface{}) {
if id == nil {
return
}
q.cond.L.Lock()
defer q.cond.L.Unlock()
for head := q.eqlist.Front(); head != nil; head = head.Next() {
headE := head.Value.(*EmitE)
if headE.Id != id {
continue
}
if q.eqlistCur == head {
q.eqlistCur = q.eqlistCur.Next()
}
q.eqlist.Remove(head)
}
if q.eqlist.Len() == 0 {
q.eqlistCur = nil
}
}
func (q *EmitQueue) core() {
q.wg.Add(1)
defer q.wg.Done()
for {
q.cond.L.Lock()
for q.eqlist.Len() == 0 && !q.stopped.Load() {
q.cond.Wait()
}
q.cond.L.Unlock()
select {
case <-q.waitTimer.C:
case <-q.stop:
return
}
q.cond.L.Lock()
if q.eqlist.Len() > 0 {
if q.eqlistCur == nil {
q.eqlistCur = q.eqlist.Front()
}
headE := q.eqlistCur.Value.(*EmitE)
q.eqlistCur = q.eqlistCur.Next()
q.cond.L.Unlock()
select {
case q.emitChan <- headE:
case <-q.stop:
return
}
} else {
q.cond.L.Unlock()
}
q.waitTimer.Reset(q.TimeInterval)
}
}
func (q *EmitQueue) Start() {
go q.core()
}
func (q *EmitQueue) Stop() {
q.stopped.Store(true)
if q.stop != nil {
close(q.stop)
}
q.cond.Broadcast()
q.wg.Wait()
}
func NewEmitQueue(c chan<- *EmitE, interval time.Duration) *EmitQueue {
q := &EmitQueue{
stop: make(chan interface{}),
cond: sync.NewCond(new(sync.Mutex)),
eqlist: list.New(),
emitChan: c,
waitTimer: time.NewTimer(interval),
TimeInterval: interval,
}
return q
}