-
Notifications
You must be signed in to change notification settings - Fork 15
/
broker.go
172 lines (136 loc) · 3.75 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 net
import (
"net/http"
"sync"
"time"
)
type Broker struct {
mtx sync.Mutex
clientSessions map[string]map[string]*ClientConnection
clientMetadata map[string]ClientMetadata
customHeaders map[string]string
disconnectCallback func(clientId string, sessionId string)
}
func NewBroker(customHeaders map[string]string) *Broker {
return &Broker{
clientSessions: make(map[string]map[string]*ClientConnection),
clientMetadata: map[string]ClientMetadata{},
customHeaders: customHeaders,
}
}
func (b *Broker) Connect(clientId string, w http.ResponseWriter, r *http.Request) (*ClientConnection, error) {
return b.ConnectWithHeartBeatInterval(clientId, w, r, 15*time.Second)
}
func (b *Broker) ConnectWithHeartBeatInterval(clientId string, w http.ResponseWriter, r *http.Request, interval time.Duration) (*ClientConnection, error) {
client, err := newClientConnection(clientId, w, r)
if err != nil {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return nil, NewStreamingUnsupportedError(err.Error())
}
b.setHeaders(w)
b.addClient(clientId, client)
go client.serve(
interval,
func() {
b.removeClient(clientId, client.sessionId) //onClose callback
},
)
return client, nil
}
func (b *Broker) setHeaders(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
for k, v := range b.customHeaders {
w.Header().Set(k, v)
}
}
func (b *Broker) IsClientPresent(clientId string) bool {
b.mtx.Lock()
defer b.mtx.Unlock()
_, ok := b.clientSessions[clientId]
return ok
}
func (b *Broker) SetClientMetadata(clientId string, metadata map[string]interface{}) error {
b.mtx.Lock()
defer b.mtx.Unlock()
_, ok := b.clientSessions[clientId]
if !ok {
return NewUnknownClientError(clientId)
}
b.clientMetadata[clientId] = metadata
return nil
}
func (b *Broker) GetClientMetadata(clientId string) (map[string]interface{}, error) {
b.mtx.Lock()
defer b.mtx.Unlock()
_, ok := b.clientSessions[clientId]
md, ok2 := b.clientMetadata[clientId]
if !ok || !ok2 {
return nil, NewUnknownClientError(clientId)
}
return md, nil
}
func (b *Broker) addClient(clientId string, connection *ClientConnection) {
b.mtx.Lock()
defer b.mtx.Unlock()
_, ok := b.clientSessions[clientId]
if !ok {
b.clientSessions[clientId] = make(map[string]*ClientConnection)
}
b.clientSessions[clientId][connection.sessionId] = connection
}
func (b *Broker) removeClient(clientId string, sessionId string) {
b.mtx.Lock()
defer b.mtx.Unlock()
sessions, ok := b.clientSessions[clientId]
if !ok {
return
}
delete(sessions, sessionId)
if len(b.clientSessions[clientId]) == 0 {
delete(b.clientSessions, clientId)
delete(b.clientMetadata, clientId)
}
if b.disconnectCallback != nil {
go b.disconnectCallback(clientId, sessionId)
}
}
func (b *Broker) Broadcast(event Event) {
b.mtx.Lock()
defer b.mtx.Unlock()
for _, sessions := range b.clientSessions {
for _, c := range sessions {
c.Send(event)
}
}
}
func (b *Broker) Send(clientId string, event Event) error {
b.mtx.Lock()
defer b.mtx.Unlock()
sessions, ok := b.clientSessions[clientId]
if !ok {
return NewUnknownClientError(clientId)
}
for _, c := range sessions {
c.Send(event)
}
return nil
}
func (b *Broker) SetDisconnectCallback(cb func(clientId string, sessionId string)) {
b.disconnectCallback = cb
}
func (b *Broker) Close() error {
b.mtx.Lock()
defer b.mtx.Unlock()
for _, v := range b.clientSessions {
// Mark all client sessions as done
for _, session := range v {
session.doneChan <- true
}
}
// Clear client sessions
b.clientSessions = map[string]map[string]*ClientConnection{}
return nil
}