-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.go
61 lines (56 loc) · 1.72 KB
/
pattern.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
package mpx
import (
"github.com/RedisMPX/go-mpx/internal/list"
)
// A PatternSubscription ties a OnMessageFunc to one Redis Pub/Sub pattern through
// a single multiplexed connection. Use NewPatternSubscription from Multiplexer to create a
// new PatternSubscription.
// Before disposing of a PatternSubscription you must call Close on it.
// PatternSubscription instances are not safe for concurrent use.
//
// For more information about the pattern syntax: https://redis.io/topics/pubsub#pattern-matching-subscriptions
type PatternSubscription struct {
mpx *Multiplexer
pattern string
onMessage OnMessageFunc
onDisconnect OnDisconnectFunc
onActivation OnActivationFunc
onDisconnectNode *list.Element
onMessageNode *list.Element
closed bool
}
func createPatternSubscription(
mpx *Multiplexer,
pattern string,
onMessage OnMessageFunc,
onDisconnect OnDisconnectFunc,
onActivation OnActivationFunc,
) *list.Element {
onDisconnectNode := list.NewElement(nil)
patSub := PatternSubscription{
mpx,
pattern,
onMessage,
onDisconnect,
onActivation,
onDisconnectNode,
nil,
false,
}
patSub.onMessageNode = list.NewElement(&patSub)
onDisconnectNode.Value = &patSub
return onDisconnectNode
}
// Returns the pattern that this PatternSubscription is subscribed to.
func (p PatternSubscription) GetPattern() string {
return p.pattern
}
// Closes the PatternSubscription and frees all allocated resources.
// You don't need to call Close if you're also disposing of the whole Multiplexer.
func (p *PatternSubscription) Close() {
if p.closed {
panic("tried to use a closed PatternSubscription")
}
p.closed = true
p.mpx.reqCh <- request{patternClose, "", p.onDisconnectNode}
}