-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathsubscriberlist.go
More file actions
119 lines (91 loc) · 2.36 KB
/
subscriberlist.go
File metadata and controls
119 lines (91 loc) · 2.36 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
package mercure
import (
"sort"
"strings"
"github.com/dunglas/skipfilter"
)
type SubscriberList struct {
skipfilter *skipfilter.SkipFilter[*LocalSubscriber, string]
}
// 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}),
)
// DefaultSubscriberListCacheSize is the default size of the skipfilter cache.
//
// Let's say update topics take 100 bytes on average, a cache with
// 100,000 entries will use about 10MB.
const DefaultSubscriberListCacheSize = 100_000
func NewSubscriberList(cacheSize int) *SubscriberList {
return &SubscriberList{
skipfilter: skipfilter.New[*LocalSubscriber, string](func(s *LocalSubscriber, filter string) bool {
return s.MatchTopics(decode(filter))
}, cacheSize),
}
}
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) []*LocalSubscriber {
return sl.skipfilter.MatchAny(encode(u.Topics, u.Private))
}
func (sl *SubscriberList) Walk(start uint64, callback func(s *LocalSubscriber) bool) uint64 {
return sl.skipfilter.Walk(start, func(val *LocalSubscriber) bool {
return callback(val)
})
}
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()
}