-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
217 lines (187 loc) · 4.24 KB
/
hub.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package art
import (
"sync"
"sync/atomic"
)
func NewHub() *Hub {
return &Hub{
waitStop: make(chan struct{}),
}
}
type Hub struct {
collections sync.Map
concurrencyQty atomic.Int32
bucket chan struct{}
isStopped atomic.Bool
waitStop chan struct{}
}
func (hub *Hub) Join(key string, adp IAdapter) error {
if hub.isStopped.Load() {
return ErrorWrapWithMessage(ErrClosed, "art Hub Join")
}
loaded := true
for loaded {
_, loaded = hub.collections.LoadOrStore(key, adp)
if loaded {
hub.remove(key)
}
}
return nil
}
func (hub *Hub) Shutdown() {
if hub.isStopped.Swap(true) {
return
}
hub.collections.Range(func(key, value any) bool {
hub.remove(key.(string))
return true
})
close(hub.waitStop)
}
func (hub *Hub) IsShutdown() bool {
return hub.isStopped.Load()
}
func (hub *Hub) WaitShutdown() chan struct{} {
return hub.waitStop
}
func (hub *Hub) UpdateByOldKey(oldKey string, update func(IAdapter) (freshKey string)) error {
value, ok := hub.collections.LoadAndDelete(oldKey)
if !ok {
return ErrorWrapWithMessage(ErrNotFound, "key=%v not exist in hub", oldKey)
}
adp := value.(IAdapter)
freshKey := update(adp)
return hub.Join(freshKey, adp)
}
func (hub *Hub) FindByKey(key string) (adp IAdapter, found bool) {
value, ok := hub.collections.Load(key)
if ok {
return value.(IAdapter), true
}
return adp, false
}
// If filter returns true, find target
func (hub *Hub) FindOne(filter func(IAdapter) bool) (adp IAdapter, found bool) {
var target IAdapter
hub.DoSync(func(adp IAdapter) (stop bool) {
if filter(adp) {
target = adp
found = true
return true
}
return false
})
return target, found
}
// If filter returns true, find target
func (hub *Hub) FindMulti(filter func(IAdapter) bool) (all []IAdapter, found bool) {
all = make([]IAdapter, 0)
hub.DoSync(func(adp IAdapter) bool {
if filter(adp) {
all = append(all, adp)
}
return false
})
return all, len(all) > 0
}
func (hub *Hub) FindMultiByKey(keys []string) (all []IAdapter, found bool) {
all = make([]IAdapter, 0)
for i := 0; i < len(keys); i++ {
adp, ok := hub.FindByKey(keys[i])
if ok {
all = append(all, adp)
}
}
return all, len(all) > 0
}
func (hub *Hub) RemoveByKey(key string) {
hub.remove(key)
}
// If filter returns true, remove target
func (hub *Hub) RemoveOne(filter func(IAdapter) bool) {
hub.collections.Range(func(key, value any) bool {
adp := value.(IAdapter)
if filter(adp) {
hub.remove(key.(string))
return false
}
return true
})
}
// If filter returns true, remove target
func (hub *Hub) RemoveMulti(filter func(IAdapter) bool) {
hub.collections.Range(func(key, value any) bool {
adp := value.(IAdapter)
if filter(adp) {
hub.remove(key.(string))
}
return true
})
}
func (hub *Hub) RemoveMultiByKey(keys []string) {
for i := 0; i < len(keys); i++ {
hub.remove(keys[i])
}
}
func (hub *Hub) remove(key string) {
adp, loaded := hub.collections.LoadAndDelete(key)
if !loaded {
return
}
adapter := adp.(IAdapter)
if !adapter.IsStopped() {
adapter.Stop()
}
}
// DoSync
// If action returns stop=true, stops the iteration.
func (hub *Hub) DoSync(action func(IAdapter) (stop bool)) {
hub.collections.Range(func(_, value any) bool {
adp := value.(IAdapter)
stop := action(adp)
if stop {
return false
}
return true
})
}
func (hub *Hub) DoAsync(action func(IAdapter)) {
hub.collections.Range(func(_, value any) bool {
adp := value.(IAdapter)
if hub.concurrencyQty.Load() <= 0 {
go action(adp)
return true
}
hub.bucket <- struct{}{}
go func() {
defer func() {
<-hub.bucket
}()
action(adp)
}()
return true
})
}
// Count
// If filter returns true, increase count
func (hub *Hub) Count(filter func(IAdapter) bool) int {
cnt := 0
hub.DoSync(func(adp IAdapter) bool {
if filter(adp) {
cnt++
}
return false
})
return cnt
}
func (hub *Hub) Total() int {
filter := func(t IAdapter) bool { return true }
return hub.Count(filter)
}
// SetConcurrencyQty
// concurrencyQty controls how many tasks can run simultaneously,
// preventing resource usage or avoid frequent context switches.
func (hub *Hub) SetConcurrencyQty(concurrencyQty int) {
hub.concurrencyQty.Store(int32(concurrencyQty))
hub.bucket = make(chan struct{}, hub.concurrencyQty.Load())
}