-
Notifications
You must be signed in to change notification settings - Fork 4
/
hub.go
340 lines (301 loc) · 7.14 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package tok
import (
"context"
"expvar"
"log"
)
var (
expOnline = expvar.NewInt("tokOnline")
expUp = expvar.NewInt("tokUp")
expDown = expvar.NewInt("tokDown")
expEnq = expvar.NewInt("tokEnq")
expDeq = expvar.NewInt("tokDeq")
)
type checkFrame struct {
uid interface{}
chBool chan bool // channel to return online status
}
type downFrame struct {
uid interface{}
ttl uint32
data []byte
chErr chan error // channel to read send result from
}
type upFrame struct {
dv *Device
data []byte
}
// HubConfig config struct for creating new Hub
type HubConfig struct {
Actor Actor // Actor implement dispatch logic
Q Queue // Message Queue, if nil, message to offline user will not be cached
Sso bool // If it's true, new connection with same uid will kick off old ones
}
// Hub core of tok, dispatch message between connections
type Hub struct {
sso bool
actor Actor
q Queue
cons map[interface{}][]*connection // connection list
chUp chan *upFrame
chDown chan *downFrame
chConState chan *conState
chReadSignal chan interface{}
chKick chan interface{}
chQueryOnline chan chan []interface{}
chCheck chan *checkFrame
}
func createHub(actor Actor, q Queue, sso bool) *Hub {
if ReadTimeout > 0 {
log.Println("[tok] read timeout is enabled, make sure it's greater than your client ping interval. otherwise you'll get read timeout err")
} else {
if actor.Ping() == nil {
log.Fatalln("[tok] both read timeout and server ping have been disabled, server socket resource leak might happen")
}
}
hub := &Hub{
sso: sso,
actor: actor,
q: q,
cons: make(map[interface{}][]*connection),
chUp: make(chan *upFrame),
chDown: make(chan *downFrame),
chConState: make(chan *conState),
chReadSignal: make(chan interface{}),
chKick: make(chan interface{}),
chQueryOnline: make(chan chan []interface{}),
chCheck: make(chan *checkFrame),
}
go hub.run()
return hub
}
func (p *Hub) run() {
for {
select {
case state := <-p.chConState:
// log.Printf("connection state change: %v, %v \n", state.online, &state.con)
if state.online {
p.goOnline(state.con)
} else {
p.goOffline(state.con)
}
count := int64(len(p.cons))
expOnline.Set(count)
case f := <-p.chUp:
// log.Println("up data")
expUp.Add(1)
go func() {
b, err := p.actor.BeforeReceive(f.dv, f.data)
if err != nil {
return
}
if b == nil {
b = f.data
}
p.actor.OnReceive(f.dv, b)
}()
case ff := <-p.chDown:
if l := p.cons[ff.uid]; len(l) > 0 {
// online
go p.down(ff, l)
} else {
// offline
if ff.ttl == 0 {
ff.chErr <- ErrOffline
close(ff.chErr)
} else {
go p.cache(ff)
}
}
case cf := <-p.chCheck:
_, ok := p.cons[cf.uid]
cf.chBool <- ok
close(cf.chBool)
case uid := <-p.chReadSignal:
// only pop msg for online user
if len(p.cons[uid]) > 0 {
go p.popMsg(uid)
}
case uid := <-p.chKick:
p.innerKick(uid)
case chOnline := <-p.chQueryOnline:
result := make([]interface{}, 0, len(p.cons))
for uid := range p.cons {
result = append(result, uid)
}
chOnline <- result
close(chOnline)
}
}
}
func (p *Hub) popMsg(uid interface{}) {
if p.q == nil {
return
}
ctx := context.TODO()
for {
b, err := p.q.Deq(ctx, uid)
if err != nil {
log.Println("deq error", err)
return
}
if len(b) == 0 {
// no more data in queue
return
}
expDeq.Add(1)
if err := p.Send(uid, b, 0); err != nil {
if err := p.q.Enq(ctx, uid, b); err != nil {
log.Println("re-cache err", err, uid)
}
return
}
}
}
// Send message to someone.
// ttl is expiry seconds. 0 means only send to online user
// If ttl = 0 and user is offline, ErrOffline will be returned.
// If ttl > 0 and user is offline or online but send fail, message will be cached for ttl seconds.
func (p *Hub) Send(to interface{}, b []byte, ttl uint32) error {
ff := &downFrame{uid: to, data: b, ttl: ttl, chErr: make(chan error)}
p.chDown <- ff
err := <-ff.chErr
if ttl > 0 && err != nil {
// online send err
ff.chErr = make(chan error) // create new channel
go p.cache(ff)
return <-ff.chErr
}
return err
}
// CheckOnline return whether user online or not
func (p *Hub) CheckOnline(uid interface{}) bool {
cf := &checkFrame{uid: uid, chBool: make(chan bool)}
p.chCheck <- cf
return <-cf.chBool
}
// Online query online user list
func (p *Hub) Online() []interface{} {
ch := make(chan []interface{})
p.chQueryOnline <- ch
return <-ch
}
func (p *Hub) cache(ff *downFrame) {
defer close(ff.chErr)
ctx := context.TODO()
expEnq.Add(1)
if p.q == nil {
ff.chErr <- ErrQueueRequired
return
}
if err := p.q.Enq(ctx, ff.uid, ff.data, ff.ttl); err != nil {
ff.chErr <- err
}
}
func (p *Hub) down(f *downFrame, conns []*connection) {
defer close(f.chErr)
expDown.Add(1)
for _, con := range conns {
b, err := p.actor.BeforeSend(con.dv, f.data)
if err != nil {
return
}
if b == nil {
b = f.data
}
if err := con.Write(b); err != nil {
f.chErr <- err
continue
}
go p.actor.OnSent(con.dv, f.data)
}
}
func (p *Hub) goOffline(conn *connection) {
l := p.cons[conn.uid()]
rest := connExclude(l, conn)
// this connection has gotten offline, ignore
if len(l) == len(rest) {
return
}
if len(rest) == 0 {
delete(p.cons, conn.uid())
} else {
p.cons[conn.uid()] = rest
}
go p.close(conn)
}
func (p *Hub) innerKick(uid interface{}) {
for _, conn := range p.cons[uid] {
go p.close(conn)
}
delete(p.cons, uid)
}
func (p *Hub) byeThenClose(kicker *Device, conn *connection) {
b := p.actor.Bye(kicker, "sso", conn.dv)
if b != nil {
data, err := p.actor.BeforeSend(conn.dv, b)
if err == nil {
if data != nil {
b = data
}
if err := conn.Write(b); err != nil {
log.Println("[tok] write bye error", err)
}
}
}
p.close(conn)
}
func (p *Hub) close(conn *connection) {
conn.close()
p.actor.OnClose(conn.dv)
}
func (p *Hub) goOnline(conn *connection) {
defer func() {
go p.tryDeliver(conn.uid())
}()
l := p.cons[conn.uid()]
if l == nil {
p.cons[conn.uid()] = []*connection{conn}
return
}
if p.sso {
for _, c := range l {
if conn.ShareConn(c) {
continue // never close share connection
}
// notify before close connection
go p.byeThenClose(conn.dv, c)
}
p.cons[conn.uid()] = []*connection{conn}
return
}
// it's a new connection
if len(connExclude(l, conn)) == len(l) {
l = append(l, conn)
p.cons[conn.uid()] = l
}
}
// tryDeliver try to deliver all messages, if uid is online
func (p *Hub) tryDeliver(uid interface{}) {
p.chReadSignal <- uid
}
// Kick kick all connections of uid
func (p *Hub) Kick(uid interface{}) {
p.chKick <- uid
}
func (p *Hub) stateChange(conn *connection, online bool) {
p.chConState <- &conState{conn, online}
}
// receive data from user
func (p *Hub) receive(dv *Device, b []byte) {
p.chUp <- &upFrame{dv: dv, data: b}
}
func connExclude(l []*connection, ex *connection) []*connection {
rest := make([]*connection, 0, len(l))
for _, c := range l {
if c != ex {
rest = append(rest, c)
}
}
return rest
}