mirror of
https://github.com/neondatabase/neon.git
synced 2026-05-15 20:20:38 +00:00
(stacked on #9990 and #9995) Partially fixes #1287 with a custom option field to enable the fixed behaviour. This allows us to gradually roll out the fix without silently changing the observed behaviour for our customers. related to https://github.com/neondatabase/cloud/issues/15284
76 lines
2.0 KiB
Rust
76 lines
2.0 KiB
Rust
use crate::client::SocketConfig;
|
|
use crate::codec::BackendMessage;
|
|
use crate::config::Host;
|
|
use crate::connect_raw::connect_raw;
|
|
use crate::connect_socket::connect_socket;
|
|
use crate::tls::{MakeTlsConnect, TlsConnect};
|
|
use crate::{Client, Config, Connection, Error, RawConnection};
|
|
use postgres_protocol2::message::backend::Message;
|
|
use tokio::net::TcpStream;
|
|
use tokio::sync::mpsc;
|
|
|
|
pub async fn connect<T>(
|
|
mut tls: T,
|
|
config: &Config,
|
|
) -> Result<(Client, Connection<TcpStream, T::Stream>), Error>
|
|
where
|
|
T: MakeTlsConnect<TcpStream>,
|
|
{
|
|
let hostname = match &config.host {
|
|
Host::Tcp(host) => host.as_str(),
|
|
};
|
|
|
|
let tls = tls
|
|
.make_tls_connect(hostname)
|
|
.map_err(|e| Error::tls(e.into()))?;
|
|
|
|
match connect_once(&config.host, config.port, tls, config).await {
|
|
Ok((client, connection)) => Ok((client, connection)),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
async fn connect_once<T>(
|
|
host: &Host,
|
|
port: u16,
|
|
tls: T,
|
|
config: &Config,
|
|
) -> Result<(Client, Connection<TcpStream, T::Stream>), Error>
|
|
where
|
|
T: TlsConnect<TcpStream>,
|
|
{
|
|
let socket = connect_socket(host, port, config.connect_timeout).await?;
|
|
let RawConnection {
|
|
stream,
|
|
parameters,
|
|
delayed_notice,
|
|
process_id,
|
|
secret_key,
|
|
} = connect_raw(socket, tls, config).await?;
|
|
|
|
let socket_config = SocketConfig {
|
|
host: host.clone(),
|
|
port,
|
|
connect_timeout: config.connect_timeout,
|
|
};
|
|
|
|
let (sender, receiver) = mpsc::unbounded_channel();
|
|
let client = Client::new(
|
|
sender,
|
|
socket_config,
|
|
config.ssl_mode,
|
|
process_id,
|
|
secret_key,
|
|
);
|
|
|
|
// delayed notices are always sent as "Async" messages.
|
|
let delayed = delayed_notice
|
|
.into_iter()
|
|
.map(|m| BackendMessage::Async(Message::NoticeResponse(m)))
|
|
.collect();
|
|
|
|
let connection = Connection::new(stream, delayed, parameters, receiver);
|
|
|
|
Ok((client, connection))
|
|
}
|