Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 psubscribe/punsubscribe to PubSub api #691
Add psubscribe/punsubscribe to PubSub api #691
Changes from 1 commit
349f3c6
a142d84
a27fce4
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Timeouts in tests usually end up becoming flaky. If there's a better way to test this, that would be preferable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been trying to come up with a better way of testing it, but I can't come up with one. I have worked around it with a sleep for now.
We need to start pulling prior to publishing the message, otherwise we won't get the published message. So I start the pulling of the stream on another fiber and I could wait until resource acquisition/stream initialization on the fiber is done prior to proceeding. But that does not guarantee that we are actually subscribed. In principle, the subscription is not active until redis responds with
subscribe
. Today, we do not wait until redis responds, so there is an interval between the stream having started and the subscription actually being active. This is what thesleep
solves in the tests.We could change the stream implementation so it blocks the resource acquisition in
PubSubInternals.channel
andPubSubInternals.pattern
until thesubscribe
event is received. I would be happy to implement this (I think this behavior makes more sense). But it is a change in behavior from how redis4cats work today.What are your thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cwe-dixa I had a look at the code and it seems we could know when we are subscribed from both
psubscribed
andsubscribed
indefaultListener
. I think the best idea would be to add the ability to fetch this information (alsounsubscribed
andpunsubscribed
) all the way toPubSubCommands
, which could be internally powered by aDeferred[F, Unit]
or so. E.g.Or we could also expose it as
def isSubscribed(channel: RedisChannel[K]): F[Boolean]
, but I think the former would have a better UX.In my experience, using either
sleep
ortimeout
in tests becomes flakyness issues sooner or later, and we already have an unsolved one related to streams, so I'd prefer to avoid more maintenance burden.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the long delay :-(
We could add a
subscribed
method as suggested toPubSubCommands
, but that does require quite a bit of care to handle correctly... And I'm starting to think that maybe we should just not handle it.If we do add a
subscribed
method toPubSubCommands
we need to consider what happens if we start two subscriptions to the same channel. ShouldPubSubCommands
defer to the first or the second one? What if one of them is cancelled?Having
subscribed
seems to imply also having apsubscribed
, but patterns have a hierarchy of sorts, unlike channels. Doespsubscribed(Pattern("f*"))
implypsubscribed(Pattern("foo*"))
?Also, for newcomers, it might not be obvious that something like
will block forever, since the
Stream
won't emit thesubscribe
command until it is pulled, hence.subscribed(RedisChannel("foo"))
will never complete.For the above, I'm starting to favor simply adding some retries to the tests, which I have done. Please let me know what you think :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, even with the retries, the test hangs sometimes, especially on CI.
It seems that there is some race condition in the listener. I can't explain why, but what I observe is:
PubSubInternals#pattern
)PubSubInternals#pattern
)I think something weird is happening with the dispatcher. I have tried dispatching on a closed dispatcher and that just throws an error. But we are not getting an error in these tests. Instead, it just block indefinitely.
Using either
unsafeRunAndForget
orunsafeRunTimed
in the listener makes the test pass, but I'm not sure if we should be using either of those. Especially since I don't fully understand what the issue is.I'm hoping that you do 🤞