|
| 1 | +use std::{net::IpAddr, time::Duration}; |
| 2 | + |
| 3 | +use pyo3::{pyclass, pymethods, Py, Python}; |
| 4 | +use tokio_postgres::config::{ChannelBinding, Host, LoadBalanceHosts, SslMode, TargetSessionAttrs}; |
| 5 | + |
| 6 | +#[pyclass] |
| 7 | +pub struct ConnectionPoolBuilder { |
| 8 | + pub user: Option<String>, |
| 9 | + pub password: Option<Vec<u8>>, |
| 10 | + pub dbname: Option<String>, |
| 11 | + pub options: Option<String>, |
| 12 | + pub application_name: Option<String>, |
| 13 | + pub ssl_mode: SslMode, |
| 14 | + pub host: Vec<Host>, |
| 15 | + pub hostaddr: Vec<IpAddr>, |
| 16 | + pub port: Vec<u16>, |
| 17 | + pub connect_timeout: Option<Duration>, |
| 18 | + pub tcp_user_timeout: Option<Duration>, |
| 19 | + pub keepalives: bool, |
| 20 | + #[cfg(not(target_arch = "wasm32"))] |
| 21 | + // pub keepalive_config: KeepaliveConfig, |
| 22 | + pub target_session_attrs: TargetSessionAttrs, |
| 23 | + pub channel_binding: ChannelBinding, |
| 24 | + pub load_balance_hosts: LoadBalanceHosts, |
| 25 | +} |
| 26 | + |
| 27 | +#[pymethods] |
| 28 | +impl ConnectionPoolBuilder { |
| 29 | + #[new] |
| 30 | + fn new() -> Self { |
| 31 | + ConnectionPoolBuilder { |
| 32 | + user: None, |
| 33 | + password: None, |
| 34 | + dbname: None, |
| 35 | + options: None, |
| 36 | + application_name: None, |
| 37 | + ssl_mode: SslMode::Prefer, |
| 38 | + host: vec![], |
| 39 | + hostaddr: vec![], |
| 40 | + port: vec![], |
| 41 | + connect_timeout: None, |
| 42 | + tcp_user_timeout: None, |
| 43 | + keepalives: true, |
| 44 | + // #[cfg(not(target_arch = "wasm32"))] |
| 45 | + // keepalive_config: KeepaliveConfig { |
| 46 | + // idle: Duration::from_secs(2 * 60 * 60), |
| 47 | + // interval: None, |
| 48 | + // retries: None, |
| 49 | + // }, |
| 50 | + target_session_attrs: TargetSessionAttrs::Any, |
| 51 | + channel_binding: ChannelBinding::Prefer, |
| 52 | + load_balance_hosts: LoadBalanceHosts::Disable, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Sets the user to authenticate with. |
| 57 | + /// |
| 58 | + /// Defaults to the user executing this process. |
| 59 | + #[must_use] |
| 60 | + pub fn user(self_: Py<Self>, user: String) -> Py<ConnectionPoolBuilder> { |
| 61 | + Python::with_gil(|gil| { |
| 62 | + let mut self_ = self_.borrow_mut(gil); |
| 63 | + self_.user = Some(user); |
| 64 | + }); |
| 65 | + self_ |
| 66 | + } |
| 67 | + |
| 68 | + // /// Sets the password to authenticate with. |
| 69 | + // pub fn password(&mut self, password: String) -> &mut ConnectionPoolBuilder { |
| 70 | + // self.password = Some(password.into_bytes().to_vec()); |
| 71 | + // self |
| 72 | + // } |
| 73 | + |
| 74 | + // /// Gets the password to authenticate with, if one has been configured with |
| 75 | + // /// the `password` method. |
| 76 | + // pub fn get_password(&self) -> Option<&[u8]> { |
| 77 | + // self.password.as_deref() |
| 78 | + // } |
| 79 | + |
| 80 | + // /// Sets the name of the database to connect to. |
| 81 | + // /// |
| 82 | + // /// Defaults to the user. |
| 83 | + // pub fn dbname(&mut self, dbname: &str) -> &mut ConnectionPoolBuilder { |
| 84 | + // self.dbname = Some(dbname.to_string()); |
| 85 | + // self |
| 86 | + // } |
| 87 | + |
| 88 | + // /// Gets the name of the database to connect to, if one has been configured |
| 89 | + // /// with the `dbname` method. |
| 90 | + // pub fn get_dbname(&self) -> Option<&str> { |
| 91 | + // self.dbname.as_deref() |
| 92 | + // } |
| 93 | + |
| 94 | + // /// Sets command line options used to configure the server. |
| 95 | + // pub fn options(&mut self, options: &str) -> &mut ConnectionPoolBuilder { |
| 96 | + // self.options = Some(options.to_string()); |
| 97 | + // self |
| 98 | + // } |
| 99 | + |
| 100 | + // /// Gets the command line options used to configure the server, if the |
| 101 | + // /// options have been set with the `options` method. |
| 102 | + // pub fn get_options(&self) -> Option<&str> { |
| 103 | + // self.options.as_deref() |
| 104 | + // } |
| 105 | + |
| 106 | + // /// Sets the value of the `application_name` runtime parameter. |
| 107 | + // pub fn application_name(&mut self, application_name: &str) -> &mut ConnectionPoolBuilder { |
| 108 | + // self.application_name = Some(application_name.to_string()); |
| 109 | + // self |
| 110 | + // } |
| 111 | + |
| 112 | + // /// Gets the value of the `application_name` runtime parameter, if it has |
| 113 | + // /// been set with the `application_name` method. |
| 114 | + // pub fn get_application_name(&self) -> Option<&str> { |
| 115 | + // self.application_name.as_deref() |
| 116 | + // } |
| 117 | + |
| 118 | + // /// Sets the SSL configuration. |
| 119 | + // /// |
| 120 | + // /// Defaults to `prefer`. |
| 121 | + // pub fn ssl_mode(&mut self, ssl_mode: SslMode) -> &mut ConnectionPoolBuilder { |
| 122 | + // self.ssl_mode = ssl_mode; |
| 123 | + // self |
| 124 | + // } |
| 125 | + |
| 126 | + // /// Gets the SSL configuration. |
| 127 | + // pub fn get_ssl_mode(&self) -> SslMode { |
| 128 | + // self.ssl_mode |
| 129 | + // } |
| 130 | + |
| 131 | + // /// Adds a host to the configuration. |
| 132 | + // /// |
| 133 | + // /// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix |
| 134 | + // /// systems, a host starting with a `/` is interpreted as a path to a directory containing Unix domain sockets. |
| 135 | + // /// There must be either no hosts, or the same number of hosts as hostaddrs. |
| 136 | + // pub fn host(&mut self, host: &str) -> &mut ConnectionPoolBuilder { |
| 137 | + // #[cfg(unix)] |
| 138 | + // { |
| 139 | + // if host.starts_with('/') { |
| 140 | + // return self.host_path(host); |
| 141 | + // } |
| 142 | + // } |
| 143 | + |
| 144 | + // self.host.push(Host::Tcp(host.to_string())); |
| 145 | + // self |
| 146 | + // } |
| 147 | + |
| 148 | + // /// Gets the hosts that have been added to the configuration with `host`. |
| 149 | + // pub fn get_hosts(&self) -> &[Host] { |
| 150 | + // &self.host |
| 151 | + // } |
| 152 | + |
| 153 | + // /// Adds a Unix socket host to the configuration. |
| 154 | + // /// |
| 155 | + // /// Unlike `host`, this method allows non-UTF8 paths. |
| 156 | + // #[cfg(unix)] |
| 157 | + // pub fn host_path(&mut self, host: String) -> &mut ConnectionPoolBuilder { |
| 158 | + // self.host.push(Host::Unix(host.bytes().to_path_buf())); |
| 159 | + // self |
| 160 | + // } |
| 161 | + |
| 162 | + // /// Adds a hostaddr to the configuration. |
| 163 | + // /// |
| 164 | + // /// Multiple hostaddrs can be specified by calling this method multiple times, and each will be tried in order. |
| 165 | + // /// There must be either no hostaddrs, or the same number of hostaddrs as hosts. |
| 166 | + // pub fn hostaddr(&mut self, hostaddr: IpAddr) -> &mut ConnectionPoolBuilder { |
| 167 | + // self.hostaddr.push(hostaddr); |
| 168 | + // self |
| 169 | + // } |
| 170 | + |
| 171 | + // /// Adds a port to the configuration. |
| 172 | + // /// |
| 173 | + // /// Multiple ports can be specified by calling this method multiple times. There must either be no ports, in which |
| 174 | + // /// case the default of 5432 is used, a single port, in which it is used for all hosts, or the same number of ports |
| 175 | + // /// as hosts. |
| 176 | + // pub fn port(&mut self, port: u16) -> &mut ConnectionPoolBuilder { |
| 177 | + // self.port.push(port); |
| 178 | + // self |
| 179 | + // } |
| 180 | + |
| 181 | + // /// Gets the ports that have been added to the configuration with `port`. |
| 182 | + // pub fn get_ports(&self) -> &[u16] { |
| 183 | + // &self.port |
| 184 | + // } |
| 185 | + |
| 186 | + // /// Sets the timeout applied to socket-level connection attempts. |
| 187 | + // /// |
| 188 | + // /// Note that hostnames can resolve to multiple IP addresses, and this timeout will apply to each address of each |
| 189 | + // /// host separately. Defaults to no limit. |
| 190 | + // pub fn connect_timeout(&mut self, connect_timeout: Duration) -> &mut ConnectionPoolBuilder { |
| 191 | + // self.connect_timeout = Some(connect_timeout); |
| 192 | + // self |
| 193 | + // } |
| 194 | + |
| 195 | + // /// Gets the connection timeout, if one has been set with the |
| 196 | + // /// `connect_timeout` method. |
| 197 | + // pub fn get_connect_timeout(&self) -> Option<&Duration> { |
| 198 | + // self.connect_timeout.as_ref() |
| 199 | + // } |
| 200 | + |
| 201 | + // /// Sets the TCP user timeout. |
| 202 | + // /// |
| 203 | + // /// This is ignored for Unix domain socket connections. It is only supported on systems where |
| 204 | + // /// TCP_USER_TIMEOUT is available and will default to the system default if omitted or set to 0; |
| 205 | + // /// on other systems, it has no effect. |
| 206 | + // pub fn tcp_user_timeout(&mut self, tcp_user_timeout: Duration) -> &mut ConnectionPoolBuilder { |
| 207 | + // self.tcp_user_timeout = Some(tcp_user_timeout); |
| 208 | + // self |
| 209 | + // } |
| 210 | + |
| 211 | + // /// Gets the TCP user timeout, if one has been set with the |
| 212 | + // /// `user_timeout` method. |
| 213 | + // pub fn get_tcp_user_timeout(&self) -> Option<&Duration> { |
| 214 | + // self.tcp_user_timeout.as_ref() |
| 215 | + // } |
| 216 | + |
| 217 | + // /// Controls the use of TCP keepalive. |
| 218 | + // /// |
| 219 | + // /// This is ignored for Unix domain socket connections. Defaults to `true`. |
| 220 | + // pub fn keepalives(&mut self, keepalives: bool) -> &mut ConnectionPoolBuilder { |
| 221 | + // self.keepalives = keepalives; |
| 222 | + // self |
| 223 | + // } |
| 224 | + |
| 225 | + // /// Reports whether TCP keepalives will be used. |
| 226 | + // pub fn get_keepalives(&self) -> bool { |
| 227 | + // self.keepalives |
| 228 | + // } |
| 229 | + |
| 230 | + // // /// Sets the amount of idle time before a keepalive packet is sent on the connection. |
| 231 | + // // /// |
| 232 | + // // /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. Defaults to 2 hours. |
| 233 | + // // #[cfg(not(target_arch = "wasm32"))] |
| 234 | + // // pub fn keepalives_idle(&mut self, keepalives_idle: Duration) -> &mut ConnectionPoolBuilder { |
| 235 | + // // self.keepalive_config.idle = keepalives_idle; |
| 236 | + // // self |
| 237 | + // // } |
| 238 | + |
| 239 | + // // /// Gets the configured amount of idle time before a keepalive packet will |
| 240 | + // // /// be sent on the connection. |
| 241 | + // // #[cfg(not(target_arch = "wasm32"))] |
| 242 | + // // pub fn get_keepalives_idle(&self) -> Duration { |
| 243 | + // // self.keepalive_config.idle |
| 244 | + // // } |
| 245 | + |
| 246 | + // // /// Sets the time interval between TCP keepalive probes. |
| 247 | + // // /// On Windows, this sets the value of the tcp_keepalive struct’s keepaliveinterval field. |
| 248 | + // // /// |
| 249 | + // // /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. |
| 250 | + // // #[cfg(not(target_arch = "wasm32"))] |
| 251 | + // // pub fn keepalives_interval( |
| 252 | + // // &mut self, |
| 253 | + // // keepalives_interval: Duration, |
| 254 | + // // ) -> &mut ConnectionPoolBuilder { |
| 255 | + // // self.keepalive_config.interval = Some(keepalives_interval); |
| 256 | + // // self |
| 257 | + // // } |
| 258 | + |
| 259 | + // // /// Gets the time interval between TCP keepalive probes. |
| 260 | + // // #[cfg(not(target_arch = "wasm32"))] |
| 261 | + // // pub fn get_keepalives_interval(&self) -> Option<Duration> { |
| 262 | + // // self.keepalive_config.interval |
| 263 | + // // } |
| 264 | + |
| 265 | + // // /// Sets the maximum number of TCP keepalive probes that will be sent before dropping a connection. |
| 266 | + // // /// |
| 267 | + // // /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. |
| 268 | + // // #[cfg(not(target_arch = "wasm32"))] |
| 269 | + // // pub fn keepalives_retries(&mut self, keepalives_retries: u32) -> &mut ConnectionPoolBuilder { |
| 270 | + // // self.keepalive_config.retries = Some(keepalives_retries); |
| 271 | + // // self |
| 272 | + // // } |
| 273 | + |
| 274 | + // // /// Gets the maximum number of TCP keepalive probes that will be sent before dropping a connection. |
| 275 | + // // #[cfg(not(target_arch = "wasm32"))] |
| 276 | + // // pub fn get_keepalives_retries(&self) -> Option<u32> { |
| 277 | + // // self.keepalive_config.retries |
| 278 | + // // } |
| 279 | + |
| 280 | + // /// Sets the requirements of the session. |
| 281 | + // /// |
| 282 | + // /// This can be used to connect to the primary server in a clustered database rather than one of the read-only |
| 283 | + // /// secondary servers. Defaults to `Any`. |
| 284 | + // pub fn target_session_attrs( |
| 285 | + // &mut self, |
| 286 | + // target_session_attrs: TargetSessionAttrs, |
| 287 | + // ) -> &mut ConnectionPoolBuilder { |
| 288 | + // self.target_session_attrs = target_session_attrs; |
| 289 | + // self |
| 290 | + // } |
| 291 | + |
| 292 | + // /// Gets the requirements of the session. |
| 293 | + // pub fn get_target_session_attrs(&self) -> TargetSessionAttrs { |
| 294 | + // self.target_session_attrs |
| 295 | + // } |
| 296 | + |
| 297 | + // /// Sets the channel binding behavior. |
| 298 | + // /// |
| 299 | + // /// Defaults to `prefer`. |
| 300 | + // pub fn channel_binding( |
| 301 | + // &mut self, |
| 302 | + // channel_binding: ChannelBinding, |
| 303 | + // ) -> &mut ConnectionPoolBuilder { |
| 304 | + // self.channel_binding = channel_binding; |
| 305 | + // self |
| 306 | + // } |
| 307 | + |
| 308 | + // /// Gets the channel binding behavior. |
| 309 | + // pub fn get_channel_binding(&self) -> ChannelBinding { |
| 310 | + // self.channel_binding |
| 311 | + // } |
| 312 | + |
| 313 | + // /// Sets the host load balancing behavior. |
| 314 | + // /// |
| 315 | + // /// Defaults to `disable`. |
| 316 | + // pub fn load_balance_hosts( |
| 317 | + // &mut self, |
| 318 | + // load_balance_hosts: LoadBalanceHosts, |
| 319 | + // ) -> &mut ConnectionPoolBuilder { |
| 320 | + // self.load_balance_hosts = load_balance_hosts; |
| 321 | + // self |
| 322 | + // } |
| 323 | +} |
0 commit comments