-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
166 lines (137 loc) · 3.53 KB
/
connection.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
package wso
import (
"sync"
"github.com/go-redis/redis"
"github.com/redis/rueidis"
)
type ConnectionPool struct {
Clients map[*Websocket]struct{}
alreadyClosed bool
close chan struct{}
connect chan *Websocket
disconnect chan *Websocket
broadcast chan []byte
options *websocketOptions
}
func NewConnectionPool(callOptions ...WebsocketCallOption) *ConnectionPool {
cp := &ConnectionPool{
Clients: make(map[*Websocket]struct{}),
close: make(chan struct{}),
connect: make(chan *Websocket),
disconnect: make(chan *Websocket),
broadcast: make(chan []byte),
}
cp.options = applyOptions(callOptions...)
return cp
}
func (p *ConnectionPool) SubscribeRedisPubsub(pubsub *redis.PubSub) *ConnectionPool {
// 分离 goroutine 来处理广播消息
go func() {
// 监听 Redis 的 Pub/Sub 消息频道
defer func() {
pubsub.Close()
}()
for {
select {
case <-p.close:
p.options.logger.Debug("subscribed pubsub closed")
return
case message := <-pubsub.Channel():
// 广播到所有连接
p.Broadcast([]byte(message.Payload))
}
}
}()
return p
}
func (p *ConnectionPool) SubscribeRueidisPubsub() (*ConnectionPool, rueidis.PubSubHooks) {
return p, rueidis.PubSubHooks{
OnMessage: func(m rueidis.PubSubMessage) {
// 广播到所有连接
p.Broadcast([]byte(m.Message))
},
}
}
func (p *ConnectionPool) Open() *ConnectionPool {
go func() {
for {
select {
case <-p.close:
p.options.logger.Debug("opened connection pool closed")
return
case client := <-p.connect:
p.Clients[client] = struct{}{}
case client := <-p.disconnect:
delete(p.Clients, client)
case message := <-p.broadcast:
for client := range p.Clients {
client.Write(message)
}
}
}
}()
return p
}
func (p *ConnectionPool) Close() {
if p.alreadyClosed {
return
}
p.alreadyClosed = true
for c := range p.Clients {
c.Close()
p.Remove(c)
}
p.close <- struct{}{}
close(p.close)
close(p.connect)
close(p.disconnect)
close(p.broadcast)
}
func (p *ConnectionPool) Add(clientWebsocket *Websocket) {
p.connect <- clientWebsocket
p.options.logger.Debugf("websocket 已连接: %s (%d 当前总计)", clientWebsocket.remoteAddr, len(p.Clients)+1)
}
func (p *ConnectionPool) Remove(clientWebsocket *Websocket) {
p.disconnect <- clientWebsocket
p.options.logger.Debugf("websocket 已离线: %s (%d 当前总计)", clientWebsocket.remoteAddr, len(p.Clients)-1)
}
func (p *ConnectionPool) Broadcast(message []byte) {
p.broadcast <- message
p.options.logger.Debugf("广播消息到 %d 个客户端", len(p.Clients))
}
type ConnectionPoolMap struct {
mutex sync.Mutex
mConnectionPool map[string]*ConnectionPool
mConnectionPoolKeys []string
}
func NewConnectionPoolMap() *ConnectionPoolMap {
return &ConnectionPoolMap{
mConnectionPool: make(map[string]*ConnectionPool),
mConnectionPoolKeys: make([]string, 0),
}
}
func (m *ConnectionPoolMap) Set(key string, value *ConnectionPool) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.mConnectionPool[key] = value
m.mConnectionPoolKeys = append(m.mConnectionPoolKeys, key)
}
func (m *ConnectionPoolMap) Get(key string) *ConnectionPool {
m.mutex.Lock()
defer m.mutex.Unlock()
p, ok := m.mConnectionPool[key]
if !ok || p == nil {
return nil
}
return p
}
func (m *ConnectionPoolMap) Delete(key string) {
m.mutex.Lock()
defer m.mutex.Unlock()
delete(m.mConnectionPool, key)
}
func (m *ConnectionPoolMap) Keys() []string {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.mConnectionPoolKeys
}