sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

Add available() function to subscriber #587

Open ibensw opened 2 months ago

ibensw commented 2 months ago

I am using redis++ for an application with a redis backend. We rely on pubsub for some communication between different processes.

Currently, we use socket_timeout and a loop to consume messages. This has some drawbacks however.

  1. If other work comes in (not via redis) it can only be handled after the socket_timeout expires
  2. I cannot check if the subscriber has a message without blocking

Therefor, I created a method in subscriber that calls a non-blocking poll call on the hiredis file descriptor.

sewenew commented 2 months ago

Thanks for contributing!

However, your PR does not work with Windows, which does not have poll.h related stuffs. Also, even if you can check if it's available, you cannot run consuming in one thread, while call subscribe in another thread. Since Subscriber is not thread-safe.

Looks like this question is posted by you, and I've answered with 2 possible solutions to your problem. Please check if these solutions work with your case.

Regards

ibensw commented 2 months ago

I agree that drawing in extra (posix) dependencies it not ideal. The solutions you provided do not fit our need. It looks like hiredis thought of this by providing a REDIS_OPT_NONBLOCK option which would solve my problem. However, this option should only be used on subscribers and if I understand the code directly all connections are created without knowledge of being used as a subscriber or not. I could dig into that direction if you think that is a viable option.

Another option hiredis provides is to create a connection by providing a fd, but since redis++ uses a connection pool this would require a create and destroy callback and might be to complicated for this task.