Line data Source code
1 : use std::net::IpAddr;
2 :
3 : use futures_util::TryStreamExt;
4 : use postgres_protocol2::message::backend::Message;
5 : use tokio::io::{AsyncRead, AsyncWrite};
6 : use tokio::net::TcpStream;
7 : use tokio::sync::mpsc;
8 :
9 : use crate::client::SocketConfig;
10 : use crate::config::Host;
11 : use crate::connect_raw::StartupStream;
12 : use crate::connect_socket::connect_socket;
13 : use crate::tls::{MakeTlsConnect, TlsConnect};
14 : use crate::{Client, Config, Connection, Error};
15 :
16 0 : pub async fn connect<T>(
17 0 : tls: &T,
18 0 : config: &Config,
19 0 : ) -> Result<(Client, Connection<TcpStream, T::Stream>), Error>
20 0 : where
21 0 : T: MakeTlsConnect<TcpStream>,
22 0 : {
23 0 : let hostname = match &config.host {
24 0 : Host::Tcp(host) => host.as_str(),
25 : };
26 :
27 0 : let tls = tls
28 0 : .make_tls_connect(hostname)
29 0 : .map_err(|e| Error::tls(e.into()))?;
30 :
31 0 : match connect_once(config.host_addr, &config.host, config.port, tls, config).await {
32 0 : Ok((client, connection)) => Ok((client, connection)),
33 0 : Err(e) => Err(e),
34 : }
35 0 : }
36 :
37 0 : async fn connect_once<T>(
38 0 : host_addr: Option<IpAddr>,
39 0 : host: &Host,
40 0 : port: u16,
41 0 : tls: T,
42 0 : config: &Config,
43 0 : ) -> Result<(Client, Connection<TcpStream, T::Stream>), Error>
44 0 : where
45 0 : T: TlsConnect<TcpStream>,
46 0 : {
47 0 : let socket = connect_socket(host_addr, host, port, config.connect_timeout).await?;
48 0 : let mut stream = config.tls_and_authenticate(socket, tls).await?;
49 0 : let (process_id, secret_key) = wait_until_ready(&mut stream).await?;
50 :
51 0 : let socket_config = SocketConfig {
52 0 : host_addr,
53 0 : host: host.clone(),
54 0 : port,
55 0 : connect_timeout: config.connect_timeout,
56 0 : };
57 :
58 0 : let (client_tx, conn_rx) = mpsc::unbounded_channel();
59 0 : let (conn_tx, client_rx) = mpsc::channel(4);
60 0 : let client = Client::new(
61 0 : client_tx,
62 0 : client_rx,
63 0 : socket_config,
64 0 : config.ssl_mode,
65 0 : process_id,
66 0 : secret_key,
67 : );
68 :
69 0 : let stream = stream.into_framed();
70 0 : let connection = Connection::new(stream, conn_tx, conn_rx);
71 :
72 0 : Ok((client, connection))
73 0 : }
74 :
75 0 : async fn wait_until_ready<S, T>(stream: &mut StartupStream<S, T>) -> Result<(i32, i32), Error>
76 0 : where
77 0 : S: AsyncRead + AsyncWrite + Unpin,
78 0 : T: AsyncRead + AsyncWrite + Unpin,
79 0 : {
80 0 : let mut process_id = 0;
81 0 : let mut secret_key = 0;
82 :
83 : loop {
84 0 : match stream.try_next().await.map_err(Error::io)? {
85 0 : Some(Message::BackendKeyData(body)) => {
86 0 : process_id = body.process_id();
87 0 : secret_key = body.secret_key();
88 0 : }
89 : // These values are currently not used by `Client`/`Connection`. Ignore them.
90 0 : Some(Message::ParameterStatus(_)) | Some(Message::NoticeResponse(_)) => {}
91 0 : Some(Message::ReadyForQuery(_)) => return Ok((process_id, secret_key)),
92 0 : Some(Message::ErrorResponse(body)) => return Err(Error::db(body)),
93 0 : Some(_) => return Err(Error::unexpected_message()),
94 0 : None => return Err(Error::closed()),
95 : }
96 : }
97 0 : }
|