diff --git a/backend/database/pubsub.go b/backend/database/pubsub.go index 4570e7b4..27b10687 100644 --- a/backend/database/pubsub.go +++ b/backend/database/pubsub.go @@ -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) }