Merge pull request #166 from hantmac/develop

bug fix: fix redis connection dead cycle
This commit is contained in:
Marvin Zhang
2019-08-24 15:21:59 +08:00
committed by GitHub

View File

@@ -1,9 +1,11 @@
package database
import (
"errors"
"fmt"
"github.com/apex/log"
"github.com/gomodule/redigo/redis"
"time"
"unsafe"
)
@@ -23,7 +25,9 @@ func (c *Subscriber) Connect() {
c.client = redis.PubSubConn{Conn: conn}
c.cbMap = make(map[string]SubscribeCallback)
go func() {
//retry connect redis 5 times, or panic
index := 0
go func(i int) {
for {
log.Debug("wait...")
switch res := c.client.Receive().(type) {
@@ -34,11 +38,24 @@ func (c *Subscriber) Connect() {
case redis.Subscription:
fmt.Printf("%s: %s %d\n", res.Channel, res.Kind, res.Count)
case error:
log.Error("error handle...")
log.Error("error handle redis connection...")
con, err := GetRedisConn()
if err != nil {
log.Fatal("redis dial failed")
continue
}
c.client = redis.PubSubConn{Conn: con}
c.cbMap = make(map[string]SubscribeCallback)
time.Sleep(2 * time.Second)
if i > 5 {
panic(errors.New("redis connection failed too many times, panic"))
}
i += 1
continue
}
}
}()
}(index)
}