-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic.go
More file actions
67 lines (60 loc) · 1.96 KB
/
topic.go
File metadata and controls
67 lines (60 loc) · 1.96 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
package fibersse
import "strings"
// topicMatch checks if a subscription pattern matches a concrete topic.
// Supports NATS-style wildcards:
//
// - `*` matches exactly one segment (between dots)
// - `>` matches one or more trailing segments (must be last token)
// - No wildcards = exact match
//
// Examples:
//
// topicMatch("notifications.*", "notifications.orders") → true
// topicMatch("notifications.*", "notifications.orders.new") → false
// topicMatch("analytics.>", "analytics.live") → true
// topicMatch("analytics.>", "analytics.live.visitors") → true
// topicMatch("analytics.>", "analytics") → false
// topicMatch("events", "events") → true
// topicMatch("events", "events.sub") → false
func topicMatch(pattern, topic string) bool {
// Fast path: no wildcards
if !strings.ContainsAny(pattern, "*>") {
return pattern == topic
}
patParts := strings.Split(pattern, ".")
topParts := strings.Split(topic, ".")
for i, pp := range patParts {
switch pp {
case ">":
// `>` must be the last token and matches 1+ remaining segments
return i < len(topParts)
case "*":
// `*` matches exactly one segment
if i >= len(topParts) {
return false
}
// Segment matched, continue
default:
// Literal match
if i >= len(topParts) || pp != topParts[i] {
return false
}
}
}
// All pattern parts consumed — topic must also be fully consumed
return len(patParts) == len(topParts)
}
// topicMatchesAny returns true if the concrete topic matches any of the patterns.
func topicMatchesAny(patterns []string, topic string) bool {
for _, p := range patterns {
if topicMatch(p, topic) {
return true
}
}
return false
}
// connMatchesTopic returns true if a connection's subscription patterns
// match the given concrete topic.
func connMatchesTopic(conn *Connection, topic string) bool {
return topicMatchesAny(conn.Topics, topic)
}