-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 PgListener::next_buffered(), to support batch processing of notifications #3560
Conversation
Changed function name in response to feedback, and got tests passing. Frustratingly, try_recv() doesn't seem to be enough to fill the buffer of more than one notification, but a simple SQL statement like 'SELECT 1' is. Not sure why that is. |
The buffer exists because the server may send notifications at almost any time, including while executing a query. I don't have much issue with changing
Now that I think about it, I'm actually wary of changing this behavior because, in the case of the server constantly emitting notifications, we'd have potential issues with it never returning and just continuing to fill the buffer forever. We'd have to stop reading messages at some arbitrary, or configurable limit. I might end up re-thinking the |
Now that I think about this more, if you actually want it to try reading from the socket and just not wait, you would just use I would hesitate to recommend it right now only because if the connection is broken, it'll try to reconnect but because it doesn't wait, it'll just open a socket and then immediately close it. We'd need to spawn the |
Thanks @abonander. I didn't know about Having try_recv() read up to a hard-coded number of notifications (say, 100?) seems like a pretty straightforward and safe change to me, and it could be made configurable whenever the PgListener interface is rethought. I'm happy to make that change and update the PR if you're open to it. |
Before long, I want to make a significant refactor to the driver code that runs the connection state machine in its own task. Then, it will always eagerly read notifications and send them over a channel to be processed. |
Hello! I wanted to support batch processing of notifications when there is a lot of them, so it made sense to grab whatever notifications might be readily available.
try_next()
is unsuitable for that, since if the buffer is empty, I don't want to wait for a new NOTIFY to happen before progressing with the batch. So, it seemed to make sense to add a new non-async method that is essentially the first buffer check oftry_recv()
, and then call that in a loop to build up a batch until it returns None (or a batch limit is released).I updated
try_recv()
to call the new function, just to make things as DRY as possible, but it isn't a huge improvement. Happy to revert that part if you prefer.Also happy to bikeshed the new method's name/interface. A couple options I considered were:
Anyway, ran the tests with
cargo test -p sqlx-postgres --all-features
, please let me know if you think there's anything else worth testing/running.Thanks!