-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
210 lines (190 loc) · 3.98 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
package ep
import (
"errors"
"fmt"
"log"
"sync"
"time"
)
var (
ErrClientNil = errors.New("client=nil")
ErrClientAttached = errors.New("client already attached")
)
type IHub interface {
Add(*Client) error
ReadLoop(int)
WriteLoop()
ControlLoop()
}
type Hub struct {
clients sync.Map // <clientID, *Client>, 全集
csUIDs sync.Map // uid map[clientID]*Client
nPoll int // 并发度
up chan Req
unregister chan *Client
}
func NewHub() *Hub {
return &Hub{
nPoll: 80,
up: make(chan Req, ClientUpQueueSize),
unregister: make(chan *Client, HubUnregisterQueueSize),
}
}
func (e *Hub) Add(cli *Client) error {
if e == nil {
return nil
}
if cli == nil {
return ErrClientNil
}
if _, ok := e.clients.Load(cli.ID); ok {
return ErrClientAttached
}
e.clients.Store(cli.ID, cli)
cli.upChan = e.up
if cli.UID != "" {
if c, ok := e.csUIDs.Load(cli.UID); ok {
if cc, ok := c.(*sync.Map); ok {
cc.Store(cli.ID, cli)
}
} else {
cc := &sync.Map{}
cc.Store(cli.ID, cli)
e.csUIDs.Store(cli.UID, cc)
}
}
cli.hub = e
return nil
}
// ReadLoop receive message from kafka
func (e *Hub) ReadLoop(id int) {
defer func() {
if err := recover(); err != nil {
log.Printf("hub.ReadLoop[%d] exit: err=%v", id, err)
}
}()
for {
select {
default:
// read message from kafka
time.Sleep(1 * time.Second)
msg := &WSXChgMessage{}
e.dispatch(msg)
}
}
}
// WriteLoop write message to kafka to give upstream services
func (e *Hub) WriteLoop() {
defer func() {
if err := recover(); err != nil {
log.Printf("hub.WriteLoop: err %s", err)
}
}()
log.Printf("hub.writeloop start")
for {
select {
case req, ok := <-e.up:
if ok {
fmt.Printf("send req to up service %v", req)
}
}
}
log.Printf("hub.writeloop end")
}
func (e *Hub) ControlLoop() {
defer func() {
if err := recover(); err != nil {
log.Printf("hub.ControlLoop_exit: err %s", err)
}
}()
for {
select {
case c, ok := <-e.unregister:
if ok {
e.Delete(c)
}
}
}
}
func (e *Hub) broadcast(p *Payload, msgID string) {
total := 0
e.clients.Range(func(key, value interface{}) bool {
if c, ok := value.(*Client); ok && !c.Closed.Val() {
c.Send(p)
total += 1
}
return true
})
log.Println("OUT\thub.broadcast: trackid=", msgID, " total=", total)
}
func (e *Hub) send2uid(uids []string, p *Payload, msgid string) {
for _, uid := range uids {
if c, ok := e.csUIDs.Load(uid); ok {
if cc, ok := c.(*sync.Map); ok {
cc.Range(func(vcid, vcli interface{}) bool {
if cli, ok := vcli.(*Client); ok && !cli.Closed.Val() {
cli.Send(p)
}
return true
})
}
} else {
// log.Println("OUT\thub.send2uid:\ttrackid=", msgid, "uid", uid, "not found", "data", string(data))
}
}
}
func (e *Hub) send2client(cids []string, p *Payload, msgid string, extra interface{}) {
for _, cid := range cids {
if c, ok := e.clients.Load(cid); ok {
if cc, ok := c.(*Client); ok && !cc.Closed.Val() {
cc.Send(p)
}
} else {
// log.Printf("hub.send2client(): cid %s not found in hub", cid)
}
}
}
func (e *Hub) Delete(cli *Client) error {
if e == nil {
return nil
}
if cli == nil {
return ErrClientNil
}
e.clients.Delete(cli.ID)
cli.upChan = nil
if c, ok := e.csUIDs.Load(cli.UID); ok {
if cc, ok := c.(*sync.Map); ok {
cc.Delete(cli.ID)
l := SyncLen(cc)
if l == 0 {
e.csUIDs.Delete(cli.UID)
}
}
}
cli.hub = nil
return nil
}
func (e *Hub) dispatch(msg *WSXChgMessage) {
//log.Println("OUT\thub.dispatch:\t", msg)
cmd := msg.Command
rsp := NewBroadcast(msg.Command, msg.Payload)
data := rsp.Bytes()
p := &Payload{Command: cmd, Data: data}
msgID := msg.ID()
switch msg.Strategy {
case StrategyAll:
go e.broadcast(p, msgID)
case StrategyUID:
go e.send2uid(msg.To, p, msgID)
case StrategyClient:
go e.send2client(msg.To, p, msgID, msg.Resp.Data["extra"])
}
}
func (e *Hub) Start() {
for i := 0; i < e.nPoll; i++ {
go e.ReadLoop(i)
}
go e.WriteLoop()
go e.ControlLoop()
}