gomodule / redigo

Go client for Redis
Apache License 2.0
9.76k stars 1.25k forks source link

PubSubConn.ReceiveWithTimeout allways has Error #676

Open brucelin-cn opened 1 month ago

brucelin-cn commented 1 month ago
func runRecv(mq *MessageQueue) {
    conn := mq.pool.Get()
    defer conn.Close()
    tmp := rand.Intn(1000000000)
    var runningErr error
    psc := redis.PubSubConn{Conn: conn}
    for {

        v := psc.ReceiveWithTimeout(100 * time.Millisecond)
        switch msg := v.(type) {
        case redis.Message:
            log.Info("Message", tmp, msg.Channel, msg.Data)
            mq.broadcastMessage(msg.Channel, msg.Data)
        case redis.Subscription:

            log.Info("Subscription change:%v %s: %s %d\n", tmp, msg.Kind, msg.Channel, msg.Count)
        case error:

            if msg != nil {
                if nErr, ok := msg.(net.Error); ok && nErr.Timeout() {          

                    log.Error("Redis Timeout error:", tmp, msg, conn.Err())
                    if err := psc.Subscribe("foo"); err != nil {
                        log.Info("Ping failed:", err)
                        runningErr = err
                    }
                    continue
                } else {

                    runningErr = msg
                    log.Error("Redis error:", tmp, msg, conn.Err(), runningErr)

                }
            }

        }

    }
}

During the second loop, the error "use of closed network connection" keeps occurring. at line:log.Error("Redis error:", xxxx)

maybe there is some bugs here?

func (c *conn) ReceiveWithTimeout(timeout time.Duration) (reply interface{}, err error) {
      //  should be check is Timeout Err?
    if reply, err = c.readReply(); err != nil {
        return nil, c.fatal(err)
    }
     //

}