-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscribe.go
More file actions
157 lines (147 loc) · 3.21 KB
/
Subscribe.go
File metadata and controls
157 lines (147 loc) · 3.21 KB
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
package redis
import (
"strings"
"time"
"github.com/gomodule/redigo/redis"
)
func (rd *Redis) Subscribe(name string, reset func(), received func([]byte)) bool {
rd.subs[name] = &SubCallbacks{reset: reset, received: received}
if rd.subConn != nil {
//rd.subLock.Lock()
err := rd.subConn.Subscribe(name)
//rd.subLock.Unlock()
if err != nil {
rd.logger.Error(err.Error(), "subscribeName", name)
} else {
return true
}
}
return false
}
func (rd *Redis) Unsubscribe(name string) bool {
delete(rd.subs, name)
if rd.subConn != nil {
//rd.subLock.Lock()
err := rd.subConn.Unsubscribe(name)
//rd.subLock.Unlock()
if err != nil {
rd.logger.Error(err.Error(), "subscribeName", name)
} else {
return true
}
}
return false
}
func (rd *Redis) Start() {
//rd.subLock = sync.Mutex{}
if rd.subs == nil {
rd.subs = make(map[string]*SubCallbacks)
}
rd.SubRunning = true
subStartChan := make(chan bool)
go rd.receiveSub(subStartChan)
<-subStartChan
}
func (rd *Redis) receiveSub(subStartChan chan bool) {
for {
if !rd.SubRunning {
break
}
// 开始接收订阅数据
if rd.subConn == nil {
conn, err := rd.GetConnection()
if err != nil {
time.Sleep(time.Second)
continue
}
rd.subConn = &redis.PubSubConn{Conn: conn}
// 重新订阅
if len(rd.subs) > 0 {
subs := make([]interface{}, 0)
for k := range rd.subs {
subs = append(subs, k)
}
//fmt.Println(" @@@@@", subs)
err = rd.subConn.Subscribe(subs...)
if err != nil {
rd.subConn.Close()
rd.subConn = nil
time.Sleep(time.Second)
continue
}
// 重新连接时调用重置数据的回掉
for _, v := range rd.subs {
if v.reset != nil {
v.reset()
}
}
}
}
if subStartChan != nil {
subStartChan <- true
subStartChan = nil
}
for {
isErr := false
receiveObj := rd.subConn.Receive()
//receiveObj := rd.subConn.ReceiveWithTimeout(50*time.Millisecond)
//fmt.Println(" >>>", receiveObj)
switch v := receiveObj.(type) {
case redis.Message:
callback := rd.subs[v.Channel]
if callback.received != nil {
callback.received(v.Data)
}
case redis.Subscription:
case redis.Pong:
case error:
if strings.Contains(v.Error(), "i/o timeout") {
break
}
if !strings.Contains(v.Error(), "connection closed") && !strings.Contains(v.Error(), "use of closed network connection") {
rd.logger.Error(v.Error())
}
if rd.subConn != nil {
_ = rd.subConn.Close()
rd.subConn = nil
}
isErr = true
break
}
if isErr {
break
}
if !rd.SubRunning {
break
}
}
if !rd.SubRunning {
break
}
}
if rd.subStopChan != nil {
rd.subStopChan <- true
}
}
func (rd *Redis) Stop() {
if rd.SubRunning {
rd.subStopChan = make(chan bool)
rd.SubRunning = false
if rd.subConn != nil {
// 取消订阅
if len(rd.subs) > 0 {
subs := make([]interface{}, 0)
for k := range rd.subs {
subs = append(subs, k)
}
_ = rd.subConn.Unsubscribe()
}
// 读一次再关闭可以防止Close时阻塞
_ = rd.subConn.ReceiveWithTimeout(50 * time.Millisecond)
_ = rd.subConn.Close()
rd.subConn = nil
}
<-rd.subStopChan
rd.subStopChan = nil
}
}