-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublist.go
772 lines (667 loc) · 15.4 KB
/
sublist.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
Copyright 2024 eventbus Author(s)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package eventbus
import (
"errors"
"hash/maphash"
"sync"
"sync/atomic"
)
const (
TSep = "."
BtSep = '.'
Pwc = "*"
BPwc = '*'
Fwc = ">"
BFwc = '>'
)
const (
PListCacheMin = 128 // build a quick cache when the number of subscriptions is greater than this value
StackCacheSize = 32 // normal stack cache size
)
var sublistResultPool = NewPool(
func() *sublistResult {
return &sublistResult{}
},
func(r *sublistResult) bool {
clear(r.psubs)
r.psubs = r.psubs[:0]
clear(r.qsubs)
r.qsubs = r.qsubs[:0]
return true
},
)
type sublistResult struct {
psubs []*Subscription
qsubs [][]*Subscription
}
var emptyResult = &sublistResult{}
type Snmp struct {
Matches atomic.Uint64
Count atomic.Uint64
Inserts atomic.Uint64
Removes atomic.Uint64
}
// AddTo adds the snmp to the to snmp.
func (s *Snmp) AddTo(to *Snmp) {
to.Matches.Add(s.Matches.Load())
to.Count.Add(s.Count.Load())
to.Inserts.Add(s.Inserts.Load())
to.Removes.Add(s.Removes.Load())
}
type Sublist struct {
lock sync.RWMutex
Snmp
genId atomic.Uint64 // sublist generation id. changed on insert/remove
root *Level
}
// NewBus
func NewBus() *Sublist {
return &Sublist{
root: NewLevel(),
}
}
type Subscription struct {
subject string // topic
status atomic.Int32
sub ICall
subsOption
}
// NewSubs
func NewSubs(subject string, call ICall, opts ...Opt) *Subscription {
var subs = Subscription{
subject: subject,
sub: call,
}
subs.apply(opts...)
return &subs
}
// Subject
func (s *Subscription) Subject() string {
if s == nil {
return ""
}
return s.subject
}
// canCall returns true if the subscribe can be called
func (s *Subscription) canCall() bool {
// if the subscribe is done, return false
// if the subscribe is once and executed, return false
if s.isDone() {
return false
}
if !s.isOnce() {
return true
}
return s.status.CompareAndSwap(
statusInit, statusExecuted,
)
}
// stop stops the subscribe
func (s *Subscription) stop() {
s.status.Store(statusClosed)
}
// isDone returns true if the subscribe is closed
func (s *Subscription) isDone() bool {
return s.status.Load() == statusClosed
}
// split splits the subject into tokens.
func (s *Subscription) split(cache []string) ([]string, bool) {
subject := s.Subject()
return SplitSubject(subject, cache)
}
// call
func (s *Subscription) call(param any, reply replyFunc) bool {
if !s.canCall() {
return false
}
return s.sub.run(param, reply)
}
// SplitSubject splits the subject into tokens.
func SplitSubject(subject string, cache []string) ([]string, bool) {
var start int
for i := 0; i < len(subject); i++ {
if subject[i] == BtSep {
if start == i {
return nil, false
}
cache = append(cache, subject[start:i])
start = i + 1
}
}
return append(cache, subject[start:]), start < len(subject)
}
// ValidSubject returns true if the subject is valid.
func ValidSubject(subject string) bool {
var start int
for i := 0; i < len(subject); i++ {
if subject[i] == BtSep {
if start == i {
return false
}
start = i + 1
}
}
return start < len(subject)
}
// TotalNodes returns the total number of nodes.
func (s *Sublist) TotalNodes() int {
if s == nil {
return 0
}
s.lock.RLock()
defer s.lock.RUnlock()
return s.root.TotalNodes()
}
// SnmpInfo
func (s *Sublist) SnmpInfo() *Snmp {
var ret Snmp
ret.Count.Store(s.Count.Load())
ret.Inserts.Store(s.Inserts.Load())
ret.Removes.Store(s.Removes.Load())
ret.Matches.Store(s.Matches.Load())
return &ret
}
// Subscribe adds the subscription into the sublist.
func (s *Sublist) Subscribe(sub *Subscription) error {
if s == nil {
return ErrSublistNil
}
var cache [StackCacheSize]string
tokens, valid := sub.split(cache[:0])
if !valid {
return ErrInvalidSubject
}
var n *Node // the node to insert the subscription into
var hasFwc bool // whether the full wildcard is found
s.lock.Lock()
defer s.lock.Unlock()
level := s.root
for _, token := range tokens {
lt := len(token)
if hasFwc {
// the full wildcard is not allowed to be followed by other tokens
return ErrInvalidSubject
}
if lt > 1 {
// this is normal token
n = level.Nodes[token]
} else {
switch token[0] {
case BPwc: // *
n = level.Pwc
case BFwc: // >
n = level.Fwc
hasFwc = true
default:
n = level.Nodes[token]
}
}
if n == nil {
n = NewNode()
if lt > 1 {
// this is normal token
level.Nodes[token] = n
} else {
switch token[0] {
case BPwc:
level.Pwc = n
case BFwc:
level.Fwc = n
default:
level.Nodes[token] = n
}
}
}
if n.Next == nil {
n.Next = NewLevel()
}
level = n.Next
}
if !sub.isQueue() {
n.Psubs.Add(sub)
if n.Plist != nil {
// when the number of subscriptions is greater than the minimum quick cache value,
// add the subscription to the quick cache
n.Plist = append(n.Plist, sub)
} else if n.Psubs.Len() > PListCacheMin {
// build a quick cache when the number of subscriptions
// is greater than the minimum quick cache value
n.Plist = n.Psubs.AppendToSlice(nil)
}
} else {
if n.Qsubs == nil {
n.Qsubs = make(map[string]ISet[*Subscription])
}
queueName := sub.queue
// add the subscription to the queue set
subs, ok := n.Qsubs[queueName]
if !ok {
subs = NewMixSet[*Subscription]()
n.Qsubs[queueName] = subs
}
subs.Add(sub)
}
s.Count.Add(1)
s.Inserts.Add(1)
s.genId.Add(1)
return nil
}
// removeFromNodeInLock removes the subscription from the node.
// returns found and last
// found: true if the subscription is found in the node
// last: true if remove the last subscription in the subscription list
func (s *Sublist) removeFromNodeInLock(n *Node, sub *Subscription) (found, last bool) {
if n == nil {
return false, true
}
if !sub.isQueue() {
// process the normal subscriptions
found = n.Psubs.Contains(sub)
if !found {
return
}
n.Psubs.Remove(sub)
if n.Plist != nil {
n.Plist = nil
}
last = n.Psubs.Len() == 0
return
} else {
// process the queue subscriptions
queueSubs, ok := n.Qsubs[sub.queue]
if !ok {
return
}
found = queueSubs.Contains(sub)
if found {
queueSubs.Remove(sub)
}
last = queueSubs.Len() == 0
if last {
delete(n.Qsubs, sub.queue)
}
}
return
}
// Publish
func (s *Sublist) Publish(subject string, param any) (err error) {
return s.Request(subject, param, nil)
}
// Request
func (s *Sublist) Request(subject string, param any, reply IReply) (err error) {
if s == nil {
return ErrSublistNil
}
if !ValidSubject(subject) {
return ErrInvalidSubject
}
var slowConsumeCount int
ret := s.match(subject)
var removeOnces = ret.psubs[:0]
var r replyFunc
if reply != nil {
r = reply.reply
}
var call = func(sub *Subscription, param any, reply replyFunc) bool {
success := sub.call(param, reply)
isOnce := sub.isOnce()
if success && isOnce {
removeOnces = append(removeOnces, sub)
} else if !success && !isOnce {
if !sub.isDone() {
// only in channel mode
slowConsumeCount++
}
}
return success
}
for _, sub := range ret.psubs {
call(sub, param, r)
}
// process the queue subscriptions
for _, subs := range ret.qsubs {
length := len(subs)
if length == 0 {
continue
}
// maybe should shuffle the queue subscriptions?
// in the queue mode, only one subscription is executed
var start = randIdx(length)
for i := range len(subs) {
if call(subs[(start+i)%length], param, r) {
break
}
}
}
if len(removeOnces) > 0 {
s.UnsubscribeBatch(removeOnces)
}
sublistResultPool.Put(ret)
if slowConsumeCount > 0 {
err = slowConsumerErr{count: slowConsumeCount}
}
return
}
func (s *Sublist) Unsubscribe(sub *Subscription) error {
if s == nil {
return ErrSublistNil
}
return s.remove(sub, true, true)
}
// UnsubscribeBatch
func (s *Sublist) UnsubscribeBatch(subs []*Subscription) error {
if s == nil {
return ErrSublistNil
}
if len(subs) == 0 {
return nil
}
if len(subs) == 1 {
return s.Unsubscribe(subs[0])
}
// split the subscriptions into batches
var errorCache [StackCacheSize]error
var allErrors = errorCache[:0]
var validSubs = subs[:0]
for _, sub := range subs {
if ValidSubject(sub.Subject()) {
validSubs = append(validSubs, sub)
} else {
allErrors = append(allErrors, &subjectError{
subject: sub.Subject(),
err: ErrInvalidSubject,
})
}
}
if len(validSubs) == 0 {
return errors.Join(allErrors...)
}
subs = validSubs
// avoid too many locks
const BatchNum = 32
var batchCache [StackCacheSize / 2][]*Subscription
var batch = batchCache[:0]
for len(subs) > BatchNum {
batch = append(batch, subs[:BatchNum])
subs = subs[BatchNum:]
}
if len(subs) > 0 {
batch = append(batch, subs)
}
for _, subs := range batch {
var errCnt int
s.lock.Lock()
for _, sub := range subs {
if err := s.remove(sub, false, false); err != nil {
allErrors = append(allErrors, &subjectError{
subject: sub.Subject(),
err: err,
})
errCnt++
}
}
if errCnt != len(subs) {
// maybe some subscriptions are not found
// but some subscriptions are removed successfully
// so we need to update the generation id
s.genId.Add(1)
}
s.lock.Unlock()
}
return errors.Join(allErrors...)
}
// remove removes the subscription from the sublist.
func (s *Sublist) remove(sub *Subscription, lock bool, updateGen bool) error {
var cache [StackCacheSize]string
tokens, valid := sub.split(cache[:0])
if !valid {
return ErrInvalidSubject
}
var n *Node
var hasFwc bool
if lock {
s.lock.Lock()
defer s.lock.Unlock()
}
level := s.root
var levelCache [StackCacheSize]LevelCache
var levels = levelCache[:0]
for _, token := range tokens {
lt := len(token)
if hasFwc {
// the full wildcard is not allowed to be followed by other tokens
return ErrInvalidSubject
}
if level == nil {
// the level is nil, the subscription is not found
return ErrNotFound
}
if lt > 1 {
n = level.Nodes[token]
} else {
switch token[0] {
case BPwc:
n = level.Pwc
case BFwc:
n = level.Fwc
hasFwc = true
default:
n = level.Nodes[token]
}
}
if n != nil {
levels = append(levels, LevelCache{
Level: level,
Node: n,
Token: token,
})
level = n.Next
} else {
level = nil
}
}
removed, _ := s.removeFromNodeInLock(n, sub)
if !removed {
return ErrNotFound
}
sub.stop()
s.Count.Add(^uint64(0))
s.Removes.Add(1)
if updateGen {
s.genId.Add(1)
}
for idx := len(levels) - 1; idx >= 0; idx-- {
lv, node, token := levels[idx].Level, levels[idx].Node, levels[idx].Token
if node.IsEmpty() {
lv.PruneNode(node, token)
}
}
return nil
}
// match returns a list of subscriptions that match the subject.
func (s *Sublist) match(subject string) *sublistResult {
if s == nil {
return emptyResult
}
var cache [StackCacheSize]string
tokens, valid := SplitSubject(subject, cache[:0])
if !valid {
return emptyResult
}
s.Matches.Add(1)
var ret = sublistResultPool.Get()
s.lock.RLock()
defer s.lock.RUnlock()
MatchLevel(s.root, tokens, ret)
return ret
}
const (
insertNewPos = -1
)
// findInsertQueueIndex finds the index to insert the subscription.
func (s *sublistResult) findInsertQueueIndex(queueName string) int {
if queueName == "" {
return insertNewPos
}
for idx, subs := range s.qsubs {
if len(subs) == 0 {
continue
}
if subs[0].queue == queueName {
return idx
}
}
return insertNewPos
}
// add adds the subscription to the result.
func (s *sublistResult) add(node *Node) {
if node.Plist != nil {
s.psubs = append(s.psubs, node.Plist...)
} else {
s.psubs = node.Psubs.AppendToSlice(s.psubs)
}
// process the queue subscriptions
for queueName, subs := range node.Qsubs {
if subs.Len() == 0 {
continue
}
// TODO queue weight?
if idx := s.findInsertQueueIndex(queueName); idx == insertNewPos {
s.qsubs = append(s.qsubs, subs.AppendToSlice(nil))
} else {
s.qsubs[idx] = subs.AppendToSlice(s.qsubs[idx])
}
}
}
// MultiSublist is a multi-sublist.
// NOT-SUPPORT WILD SUBSCRIPTION.
type MultiSublist struct {
sublists []*Sublist
seed maphash.Seed
}
func NewMultiBus(length uint) *MultiSublist {
if length < 1 {
panic("length must be greater than 0")
}
lists := make([]*Sublist, length)
for i := range lists {
lists[i] = NewBus()
}
return &MultiSublist{
sublists: lists,
seed: maphash.MakeSeed(),
}
}
func (m *MultiSublist) IsSubjectValid(subject string) (isWild bool, valid bool) {
var cache [StackCacheSize]string
var token []string
token, valid = SplitSubject(subject, cache[:0])
if !valid {
return
}
for _, t := range token {
if t == Pwc || t == Fwc {
isWild = true
return
}
}
return
}
// TotalNodes returns the total number of nodes.
func (m *MultiSublist) TotalNodes() int {
if m == nil {
return 0
}
var total int
for _, sublist := range m.sublists {
total += sublist.TotalNodes()
}
return total
}
// SnmpInfo
func (m *MultiSublist) SnmpInfo() *Snmp {
var snmp Snmp
for _, sublist := range m.sublists {
sublist.AddTo(&snmp)
}
return &snmp
}
// getSublist use map hash to get the sublist by subject.
func (m *MultiSublist) getSublist(subject string) *Sublist {
if m == nil {
return nil
}
hash := maphash.String(m.seed, subject)
return m.sublists[hash%uint64(len(m.sublists))]
}
// Publish
func (m *MultiSublist) Publish(subject string, param any) error {
return m.getSublist(subject).Publish(subject, param)
}
// Request
func (m *MultiSublist) Request(subject string, param any, reply IReply) error {
return m.getSublist(subject).Request(subject, param, reply)
}
// Subscribe
func (m *MultiSublist) Subscribe(sub *Subscription) error {
if isWild, valid := m.IsSubjectValid(sub.Subject()); !valid {
return ErrInvalidSubject
} else if isWild {
return ErrNotSupport
}
return m.getSublist(sub.Subject()).Subscribe(sub)
}
// Unsubscribe
func (m *MultiSublist) Unsubscribe(subs *Subscription) error {
return m.getSublist(subs.Subject()).Unsubscribe(subs)
}
// UnsubscribeBatch
func (m *MultiSublist) UnsubscribeBatch(subs []*Subscription) error {
if len(subs) < 1 {
return nil
} else if len(subs) == 1 {
return m.Unsubscribe(subs[0])
} else {
var sublistSet = make(map[*Sublist][]*Subscription, InitNodeSubCache)
for _, sub := range subs {
if !ValidSubject(sub.Subject()) {
continue
}
sublist := m.getSublist(sub.Subject())
sublistSet[sublist] = append(sublistSet[sublist], sub)
}
var all []error
for sublist, subs := range sublistSet {
if err := sublist.UnsubscribeBatch(subs); err != nil {
all = append(all, err)
}
}
return errors.Join(all...)
}
}
// Default is the default multi-sublist.
var Default = NewMultiBus(32)
type ISublist interface {
Subscribe(sub *Subscription) error
Publish(subject string, param any) error
Request(subject string, param any, reply IReply) error
Unsubscribe(sub *Subscription) error
UnsubscribeBatch(subs []*Subscription) error
SnmpInfo() *Snmp
TotalNodes() int
}
var (
_ ISublist = (*Sublist)(nil)
_ ISublist = (*MultiSublist)(nil)
)