libpqxx
7.8.2
|
An ongoing, non-blocking stepping stone to a connection. More...
#include <connection.hxx>
Public Member Functions | |
connecting (zview connection_string=""_zv) | |
Start connecting. More... | |
connecting (connecting const &)=delete | |
connecting (connecting &&)=default | |
connecting & | operator= (connecting const &)=delete |
connecting & | operator= (connecting &&)=default |
int | sock () const &noexcept |
Get the socket. The socket may change during the connection process. More... | |
constexpr bool | wait_to_read () const &noexcept |
Should we currently wait to be able to read from the socket? More... | |
constexpr bool | wait_to_write () const &noexcept |
Should we currently wait to be able to write to the socket? More... | |
void | process () & |
Progress towards completion (but don't block). More... | |
constexpr bool | done () const &noexcept |
Is our connection finished? More... | |
connection | produce () && |
Produce the completed connection object. More... | |
An ongoing, non-blocking stepping stone to a connection.
Use this when you want to create a connection to the database, but without blocking your whole thread. It is only available on systems that have the <fcntl.h>
header, and Windows.
Connecting in this way is probably not "faster" (it's more complicated and has some extra overhead), but in some situations you can use it to make your application as a whole faster. It all depends on having other useful work to do in the same thread, and being able to wait on a socket. If you have other I/O going on at the same time, your event loop can wait for both the libpqxx socket and your own sockets, and wake up whenever any of them is ready to do work.
Connecting in this way is not properly "asynchronous;" it's merely "nonblocking." This means it's not a super-high-performance mechanism like you might get with e.g. io_uring
. In particular, if we need to look up the database hostname in DNS, that will happen synchronously.
To use this, create the connecting
object, passing a connection string. Then loop: If wait_to_read returns true, wait for the socket to have incoming data on it. If wait_to_write returns true, wait for the socket to be ready for writing. Then call process to process any incoming or outgoing data. Do all of this until done returns true (or there is an exception). Finally, call produce to get the completed connection.
For example:
pqxx::connecting::connecting | ( | zview | connection_string = ""_zv | ) |
Start connecting.
Referenced by pqxx::connection::connection_string().
|
delete |
|
default |
|
noexcept |
Is our connection finished?
|
delete |
|
default |
void pqxx::connecting::process | ( | ) | & |
Progress towards completion (but don't block).
Referenced by pqxx::connection::connection_string().
connection pqxx::connecting::produce | ( | ) | && |
Produce the completed connection object.
Use this only once, after done returned true
. Once you have called this, the connecting
instance has no more use or meaning. You can't call any of its member functions afterwards.
This member function is rvalue-qualified, meaning that you can only call it on an rvalue instance of the class. If what you have is not an rvalue, turn it into one by wrapping it in std::move()
.
Referenced by pqxx::connection::connection_string().
|
noexcept |
Get the socket. The socket may change during the connection process.
|
noexcept |
Should we currently wait to be able to read from the socket?
|
noexcept |
Should we currently wait to be able to write to the socket?