-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
150 lines (132 loc) · 3.37 KB
/
main.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
package lq
import (
"sync"
)
const ChanSize = 1000
type IWaitGroup interface {
Add(i int)
Done()
Wait()
}
type IListQueue[T any] interface {
Add(...T)
Each() chan T
Close()
Wait()
}
// NewListQueue returns a queue which ensures exactly once, in order delivery of every past and future event to every
// subscriber.
//
// q := listQueue.NewListQueue[int]()
// q.Add(1, 2)
// ch := q.Each()
//
// q.Each() will return a channel which can be used to iterate over past and future arguments added with Add(...).
// For each item returned from this channel. The queue needs to be closed after all items have been added with Add(...)
// for loops using q.Each() to exit.
//
// go func() {
// for v := range ch {
// // Prints: 1
// // 2
// fmt.Println(v)
// q.Done()
// }
//
// Loops created after Close() is called will still work as expected, but new values can't be added with Add(...).
//
// q.Add(3, 4)
// q.Close()
// for v := range q.Each() {
// // Prints: 1
// // 2
// // 3
// // 4
// fmt.Println(v)
// q.Done()
// }
// }()
//
// // Waits for all events to be delivered to all channels
// q.Wait()
//
func NewListQueue[T any](items ...T) *ListQueue[T] {
wg := &sync.WaitGroup{}
wg.Add(1)
return &ListQueue[T]{
items: items,
keys: map[string]T{},
mKeys: &sync.RWMutex{},
m: &sync.RWMutex{},
Wg: wg,
}
}
type ListQueue[T any] struct {
items []T
keys map[string]T
mKeys *sync.RWMutex
m *sync.RWMutex
Wg IWaitGroup
listeners []chan T
}
// Each returns a channel which will be sent all current and future items passed to Add in the order they were added.
func (lq *ListQueue[T]) Each() chan T {
lq.m.RLock()
// Grab the number of items at the moment to ensure we don't double process messages in the case we finish iterating
// the backlog between the time the backlog updates and items start getting fed to the listeners in s.Add.
numItems := len(lq.items)
lq.Wg.Add(numItems)
// Create a listener right early, so we don't miss any messages added when we are processing the backlog.
writer := make(chan T, numItems+ChanSize)
lq.listeners = append(lq.listeners, writer)
lq.m.RUnlock()
reader := make(chan T, 0)
go func() {
for i := range writer {
reader <- i
lq.Wg.Done()
}
}()
go func() {
for i := 0; i < numItems; i++ {
writer <- lq.items[i]
}
}()
return reader
}
// Add adds all passsed arguments to the list and wakes up any paused goroutines to continue reading from the list.
func (lq *ListQueue[T]) Add(items ...T) {
lq.m.Lock()
work := len(lq.listeners) * len(items)
lq.Wg.Add(work)
lq.items = append(lq.items, items...)
lq.m.Unlock()
for _, item := range items {
for _, l := range lq.listeners {
l <- item
}
}
}
func (lq *ListQueue[T]) Wait() {
lq.Wg.Done()
// Wait for backlogs to be copied to listeners.
lq.Wg.Wait()
for _, l := range lq.listeners {
close(l)
}
}
// Add takes a key and value, if the key is unique it will add the value to the queue.
func (lq *ListQueue[T]) AddUnique(key string, value T) bool {
lq.mKeys.RLock()
_, ok := lq.keys[key]
lq.mKeys.RUnlock()
if !ok {
lq.mKeys.Lock()
lq.keys[key] = value
lq.mKeys.Unlock()
lq.Add(value)
return true
} else {
return false
}
}