pubsub.go
5.1 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
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
package redis
import (
"sync"
"github.com/garyburd/redigo/redis"
//"math/rand"
"errors"
//"sync"
"fmt"
"time"
)
type subCallBack interface {
OnMessage(channnel string, msg []byte)
OnPMessage(pattern string, channel string, msg []byte)
OnSubscription(kind string, channel string, count int)
OnError(err error)
}
type PubSubClient struct {
m_host string
db_index uint32
password string
m_pool *redis.Pool
m_conn redis.PubSubConn
m_lock sync.Mutex
m_subCallback subCallBack
m_subList map[string]bool
m_psubList map[string]bool
}
func NewPubSubClient(host string, password string, db uint32) *PubSubClient {
client := new(PubSubClient)
client.m_host = host
client.db_index = db
client.password = password
client.m_subList = make(map[string]bool)
client.m_psubList = make(map[string]bool)
client.m_pool = &redis.Pool{
MaxIdle: 64,
//MaxActive: 1,
IdleTimeout: 60 * time.Second,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", host, redis.DialDatabase(int(db)), redis.DialPassword(password))
if err != nil {
return nil, err
}
return c, err
},
}
conn, err := redis.Dial("tcp", host, redis.DialDatabase(int(db)), redis.DialPassword(password))
if err != nil {
fmt.Printf("error dial:%v", err)
return nil
}
client.m_conn = redis.PubSubConn{Conn: conn}
return client
}
func (client *PubSubClient) Publish(channel, value interface{}) error {
conn := client.m_pool.Get()
defer conn.Close()
if _, err := conn.Do("PUBLISH", channel, value); err != nil {
return err
}
return nil
}
func (client *PubSubClient) Subscribe(channel string) error {
client.m_lock.Lock()
defer client.m_lock.Unlock()
if err := client.m_conn.Subscribe(channel); err != nil {
return err
}
client.m_subList[channel] = true
return nil
}
func (client *PubSubClient) Unsubscribe(channel string) error {
//logger.DEBUG("Unsubscribe, channName:%s\n", channel)
client.m_lock.Lock()
defer client.m_lock.Unlock()
if channel == "" {
if err := client.m_conn.Unsubscribe(); err != nil {
return err
}
// clear all element in this map
for k := range client.m_subList {
delete(client.m_subList, k)
}
} else {
if err := client.m_conn.Unsubscribe(channel); err != nil {
return err
}
delete(client.m_subList, channel)
}
return nil
}
func (client *PubSubClient) PSubscribe(channel string) error {
client.m_lock.Lock()
defer client.m_lock.Unlock()
if err := client.m_conn.PSubscribe(channel); err != nil {
return err
}
client.m_psubList[channel] = true
return nil
}
func (client *PubSubClient) PUnsubscribe(channel string) error {
client.m_lock.Lock()
defer client.m_lock.Unlock()
if channel == "" {
if err := client.m_conn.PUnsubscribe(); err != nil {
return err
}
// clear all element in this map
for k := range client.m_subList {
delete(client.m_psubList, k)
}
} else {
if err := client.m_conn.PUnsubscribe(channel); err != nil {
return err
}
delete(client.m_subList, channel)
}
return nil
}
func (client *PubSubClient) SetSubCallback(cb subCallBack) {
client.m_subCallback = cb
}
func (client *PubSubClient) reConnect() {
//1. first close old connection
client.m_conn.Close()
//2. create a new connection
for {
// conn, err := redis.Dial("tcp", client.m_host)
conn, err := redis.Dial("tcp", client.m_host, redis.DialDatabase(int(client.db_index)), redis.DialPassword(client.password))
if err == nil {
client.m_conn = redis.PubSubConn{Conn: conn}
return
}
time.Sleep(time.Second)
//logger.DEBUG("reConnecting\n")
}
}
func (client *PubSubClient) reSubscribe() {
for key := range client.m_subList {
client.Subscribe(key)
}
for key := range client.m_psubList {
client.PSubscribe(key)
}
}
func (client *PubSubClient) StartSubscribe(cb subCallBack) error {
if cb == nil {
return errors.New("subCallback is not set!")
}
client.m_subCallback = cb
go func() {
//exit_loop:
for {
switch n := client.m_conn.Receive().(type) {
case redis.Message:
//logger.DEBUG("Message: chann[%s] data[%s]\n", n.Channel, n.Data)
if client.m_subCallback.OnMessage != nil {
client.m_subCallback.OnMessage(n.Channel, n.Data)
}
case redis.PMessage:
//logger.DEBUG("PMessage: pattern[%s] chann[%s] data[%s]\n", n.Pattern, n.Channel, n.Data)
if client.m_subCallback.OnPMessage != nil {
client.m_subCallback.OnPMessage(n.Pattern, n.Channel, n.Data)
}
case redis.Subscription:
//logger.DEBUG("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count)
if client.m_subCallback.OnSubscription != nil {
client.m_subCallback.OnSubscription(n.Kind, n.Channel, n.Count)
}
if n.Count == 0 {
//logger.DEBUG("exit ddd: \n")
//break exit_loop
}
case error:
if client.m_subCallback.OnError != nil {
client.m_subCallback.OnError(n)
}
//logger.DEBUG("error: %v\n", n)
client.reConnect()
client.reSubscribe()
//client.m_conn = redis.PubSubConn{Conn: m_psPool.Get()}
//break exit_loop
}
}
//logger.DEBUG("exit: \n")
}()
return nil
}