-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
subscriberlist.go
116 lines (89 loc) · 2.18 KB
/
subscriberlist.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
package mercure
import (
"sort"
"strings"
"github.com/kevburnsjr/skipfilter"
)
type SubscriberList struct {
skipfilter *skipfilter.SkipFilter
}
// We choose a delimiter and an escape character which are unlikely to be used.
const (
escape = '\x00'
delim = '\x01'
)
//nolint:gochecknoglobals
var replacer = strings.NewReplacer(
string(escape), string([]rune{escape, escape}),
string(delim), string([]rune{escape, delim}),
)
func NewSubscriberList(size int) *SubscriberList {
return &SubscriberList{
skipfilter: skipfilter.New(func(s interface{}, filter interface{}) bool {
return s.(*LocalSubscriber).MatchTopics(decode(filter.(string)))
}, size),
}
}
func encode(topics []string, private bool) string {
sort.Strings(topics)
parts := make([]string, len(topics)+1)
if private {
parts[0] = "1"
} else {
parts[0] = "0"
}
for i, t := range topics {
parts[i+1] = replacer.Replace(t)
}
return strings.Join(parts, string(delim))
}
func decode(f string) (topics []string, private bool) {
var (
privateExtracted, inEscape bool
builder strings.Builder
)
for _, char := range f {
if inEscape {
builder.WriteRune(char)
inEscape = false
continue
}
switch char {
case escape:
inEscape = true
case delim:
if !privateExtracted {
private = builder.String() == "1"
builder.Reset()
privateExtracted = true
break
}
topics = append(topics, builder.String())
builder.Reset()
default:
builder.WriteRune(char)
}
}
topics = append(topics, builder.String())
return topics, private
}
func (sl *SubscriberList) MatchAny(u *Update) (res []*LocalSubscriber) {
for _, m := range sl.skipfilter.MatchAny(encode(u.Topics, u.Private)) {
res = append(res, m.(*LocalSubscriber))
}
return
}
func (sl *SubscriberList) Walk(start uint64, callback func(s *LocalSubscriber) bool) uint64 {
return sl.skipfilter.Walk(start, func(val interface{}) bool {
return callback(val.(*LocalSubscriber))
})
}
func (sl *SubscriberList) Add(s *LocalSubscriber) {
sl.skipfilter.Add(s)
}
func (sl *SubscriberList) Remove(s *LocalSubscriber) {
sl.skipfilter.Remove(s)
}
func (sl *SubscriberList) Len() int {
return sl.skipfilter.Len()
}