-
Notifications
You must be signed in to change notification settings - Fork 88
/
broker.go
165 lines (144 loc) · 4.14 KB
/
broker.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
package turnpike
import "sync"
// Broker is the interface implemented by an object that handles routing EVENTS
// from Publishers to Subscribers.
type Broker interface {
// Publishes a message to all Subscribers.
Publish(*Session, *Publish)
// Subscribes to messages on a URI.
Subscribe(*Session, *Subscribe)
// Unsubscribes from messages on a URI.
Unsubscribe(*Session, *Unsubscribe)
// Removes all subscriptions of the subscriber.
RemoveSession(*Session)
}
// A super simple broker that matches URIs to Subscribers.
type defaultBroker struct {
routes map[URI]map[ID]*Session
subscriptions map[ID]URI
sessions map[*Session]map[ID]struct{}
lock sync.RWMutex
}
// NewDefaultBroker initializes and returns a simple broker that matches URIs to
// Subscribers.
func NewDefaultBroker() Broker {
return &defaultBroker{
routes: make(map[URI]map[ID]*Session),
subscriptions: make(map[ID]URI),
sessions: make(map[*Session]map[ID]struct{}),
}
}
// Publish sends a message to all subscribed clients except for the sender.
//
// If msg.Options["acknowledge"] == true, the publisher receives a Published event
// after the message has been sent to all subscribers.
func (br *defaultBroker) Publish(pub *Session, msg *Publish) {
pubID := NewID()
evtTemplate := Event{
Publication: pubID,
Arguments: msg.Arguments,
ArgumentsKw: msg.ArgumentsKw,
Details: make(map[string]interface{}),
}
excludePublisher := true
if exclude, ok := msg.Options["exclude_me"].(bool); ok {
excludePublisher = exclude
}
br.lock.RLock()
for id, sub := range br.routes[msg.Topic] {
// don't send event to publisher
if sub == pub && excludePublisher {
continue
}
// shallow-copy the template
event := evtTemplate
event.Subscription = id
sub.Send(&event)
}
br.lock.RUnlock()
// only send published message if acknowledge is present and set to true
if doPub, _ := msg.Options["acknowledge"].(bool); doPub {
pub.Send(&Published{Request: msg.Request, Publication: pubID})
}
}
// Subscribe subscribes the client to the given topic.
func (br *defaultBroker) Subscribe(sub *Session, msg *Subscribe) {
id := NewID()
br.lock.Lock()
r, ok := br.routes[msg.Topic]
if !ok {
r = make(map[ID]*Session)
br.routes[msg.Topic] = r
}
r[id] = sub
s, ok := br.sessions[sub]
if !ok {
s = make(map[ID]struct{})
br.sessions[sub] = s
}
s[id] = struct{}{}
br.subscriptions[id] = msg.Topic
br.lock.Unlock()
sub.Send(&Subscribed{Request: msg.Request, Subscription: id})
}
func (br *defaultBroker) Unsubscribe(sub *Session, msg *Unsubscribe) {
br.lock.Lock()
topic, ok := br.subscriptions[msg.Subscription]
if !ok {
br.lock.Unlock()
err := &Error{
Type: msg.MessageType(),
Request: msg.Request,
Error: ErrNoSuchSubscription,
}
sub.Send(err)
log.Printf("Error unsubscribing: no such subscription %v", msg.Subscription)
return
}
delete(br.subscriptions, msg.Subscription)
// clean up routes
if r, ok := br.routes[topic]; !ok {
log.Printf("Error unsubscribing: unable to find routes for %s topic", topic)
} else if _, ok := r[msg.Subscription]; !ok {
log.Printf("Error unsubscribing: %s route does not exist for %v subscription", topic, msg.Subscription)
} else {
delete(r, msg.Subscription)
if len(r) == 0 {
delete(br.routes, topic)
}
}
// clean up sender's subscription
if s, ok := br.sessions[sub]; !ok {
log.Println("Error unsubscribing: unable to find sender's subscriptions")
} else if _, ok := s[msg.Subscription]; !ok {
log.Printf("Error unsubscribing: sender does not contain %v subscription", msg.Subscription)
} else {
delete(s, msg.Subscription)
if len(s) == 0 {
delete(br.sessions, sub)
}
}
br.lock.Unlock()
sub.Send(&Unsubscribed{Request: msg.Request})
}
func (br *defaultBroker) RemoveSession(sub *Session) {
br.lock.Lock()
defer br.lock.Unlock()
for id, _ := range br.sessions[sub] {
topic, ok := br.subscriptions[id]
if !ok {
continue
}
delete(br.subscriptions, id)
// clean up routes
if r, ok := br.routes[topic]; ok {
if _, ok := r[id]; ok {
delete(r, id)
if len(r) == 0 {
delete(br.routes, topic)
}
}
}
}
delete(br.sessions, sub)
}