-
Notifications
You must be signed in to change notification settings - Fork 4
/
hub.go
228 lines (194 loc) · 5.16 KB
/
hub.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package hub
import (
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"sync"
"github.com/gorilla/websocket"
)
var (
ReadBufferSize int = 1024
WriteBufferSize int = 1024
)
type Hub struct {
sync.Mutex // protects connections
connections map[*connection]*subscriber // simple to close or remove connections
subscribers map[string]*subscriber // simple to find subscriber based on identifier string
log *log.Logger
allowedOrigins []string
wsConnFactory websocket.Upgrader // websocket connection upgrader
register chan *connection // register new connection on this channel
unregister chan *connection // unregister connection channel
subscribe chan *subscription // subscribe as user
Broadcast chan *Message // fan out message to all connections
Mailbox chan *MailMessage // fan out message to subscriber
SubscriptionTokenizer Tokenizer // for user subscription token validation
}
func New(logOutput io.Writer, origins ...string) *Hub {
h := &Hub{
allowedOrigins: origins,
register: make(chan *connection),
unregister: make(chan *connection),
connections: make(map[*connection]*subscriber),
subscribers: make(map[string]*subscriber),
subscribe: make(chan *subscription),
Broadcast: make(chan *Message),
Mailbox: make(chan *MailMessage),
}
factory := websocket.Upgrader{
ReadBufferSize: ReadBufferSize,
WriteBufferSize: WriteBufferSize,
CheckOrigin: h.checkOrigin,
}
h.wsConnFactory = factory
if nil == logOutput {
logOutput = ioutil.Discard
}
h.log = log.New(leveledLogWriter(logOutput), "", log.LstdFlags)
return h
}
func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
ws, err := h.wsConnFactory.Upgrade(w, r, nil)
if err != nil {
h.log.Println("[ERROR] failed to upgrade connection:", err)
return
}
c := &connection{send: make(chan []byte, 256), ws: ws, hub: h}
h.register <- c
go c.listenWrite()
c.listenRead()
}
func (h *Hub) Connections() int {
h.Lock()
defer h.Unlock()
return len(h.connections)
}
func (h *Hub) doRegister(c *connection) {
h.Lock()
defer h.Unlock()
h.connections[c] = nil
}
func (h *Hub) doMailbox(m *MailMessage) {
h.Lock()
defer h.Unlock()
s, ok := h.subscribers[m.Username]
if !ok {
h.log.Println("[DEBUG] there are no subscriptions from:", m.Username)
return
}
bytes, err := m.Message.bytes()
if err != nil {
h.log.Printf("[WARN] failed to marshal message: %+v, reason: %s\n", m, err)
return
}
h.log.Println("[DEBUG] subscriber connection count:", len(s.connections))
for c := range s.connections {
c.send <- bytes
}
}
func (h *Hub) doBroadcast(m *Message) {
h.Lock()
defer h.Unlock()
bytes, err := m.bytes()
if err != nil {
h.log.Printf("[WARN] failed to marshal message: %+v, reason: %s\n", m, err)
return
}
for c := range h.connections {
c.send <- bytes
}
}
func (h *Hub) doSubscribe(s *subscription) {
h.Lock()
defer h.Unlock()
if h.SubscriptionTokenizer == nil {
h.log.Println("[DEBUG] subscription tokenizer is not set, cannot validate subscriptions")
return
}
token := h.SubscriptionTokenizer.Tokenize(s.Username)
if token != s.Token {
h.log.Printf("[WARN] username [%s], token [%s] does not match given: [%s]\n", s.Username, token, s.Token)
return
}
// check if there already is a subscriber
ns, alreadyAvailable := h.subscribers[s.Username]
if !alreadyAvailable {
ns = &subscriber{
Username: s.Username,
connections: make(map[*connection]bool),
}
}
ns.connections[s.connection] = true
h.connections[s.connection] = ns
h.subscribers[ns.Username] = ns
h.log.Println("[DEBUG] subscribed as:", s.Username)
}
func (h *Hub) doUnregister(c *connection) {
h.Lock()
defer h.Unlock()
s, ok := h.connections[c]
if !ok {
h.log.Println("[WARN] cannot unregister connection, it is not registered.")
return
}
if s != nil {
delete(s.connections, c)
h.log.Printf("[DEBUG] unregistering one of subscribers: %s connections\n", s.Username)
if len(s.connections) == 0 {
// there are no more open connections for this subscriber
h.log.Printf("[DEBUG] unsubscribe: %s, no more open connections\n", s.Username)
delete(h.subscribers, s.Username)
}
}
h.log.Println("[DEBUG] unregistering socket connection")
c.close()
delete(h.connections, c)
}
func (h *Hub) checkOrigin(r *http.Request) bool {
origin := r.Header["Origin"]
if len(origin) == 0 {
return true
}
u, err := url.Parse(origin[0])
if err != nil {
return false
}
var allow bool
for _, o := range h.allowedOrigins {
if o == u.Host {
allow = true
break
}
if o == "*" {
allow = true
break
}
}
if !allow {
h.log.Printf("[DEBUG] none of allowed origins: %s matched: %s\n", strings.Join(h.allowedOrigins, ", "), u.Host)
}
return allow
}
func (h *Hub) Run() {
for {
select {
case c := <-h.register:
h.doRegister(c)
case c := <-h.unregister:
h.doUnregister(c)
case m := <-h.Broadcast:
h.doBroadcast(m)
case m := <-h.Mailbox:
h.doMailbox(m)
case s := <-h.subscribe:
h.doSubscribe(s)
}
}
}