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

Add available() function to subscriber #587

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ibensw
Copy link

@ibensw ibensw commented Aug 27, 2024

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
Copy link
Owner

sewenew commented Aug 28, 2024

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
Copy link
Author

ibensw commented Aug 28, 2024

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.

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

Successfully merging this pull request may close these issues.

2 participants