-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
183 lines (154 loc) · 4.15 KB
/
Copy pathqueue.go
File metadata and controls
183 lines (154 loc) · 4.15 KB
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
package fifo
import (
"container/list"
"context"
"sync"
"sync/atomic"
)
type Queue[ID comparable, Request any, Response any] struct {
Jobs chan job[ID, Request]
jobs map[ID]job[ID, Request]
jobsLock sync.Mutex
jobsCount atomic.Int64
closures map[ID][]func(Response)
closuresLock sync.Mutex
push chan<- ID
pull <-chan ID
ctx context.Context
done context.CancelFunc
}
type job[JobID comparable, Request any] struct {
ID JobID
Request Request
}
func NewQueue[ID comparable, Request any, Response any](ctx context.Context) *Queue[ID, Request, Response] {
queue := &Queue[ID, Request, Response]{}
queue.init(ctx)
return queue
}
func (q *Queue[ID, Request, Response]) init(ctx context.Context) {
q.jobs = map[ID]job[ID, Request]{}
q.closures = map[ID][]func(Response){}
q.push, q.pull = q.newQueue()
q.ctx, q.done = context.WithCancel(ctx)
q.Jobs = make(chan job[ID, Request])
go q.pushJobsToConsumers()
}
func (q *Queue[ID, Request, Response]) Len() int64 {
return q.jobsCount.Load()
}
func (q *Queue[ID, Request, Response]) Add(jobID ID, request Request, closures ...func(Response)) (queued bool) {
q.jobsLock.Lock()
defer q.jobsLock.Unlock()
// register closure, if any
if len(closures) > 0 {
q.closuresLock.Lock()
q.closures[jobID] = append(q.closures[jobID], closures...)
q.closuresLock.Unlock()
}
// if key already queued, don't re-queue
_, ok := q.jobs[jobID]
if ok {
return false
}
// otherwise, add item to jobs
q.jobs[jobID] = job[ID, Request]{
ID: jobID,
Request: request,
}
q.push <- jobID
q.jobsCount.Add(1)
return true
}
func (q *Queue[ID, Request, Response]) AddAndCloseOnce(jobID ID, request Request, closures ...func(Response)) (queued bool) {
q.jobsLock.Lock()
defer q.jobsLock.Unlock()
// if key already queued, don't re-queue and don't register closures
_, ok := q.jobs[jobID]
if ok {
return false
}
// register closure, if any
if len(closures) > 0 {
q.closuresLock.Lock()
q.closures[jobID] = append(q.closures[jobID], closures...)
q.closuresLock.Unlock()
}
// add item to jobs
q.jobs[jobID] = job[ID, Request]{
ID: jobID,
Request: request,
}
q.push <- jobID
q.jobsCount.Add(1)
return true
}
func (q *Queue[ID, Request, Response]) get(key ID) (job job[ID, Request]) {
q.jobsLock.Lock()
job = q.jobs[key]
q.jobsLock.Unlock()
return job
}
func (q *Queue[ID, Request, Response]) Finish(jobID ID, ret Response) {
// remove item from jobs
q.jobsLock.Lock()
delete(q.jobs, jobID)
defer q.jobsLock.Unlock()
// execute closures if exist
q.closuresLock.Lock()
for _, closure := range q.closures[jobID] {
go closure(ret)
}
delete(q.closures, jobID)
q.closuresLock.Unlock()
q.jobsCount.Add(-1)
}
func (q *Queue[ID, Request, Response]) pushJobsToConsumers() {
for {
select {
case <-q.ctx.Done():
close(q.push) // will close q.manageQueue()
close(q.Jobs) // will close q.Requests consumers
return
case key := <-q.pull: // pull K from queue
q.Jobs <- q.get(key) // and push related V to consumer
}
}
}
// newQueue based on https://github.com/Symantec/Dominator/blob/master/lib/queue/dataQueue.go
func (q *Queue[ID, Request, Response]) newQueue() (chan<- ID, <-chan ID) {
push := make(chan ID, 1)
pull := make(chan ID, 1)
go q.manageQueue(push, pull)
return push, pull
}
// manageQueue, push/pull chan in front, container/list in back
func (q *Queue[ID, Request, Response]) manageQueue(push <-chan ID, pull chan<- ID) {
queue := list.New()
for {
front := queue.Front() // does queue have anything in it?
if front != nil {
select {
case pull <- front.Value.(ID): // consumer is pulling, give it K
queue.Remove(front)
case value, ok := <-push: // adding new K to queue
if ok {
queue.PushBack(value)
} else {
push = nil // push chan is closed, we'll close pull chan when queue is empty
}
}
} else {
if push == nil {
close(pull) // queue is empty & push chan is closed, we're done
return
}
value, ok := <-push // wait for new K to add to queue
if !ok {
close(pull) // push chan closed while waiting. close pull chan, we're done
return
}
queue.PushBack(value) // add this new K to queue
}
}
}