-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_topic.go
152 lines (133 loc) · 3.17 KB
/
mem_topic.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
package mqtt
import (
"log"
"strings"
"sync"
"time"
"github.com/golang-io/mqtt/packet"
)
type MemorySubscribed struct {
maps map[string]*TopicSubscribed
mu sync.RWMutex
s *Server
}
func (m *MemorySubscribed) Print() {
m.mu.RLock()
defer m.mu.RUnlock()
for _, sub := range m.maps {
log.Printf("[%s], conn=%d", sub.TopicName, len(sub.activeConn))
}
}
func (m *MemorySubscribed) Subscribe(c *conn) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, ts := range m.maps {
ts.Subscribe(c)
}
}
func (m *MemorySubscribed) Unsubscribe(c *conn) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, ts := range m.maps {
ts.Unsubscribe(c)
}
}
// Publish 发布消息,如果是新topic需要额外处理存量connect订阅列表的构建
func (m *MemorySubscribed) Publish(message *packet.Message) error {
m.mu.RLock()
sub, ok := m.maps[message.TopicName]
m.mu.RUnlock()
if !ok {
sub = NewTopicSubscribed(message.TopicName)
m.s.mu.RLock()
for c := range m.s.activeConn { // 如果是新的topic, 这里需要构建订阅列表!
sub.Subscribe(c)
}
m.s.mu.RUnlock()
m.mu.Lock()
m.maps[message.TopicName] = sub
m.mu.Unlock()
}
return sub.Exchange(message)
}
func NewMemorySubscribed(s *Server) *MemorySubscribed {
m := &MemorySubscribed{
maps: make(map[string]*TopicSubscribed),
s: s,
}
go m.CleanEmptyTopic()
return m
}
func (m *MemorySubscribed) CleanEmptyTopic() {
ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()
for range ticker.C {
m.mu.RLock()
var empty []string
for key, sub := range m.maps {
if sub.Len() == 0 {
empty = append(empty, key)
}
}
m.mu.RUnlock()
m.mu.Lock()
for _, key := range empty {
delete(m.maps, key)
}
m.mu.Unlock()
}
}
// TopicSubscribed 用来存储当前topic有哪些客户端订阅了
type TopicSubscribed struct {
TopicName string
activeConn map[*conn]struct{}
// share map[string]map[*conn]struct{} // 共享订阅, group: conn
mux sync.RWMutex
}
func NewTopicSubscribed(topicName string) *TopicSubscribed {
if strings.Contains(topicName, "$share/") {
}
return &TopicSubscribed{
TopicName: topicName,
activeConn: make(map[*conn]struct{}),
}
}
func (s *TopicSubscribed) Subscribe(c *conn) {
if _, ok := c.subscribeTopics.Find(s.TopicName); !ok {
return
}
s.mux.Lock()
defer s.mux.Unlock()
s.activeConn[c] = struct{}{}
}
func (s *TopicSubscribed) Len() int {
s.mux.RLock()
defer s.mux.RUnlock()
return len(s.activeConn)
}
func (s *TopicSubscribed) Unsubscribe(c *conn) int {
s.mux.Lock()
defer s.mux.Unlock()
delete(s.activeConn, c)
return len(s.activeConn)
}
func (s *TopicSubscribed) Exchange(message *packet.Message) error {
s.mux.RLock()
defer s.mux.RUnlock()
for c := range s.activeConn {
response := &response{conn: c}
go func() {
newPub := &packet.PUBLISH{
FixedHeader: &packet.FixedHeader{Version: c.version, Kind: PUBLISH, Dup: 0, QoS: 0, Retain: 0},
Message: &packet.Message{TopicName: message.TopicName, Content: message.Content},
}
if newPub.QoS > 0 {
newPub.PacketID = c.PacketID + 1
c.PacketID = newPub.PacketID
}
//pktLog.Printf("send|%s - %s", Kind[newPub.Kind()], c.ID)
response.OnSend(newPub)
}()
}
return nil
}