go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

`pubsub.Receive()` will timeout

Open j178 opened this issue 1 year ago • 0 comments

#2060 cause SetReadDeadline not called, so PubSub.ReceiveTimeout(ctx, 0) will not get a read deadline set, thus it will receive the i/o timeout error after some time. Related #2139.

Expected Behavior

PubSub.ReceiveTimeout(ctx, 0) will block forever until a message is received, So does PubSub.Receive(), PubSub.ReceiveMessage() and PubSub.Channel().

Current Behavior

PubSub.Receive() will timeout if read nothing for sometime.

received subscribe: hello <nil>
redis: 2022/08/03 22:36:54 pubsub.go:168: redis: discarding bad PubSub connection: read tcp [::1]:53016->[::1]:6379: i/o timeout
received <nil> read tcp [::1]:53016->[::1]:6379: i/o timeout
received subscribe: hello <nil>
redis: 2022/08/03 22:36:57 pubsub.go:168: redis: discarding bad PubSub connection: read tcp [::1]:53035->[::1]:6379: i/o timeout
received <nil> read tcp [::1]:53035->[::1]:6379: i/o timeout
received subscribe: hello <nil>

Possible Solution

Revert #2060

Steps to Reproduce

package main

import (
    "context"
    "fmt"

    "github.com/go-redis/redis/v9"
)

func main() {
    c := redis.NewClient(&redis.Options{})
    pubsub := c.Subscribe(context.Background(), "hello")
    for {
        m, err := pubsub.Receive(context.Background())
        fmt.Println("received", m, err)
    }
}

Context (Environment)

Redis: 7.0.3 go-redis: master

j178 avatar Aug 03 '22 14:08 j178