djc/bb8

Question about connection shutdown / clean up

levkk opened this issue · 2 comments

levkk commented

Hi there,

Thank you for writing this awesome pooler, I'm using it in PgCat and it's working really well.

I see that it has a clear way to setup a connection, but I'm wondering if you had any advice on how to cleanly shut down one. In my case, since I'm implementing the Postgres protocol, I would like to send an X message to the server and then close the TCP socket. However, since I'm using async sockets, I don't think I can easily implement that as the Drop trait for my connection, since fn drop() has to be synchronous.

Any advice? Thank you!

djc commented

PgCat looks like an interesting project, might be something that comes in handy at work in the future!

There are no really great solutions here. There have been proposals to have a new AsyncDrop trait in the language, but I think that won't be imminent. Another solution is to use futures::block_on, but that means blocking the current thread while your finalizing query is working, so probably not a great fit either. The final option I can think of off the top of my head is to spawn a task that takes care of doing drop work for you asynchronously; the more evolved version of that is to have a dedicated dropper task, give each connection wrapper type an mpsc sender to send it to the dropper task and handle everything in there.

Does that help?

levkk commented

This explains the situation which is very helpful.

What I've tried so far is try_write which is synchronous but if the socket isn't ready for some reason, it won't work as desired. In tests, it's been okay for now.

Thank you!