@@ -6,6 +6,7 @@ use crate::connection::Connection;
6
6
use crate :: Client ;
7
7
use log:: info;
8
8
use std:: fmt;
9
+ use std:: net:: IpAddr ;
9
10
use std:: path:: Path ;
10
11
use std:: str:: FromStr ;
11
12
use std:: sync:: Arc ;
@@ -43,6 +44,19 @@ use tokio_postgres::{Error, Socket};
43
44
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
44
45
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
45
46
/// with the `connect` method.
47
+ /// * `hostaddr` - Numeric IP address of host to connect to. This should be in the standard IPv4 address format,
48
+ /// e.g., 172.28.40.9. If your machine supports IPv6, you can also use those addresses.
49
+ /// If this parameter is not specified, the value of `host` will be looked up to find the corresponding IP address,
50
+ /// - or if host specifies an IP address, that value will be used directly.
51
+ /// Using `hostaddr` allows the application to avoid a host name look-up, which might be important in applications
52
+ /// with time constraints. However, a host name is required for verify-full SSL certificate verification.
53
+ /// Specifically:
54
+ /// * If `hostaddr` is specified without `host`, the value for `hostaddr` gives the server network address.
55
+ /// The connection attempt will fail if the authentication method requires a host name;
56
+ /// * If `host` is specified without `hostaddr`, a host name lookup occurs;
57
+ /// * If both `host` and `hostaddr` are specified, the value for `hostaddr` gives the server network address.
58
+ /// The value for `host` is ignored unless the authentication method requires it,
59
+ /// in which case it will be used as the host name.
46
60
/// * `port` - The port to connect to. Multiple ports can be specified, separated by commas. The number of ports must be
47
61
/// either 1, in which case it will be used for all hosts, or the same as the number of hosts. Defaults to 5432 if
48
62
/// omitted or the empty string.
@@ -74,6 +88,10 @@ use tokio_postgres::{Error, Socket};
74
88
/// ```
75
89
///
76
90
/// ```not_rust
91
+ /// host=host1,host2,host3 port=1234,,5678 hostaddr=127.0.0.1,127.0.0.2,127.0.0.3 user=postgres target_session_attrs=read-write
92
+ /// ```
93
+ ///
94
+ /// ```not_rust
77
95
/// host=host1,host2,host3 port=1234,,5678 user=postgres target_session_attrs=read-write
78
96
/// ```
79
97
///
@@ -250,6 +268,7 @@ impl Config {
250
268
///
251
269
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
252
270
/// systems, a host starting with a `/` is interpreted as a path to a directory containing Unix domain sockets.
271
+ /// There must be either no hosts, or the same number of hosts as hostaddrs.
253
272
pub fn host ( & mut self , host : & str ) -> & mut Config {
254
273
self . config . host ( host) ;
255
274
self
@@ -260,6 +279,11 @@ impl Config {
260
279
self . config . get_hosts ( )
261
280
}
262
281
282
+ /// Gets the hostaddrs that have been added to the configuration with `hostaddr`.
283
+ pub fn get_hostaddrs ( & self ) -> & [ IpAddr ] {
284
+ self . config . get_hostaddrs ( )
285
+ }
286
+
263
287
/// Adds a Unix socket host to the configuration.
264
288
///
265
289
/// Unlike `host`, this method allows non-UTF8 paths.
@@ -272,6 +296,15 @@ impl Config {
272
296
self
273
297
}
274
298
299
+ /// Adds a hostaddr to the configuration.
300
+ ///
301
+ /// Multiple hostaddrs can be specified by calling this method multiple times, and each will be tried in order.
302
+ /// There must be either no hostaddrs, or the same number of hostaddrs as hosts.
303
+ pub fn hostaddr ( & mut self , hostaddr : IpAddr ) -> & mut Config {
304
+ self . config . hostaddr ( hostaddr) ;
305
+ self
306
+ }
307
+
275
308
/// Adds a port to the configuration.
276
309
///
277
310
/// Multiple ports can be specified by calling this method multiple times. There must either be no ports, in which
0 commit comments