-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.go
More file actions
184 lines (140 loc) · 3.1 KB
/
queue.go
File metadata and controls
184 lines (140 loc) · 3.1 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
184
package workqueue
import (
"sync"
"sync/atomic"
lst "github.com/shengyanli1982/workqueue/v2/internal/container/list"
)
// queueImpl 是基础队列实现,支持可选幂等语义。
type queueImpl struct {
closed atomic.Bool
once sync.Once
lock sync.Mutex
config *QueueConfig
list container
elementpool *lst.NodePool
processing Set
dirty Set
}
// NewQueue 创建基础队列。
func NewQueue(config *QueueConfig) Queue {
return newQueue(&wrapInternalList{List: lst.New()}, lst.NewNodePool(), config)
}
func newQueue(list container, elementpool *lst.NodePool, config *QueueConfig) *queueImpl {
q := &queueImpl{
config: isQueueConfigEffective(config),
list: list,
elementpool: elementpool,
}
if q.config.idempotent {
q.processing = q.config.setCreator()
q.dirty = q.config.setCreator()
}
return q
}
func (q *queueImpl) Shutdown() {
q.once.Do(func() {
q.closed.Store(true)
q.lock.Lock()
q.list.Range(func(value interface{}) bool {
q.elementpool.Put(value.(*lst.Node))
return true
})
q.list.Cleanup()
if q.config.idempotent {
q.processing.Cleanup()
q.dirty.Cleanup()
}
q.lock.Unlock()
})
}
func (q *queueImpl) IsClosed() bool {
return q.closed.Load()
}
func (q *queueImpl) Len() (count int) {
q.lock.Lock()
count = int(q.list.Len())
q.lock.Unlock()
return
}
func (q *queueImpl) Values() []interface{} {
q.lock.Lock()
items := q.list.Slice()
q.lock.Unlock()
return items
}
func (q *queueImpl) Range(fn func(interface{}) bool) {
if fn == nil {
return
}
q.lock.Lock()
q.list.Range(func(value interface{}) bool {
node := value.(*lst.Node)
return fn(node.Value)
})
q.lock.Unlock()
}
func (q *queueImpl) Put(value interface{}) error {
if q.IsClosed() {
return ErrQueueIsClosed
}
if value == nil {
return ErrElementIsNil
}
if q.config.idempotent {
// 幂等模式先判重再分配节点,减少重复入队时的对象池开销。
q.lock.Lock()
if q.dirty.Contains(value) || q.processing.Contains(value) {
q.lock.Unlock()
return ErrElementAlreadyExist
}
last := q.elementpool.Get()
last.Value = value
q.list.Push(last)
q.dirty.Add(value)
q.lock.Unlock()
} else {
// 非幂等模式在锁外申请节点,缩短临界区。
last := q.elementpool.Get()
last.Value = value
q.lock.Lock()
q.list.Push(last)
q.lock.Unlock()
}
q.config.callback.OnPut(value)
return nil
}
func (q *queueImpl) Get() (interface{}, error) {
if q.IsClosed() {
return nil, ErrQueueIsClosed
}
q.lock.Lock()
if q.list.Len() == 0 {
q.lock.Unlock()
return nil, ErrQueueIsEmpty
}
front := q.list.Pop().(*lst.Node)
value := front.Value
if q.config.idempotent {
q.processing.Add(value)
q.dirty.Remove(value)
}
q.lock.Unlock()
q.elementpool.Put(front)
q.config.callback.OnGet(value)
return value, nil
}
func (q *queueImpl) Done(value interface{}) {
if q.IsClosed() {
return
}
if q.config.idempotent {
q.lock.Lock()
if !q.processing.Contains(value) {
q.lock.Unlock()
return
}
q.processing.Remove(value)
q.lock.Unlock()
q.config.callback.OnDone(value)
}
}