rueidis icon indicating copy to clipboard operation
rueidis copied to clipboard

feature request: allow `UNBLOCK`ing a blocking call before cancelling it to reuse the connection later for better performance

Open davidxia opened this issue 4 months ago • 4 comments

👋 My team enables pipelining for specific requests by using ToPipe when building the command object. This gives us the following behavior when a blocking request is made.

This solves the problem of ensuring we don’t open too many connections, but has the downside that every time we cancel a blocking call, the connection is closed. It would be more performant to issue an UNBLOCK command (which tells Redis to stop blocking), and then we can reuse the connection.

Is this a reasonable feature request? If so, are you open to contributions?

davidxia avatar Sep 10 '25 19:09 davidxia

Hi @davidxia, nice to meet you here!

I think that is a worthwhile optimization! We are open to contributions for sure.

So, I think the optimization for the blocking .Do() should be done inside this block: https://github.com/redis/rueidis/blob/5b78011c2f17d089378f48ca0997b8e1b7edc392/mux.go#L269-L271

And it should look like the following:

  1. Check whether the cmd is a blocking command or not; If not, then we should close the wire and return it to the pool directly and skip all the following procedures.
  2. Check whether the resp.NonRedisError() is a context error; If not, then we should close the wire and return it to the pool directly and skip all the following procedures.
  3. Check whether wire.Error() is nil or not; if not, then the wire is already closed. We should return the wire to the pool directly and skip all the following procedures.
  4. Now, we can do the UNBLOCK: We don't close the wire and don't return it to the pool in the current m.blocking function. Instead, we need to create another goroutine to send the UNBLOCK command with m.pipeline(context.Background(), unblock) and only return the wire to the pool without closing it when the UNBLOCK command gets a response saying that the wire is unblocked successfully; Otherwise, we still close the wire before returning it to the pool.

rueian avatar Sep 12 '25 08:09 rueian

@rueian yes, I was glad to see you're the maintainer of this project! Thanks so much for the guidance. I'll make a PR soon. 🙏

davidxia avatar Sep 12 '25 14:09 davidxia

Hi @davidxia, do you need any help?

rueian avatar Oct 03 '25 03:10 rueian

Hi @davidxia, do you need any help?

I haven't gotten around to this yet. Will take a look today or early next week!

davidxia avatar Oct 03 '25 13:10 davidxia