Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using embedded subscribe and external client leads to io timeout #348

Open
Olex1313 opened this issue Oct 6, 2023 · 5 comments
Open

Using embedded subscribe and external client leads to io timeout #348

Olex1313 opened this issue Oct 6, 2023 · 5 comments

Comments

@Olex1313
Copy link

Olex1313 commented Oct 6, 2023

Hello, I've been using miniredis in my unit-tests and found unexpected behaviour when trying to subscribe with miniredis client and publish with https://github.com/redis/go-redis, which led to io timeout on publisher.
Then I've switched to go-redis on both sides which worked fine. Am I doing something wrong or is it a bug?

I've attach a minimal reproducing example later

@alicebob
Copy link
Owner

alicebob commented Oct 6, 2023 via email

@alicebob
Copy link
Owner

alicebob commented Oct 9, 2023

Also, make sure you use the /v2 version of miniredis.

@Olex1313
Copy link
Author

Olex1313 commented Oct 9, 2023

I've tried something like this:

func main() {
	mRedis, _ := miniredis.Run()
	mRedis.RequireUserAuth("hello", "world")
	redisUrl, _ := redis.ParseURL(fmt.Sprintf("redis://hello:world@%s", mRedis.Addr()))
	redisClient := redis.NewClient(redisUrl)

	miniSub := mRedis.NewSubscriber()
	miniSub.Subscribe("channel")

	curTime := time.Now()
	redisClient.Publish(context.Background(), "channel", "msg")
	println(time.Since(curTime) / 1000000000)

	curTime = time.Now()
	msgChan := miniSub.Messages()
	println(time.Since(curTime))

	select {
	case msg := <-msgChan:
		println(msg.Message)
	case <-time.After(time.Millisecond * 200):
		println("Too long")
	}
}

Apparently the problem is not exactly with publish, but somehow redis-go publishes a message approx 12 seconds :)

Miniredis version: github.com/alicebob/miniredis/v2 v2.30.5
redis-go version: github.com/redis/go-redis/v9 v9.1.0

@Olex1313
Copy link
Author

Olex1313 commented Oct 9, 2023

I'll try to benchmark with native-redis, dockerized-redis and miniredis today, when I have more free time, but it looks strange, maybe it is even redis client issue

@alicebob
Copy link
Owner

alicebob commented Oct 11, 2023

The problem is that "Publish" writes to a channel where nothing is reading on. If you change that to work in a Go routine it works for me:

    ...
    curTime := time.Now()
    go func() {
        redisClient.Publish(context.Background(), "channel", "msg")
        println(time.Since(curTime) / 1000000000)
    }()
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants