1use std::str::FromStr;
4
5use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod, Runtime};
6use tokio_postgres::{
7 tls::{MakeTlsConnect, TlsConnect},
8 Config, NoTls, Socket,
9};
10
11pub struct Connection;
13
14pub struct ConnectionBuilder<Tls>
16where
17 Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
18 Tls::Stream: Sync + Send,
19 Tls::TlsConnect: Sync + Send,
20 <Tls::TlsConnect as TlsConnect<Socket>>::Future: Send,
21{
22 url: String,
23 tls: Tls,
24 recycling_method: RecyclingMethod,
25 max_pool_size: Option<usize>,
26 runtime: Option<Runtime>,
27}
28
29impl<Tls> ConnectionBuilder<Tls>
30where
31 Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
32 Tls::Stream: Sync + Send,
33 Tls::TlsConnect: Sync + Send,
34 <Tls::TlsConnect as TlsConnect<Socket>>::Future: Send,
35{
36 fn to(url: impl Into<String>) -> ConnectionBuilder<NoTls> {
37 ConnectionBuilder {
38 url: url.into(),
39 tls: NoTls,
40 recycling_method: RecyclingMethod::Fast,
41 max_pool_size: None,
42 runtime: None,
43 }
44 }
45
46 pub fn tls<NewTls>(self, tls: NewTls) -> ConnectionBuilder<NewTls>
53 where
54 NewTls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
55 NewTls::Stream: Sync + Send,
56 NewTls::TlsConnect: Sync + Send,
57 <NewTls::TlsConnect as TlsConnect<Socket>>::Future: Send,
58 {
59 ConnectionBuilder {
60 tls,
61 url: self.url,
62 recycling_method: self.recycling_method,
63 runtime: self.runtime,
64 max_pool_size: self.max_pool_size,
65 }
66 }
67
68 pub fn max_pool_size(mut self, n: usize) -> Self {
73 self.max_pool_size = Some(n);
74
75 self
76 }
77
78 pub fn connect(self) -> Result<(), crate::Error> {
84 let config = Config::from_str(&self.url)?;
85 let manager_config = ManagerConfig {
86 recycling_method: self.recycling_method,
87 };
88
89 let manager = Manager::from_config(config, self.tls, manager_config);
90 let mut builder = Pool::builder(manager).runtime(Runtime::Tokio1);
91
92 if let Some(n) = self.max_pool_size {
93 builder = builder.max_size(n);
94 }
95
96 let pool = builder.build()?;
97
98 crate::set_pool(pool)?;
99
100 Ok(())
101 }
102}
103
104impl Connection {
105 pub fn build(connection_string: impl Into<String>) -> ConnectionBuilder<NoTls> {
127 ConnectionBuilder::<NoTls>::to(connection_string)
128 }
129}