Fix nil ping error

This commit is contained in:
Vladimir Mihailenco 2018-10-09 10:52:30 +03:00
parent 92c3b30cb0
commit daf101bd79
1 changed files with 7 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package redis package redis
import ( import (
"errors"
"fmt" "fmt"
"sync" "sync"
"time" "time"
@ -10,6 +11,8 @@ import (
"github.com/go-redis/redis/internal/proto" "github.com/go-redis/redis/internal/proto"
) )
var errPingTimeout = errors.New("redis: ping timeout")
// PubSub implements Pub/Sub commands bas described in // PubSub implements Pub/Sub commands bas described in
// http://redis.io/topics/pubsub. Message receiving is NOT safe // http://redis.io/topics/pubsub. Message receiving is NOT safe
// for concurrent use by multiple goroutines. // for concurrent use by multiple goroutines.
@ -438,7 +441,6 @@ func (c *PubSub) initChannel() {
timer.Stop() timer.Stop()
healthy := true healthy := true
var pingErr error
for { for {
timer.Reset(timeout) timer.Reset(timeout)
select { select {
@ -448,10 +450,13 @@ func (c *PubSub) initChannel() {
<-timer.C <-timer.C
} }
case <-timer.C: case <-timer.C:
pingErr = c.Ping() pingErr := c.Ping()
if healthy { if healthy {
healthy = false healthy = false
} else { } else {
if pingErr == nil {
pingErr = errPingTimeout
}
c.mu.Lock() c.mu.Lock()
c._reconnect(pingErr) c._reconnect(pingErr)
c.mu.Unlock() c.mu.Unlock()