forked from jcelliott/turnpike
-
Notifications
You must be signed in to change notification settings - Fork 3
/
broker.go
172 lines (146 loc) · 4.07 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
166
167
168
169
170
171
172
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)
// Remove all of session's subscriptions.
RemoveSession(*Session)
}
// A super simple broker that matches URIs to Subscribers.
type defaultBroker struct {
routes map[URI]map[ID]Sender
subscriptions map[ID]URI
subscribers map[*Session][]ID
lastRequestId ID
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]Sender),
subscriptions: make(map[ID]URI),
subscribers: make(map[*Session][]ID),
}
}
func (br *defaultBroker) nextRequestId() ID {
br.lastRequestId++
if br.lastRequestId > MAX_REQUEST_ID {
br.lastRequestId = 1
}
return br.lastRequestId
}
// 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(sess *Session, msg *Publish) {
br.RLock()
defer br.RUnlock()
pub := sess.Peer
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
}
for id, sub := range br.routes[msg.Topic] {
// shallow-copy the template
event := evtTemplate
event.Subscription = id
// don't send event to publisher
if sub != pub || !excludePublisher {
go sub.Send(&event)
}
}
// only send published message if acknowledge is present and set to true
if doPub, _ := msg.Options["acknowledge"].(bool); doPub {
go pub.Send(&Published{Request: msg.Request, Publication: pubID})
}
}
// Subscribe subscribes the client to the given topic.
func (br *defaultBroker) Subscribe(sess *Session, msg *Subscribe) {
br.Lock()
defer br.Unlock()
if _, ok := br.routes[msg.Topic]; !ok {
br.routes[msg.Topic] = make(map[ID]Sender)
}
id := br.nextRequestId()
br.routes[msg.Topic][id] = sess.Peer
br.subscriptions[id] = msg.Topic
// subscribers
ids, ok := br.subscribers[sess]
if !ok {
ids = []ID{}
}
ids = append(ids, id)
br.subscribers[sess] = ids
go sess.Peer.Send(&Subscribed{Request: msg.Request, Subscription: id})
}
func (br *defaultBroker) RemoveSession(sess *Session) {
log.Printf("broker remove peer %p", sess)
br.Lock()
defer br.Unlock()
for _, id := range br.subscribers[sess] {
br.unsubscribe(sess, id)
}
}
func (br *defaultBroker) Unsubscribe(sess *Session, msg *Unsubscribe) {
br.Lock()
defer br.Unlock()
if !br.unsubscribe(sess, msg.Subscription) {
err := &Error{
Type: msg.MessageType(),
Request: msg.Request,
Error: ErrNoSuchSubscription,
}
go sess.Peer.Send(err)
log.Printf("Error unsubscribing: no such subscription %v", msg.Subscription)
return
}
go sess.Peer.Send(&Unsubscribed{Request: msg.Request})
}
func (br *defaultBroker) unsubscribe(sess *Session, id ID) bool {
log.Printf("broker unsubscribing: %p, %d", &sess, id)
topic, ok := br.subscriptions[id]
if !ok {
return false
}
delete(br.subscriptions, id)
if r, ok := br.routes[topic]; !ok {
log.Printf("Error unsubscribing: unable to find routes for %s topic", topic)
} else if _, ok := r[id]; !ok {
log.Printf("Error unsubscribing: %s route does not exist for %v subscription", topic, id)
} else {
delete(r, id)
if len(r) == 0 {
delete(br.routes, topic)
}
}
// subscribers
ids := br.subscribers[sess][:0]
for _, id := range br.subscribers[sess] {
if id != id {
ids = append(ids, id)
}
}
if len(ids) == 0 {
delete(br.subscribers, sess)
} else {
br.subscribers[sess] = ids
}
return true
}