Skip to content

Implement notification_stream for AsyncPgConnection #251

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

Merged
merged 7 commits into from
Jul 22, 2025

Conversation

lsunsi
Copy link
Contributor

@lsunsi lsunsi commented Jul 13, 2025

Goal

This PR aims to implement a way for an AsyncPgConnection user to receive notifications from postgresql.

Implementation

It leverages the already present functionality of the underlying connection. So this PR merely wants to expose the messages.

Limitations

As @weiznich mentioned on the discussion, this will only work if the connection is present (and it might not be, depending on other constructors).

The easier fix is obviously not supporting the alternative constructors, but since we are splitting functionality it's always a good place to question if they are needed.

This PR is a DRAFT aimed to ask questions to mantainers

Copy link
Contributor Author

@lsunsi lsunsi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some questions to be discussed and I'll address solutions before making the PR ready for review!

Copy link
Owner

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR. That looks already like a really good starting point. I've hopefully added comments to all your questions.

Before merging I also would like to see at least this test from diesel being ported to verify that the function works as expected.

@lsunsi
Copy link
Contributor Author

lsunsi commented Jul 15, 2025

@weiznich I updated the impl in the right direction, I only have one more major question I'll add as a comment. Tests and doc still missing

src/pg/mod.rs Outdated

pub fn notification_stream(
&mut self,
) -> impl futures_core::Stream<Item = diesel::pg::PgNotification> + '_ {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's about this signature. I changed it to Stream to match the channel, but now I think maybe it's wrong because you said it should be Stream<QueryResult>.

That said, the poll_messages function exposes Ok(Notification | Notice) | Err, so if I get an error I can't be sure it's from a notification, a notice, or anything. It's basically just an error.

So it feels to me that I should either output the 3 possible cases, or just pick the one I want (notification). But I'm not sure of this. Anyway, I think diesel sync might have gone through the same question?

Copy link
Owner

@weiznich weiznich Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs to be Stream<QueryResult> as we currently miss one important point: We still need to poll the drive_connection future as well as part of the stream, as otherwise we won't get any new notifications while the user polls on the channel we returned. I think that connection future might return an error (via the error channel), that then must be returned from the notification stream.

(Hopefully that's "clear" and not expressed in a too confusing way)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that actually makes sense. I implemented it on 297c383.
One leftover question, does it make sense to have the error go on both error_tx and notification_tx? That's what I ended up leaving with but I was not really sure.

@lsunsi
Copy link
Contributor Author

lsunsi commented Jul 16, 2025

@weiznich I think current implementation might be good enough. If we're getting closer, I'll work on the test next

@lsunsi
Copy link
Contributor Author

lsunsi commented Jul 19, 2025

@weiznich 459f5aa How about this? I think it fit really nicely, my only question would be about the timeout for the "no notification" test.

If this is cool with you, I think the only thing missing would be some documentation?

@lsunsi
Copy link
Contributor Author

lsunsi commented Jul 20, 2025

Added documentation and doctest based on sync diesel version. 685f0ba

I think this is ready for review now

@lsunsi lsunsi marked this pull request as ready for review July 20, 2025 10:49
Copy link
Owner

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for me, thanks for working on this 👍

@weiznich weiznich merged commit 53c52a4 into weiznich:main Jul 22, 2025
47 of 48 checks passed
@lsunsi lsunsi deleted the notification-stream branch July 22, 2025 14:32
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.

3 participants