Skip to content

Add asynchronous notification support #19

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

Closed
sfackler opened this issue Oct 15, 2013 · 13 comments
Closed

Add asynchronous notification support #19

sfackler opened this issue Oct 15, 2013 · 13 comments

Comments

@sfackler
Copy link
Owner

A handler based approach like set_notice_handler doesn't really make sense here, since the common response to a notification is probably going to be querying for updated data.

Something like libpq's PGnotices seems like the most straightforward solution, but both blocking and polling options both seem like they'd be useful.

sfackler added a commit that referenced this issue Oct 15, 2013
A decent first pass on #19, but there really needs to be better support
for polling for notifications than making a query and calling next()
every once in a while.
@oberstet
Copy link

+1 for NOTIFY support.

Some notes:

Besides issuing a query blocking on NOTIFY, some PG libs also supports a 2nd mode: add the file descriptor of the connection LISTENing to a select/poll'ed set (e.g. http://pythonhosted.org/psycopg2/advanced.html#asynchronous-notifications).

In fact, psycopg2 allows to run all queries in an async style (http://pythonhosted.org/psycopg2/advanced.html#asynchronous-support) making use of the async support in libpq (http://www.postgresql.org/docs/current/static/libpq-async.html).

@sfackler
Copy link
Owner Author

Rust's new IO system doesn't currently have support for select/poll/etc. One option that may be reasonable in the sort term is to have a next_block method on the PostgresNotificationIterator that will block on the socket for a notification if there aren't any queued up. It wouldn't allow you to wait for a notification or some other input like select would, but it'd get you most of the way there.

Async queries shouldn't be too tough to add. It's a little sad that they'll make the interface more dangerous at runtime since you can't make any other queries while an async query is executing. Right now, the closest thing to this is the lazy_query API on statements inside of a transaction. You still have to wait for the first result batch to come back in this case, though, which may not be ideal if it's an expensive query.

@oberstet
Copy link

"Rust's new IO system doesn't currently have support for select/poll/etc.": that is .. limiting. OT: what about libuv and Rust? Is that usable / will it get the "standard" IO system with Rust?

@sfackler
Copy link
Owner Author

The new IO system (std::rt::io) is backed by libuv and will soon replace the legacy IO system. Rust-Postgres uses the new IO system currently as well. There has been some work towards supporting select in std::select but it isn't working yet. @alexcrichton is doing most of the work on getting the new IO system working currently I think.

@msierks
Copy link

msierks commented Nov 10, 2014

+1

@sfackler
Copy link
Owner Author

@msierks What kind of functionality are you looking for? The options I see are a nonblocking iterator (sort of what we have now), a blocking iterator, or a callback based system, or some combination of the above.

@msierks
Copy link

msierks commented Nov 12, 2014

I like the non-blocking iterator, but a blocking one with an optional timeout would be great if possible. Similarly to the wait_for_notify method in the ruby PostgreSQL adapter.

http://www.rubydoc.info/gems/pg/PG/Connection#wait_for_notify-instance_method

Currently, the only issue with the non-blocking iterator is I cannot periodically check the iterator for new notifications. I don't receive the notifications unless I do some other sql actions. Does that sound right or am I doing something improperly ?

@sfackler
Copy link
Owner Author

Yep, that's correct right now. Makes sense!

sfackler added a commit that referenced this issue Nov 26, 2014
@sfackler
Copy link
Owner Author

There's now a next_block method on Notifications: http://sfackler.github.io/doc/postgres/struct.Notifications.html#method.next_block. A version that times out is blocked on rust-lang/rust#19328.

sfackler added a commit that referenced this issue Dec 3, 2014
The setup is a little hairy, but seems correct.

cc #19
@sfackler
Copy link
Owner Author

sfackler commented Dec 3, 2014

@msierks next_block_for is now implemented. Does the current functionality cover your needs?

http://sfackler.github.io/doc/postgres/struct.Notifications.html#method.next_block_for

@msierks
Copy link

msierks commented Dec 3, 2014

@sfackler I haven't had a chance to implement it yet, but I believe it should. I will let you know once I have.

@msierks
Copy link

msierks commented Dec 4, 2014

@sfackler Yup, this fits my needs perfectly. Thanks! :)

@sfackler
Copy link
Owner Author

sfackler commented Dec 5, 2014

Cool! I'm going to close this issue. If future feature requests come up, feel free to open a new one.

@sfackler sfackler closed this as completed Dec 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants