@@ -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 ;
@@ -39,6 +40,19 @@ use tokio_postgres::{Error, Socket};
39
40
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
40
41
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
41
42
/// with the `connect` method.
43
+ /// * `hostaddr` - Numeric IP address of host to connect to. This should be in the standard IPv4 address format,
44
+ /// e.g., 172.28.40.9. If your machine supports IPv6, you can also use those addresses.
45
+ /// If this parameter is not specified, the value of `host` will be looked up to find the corresponding IP address,
46
+ /// - or if host specifies an IP address, that value will be used directly.
47
+ /// Using `hostaddr` allows the application to avoid a host name look-up, which might be important in applications
48
+ /// with time constraints. However, a host name is required for verify-full SSL certificate verification.
49
+ /// Specifically:
50
+ /// * If `hostaddr` is specified without `host`, the value for `hostaddr` gives the server network address.
51
+ /// The connection attempt will fail if the authentication method requires a host name;
52
+ /// * If `host` is specified without `hostaddr`, a host name lookup occurs;
53
+ /// * If both `host` and `hostaddr` are specified, the value for `hostaddr` gives the server network address.
54
+ /// The value for `host` is ignored unless the authentication method requires it,
55
+ /// in which case it will be used as the host name.
42
56
/// * `port` - The port to connect to. Multiple ports can be specified, separated by commas. The number of ports must be
43
57
/// either 1, in which case it will be used for all hosts, or the same as the number of hosts. Defaults to 5432 if
44
58
/// omitted or the empty string.
@@ -67,6 +81,10 @@ use tokio_postgres::{Error, Socket};
67
81
/// ```
68
82
///
69
83
/// ```not_rust
84
+ /// 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
85
+ /// ```
86
+ ///
87
+ /// ```not_rust
70
88
/// host=host1,host2,host3 port=1234,,5678 user=postgres target_session_attrs=read-write
71
89
/// ```
72
90
///
@@ -204,6 +222,7 @@ impl Config {
204
222
///
205
223
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
206
224
/// systems, a host starting with a `/` is interpreted as a path to a directory containing Unix domain sockets.
225
+ /// There must be either no hosts, or the same number of hosts as hostaddrs.
207
226
pub fn host ( & mut self , host : & str ) -> & mut Config {
208
227
self . config . host ( host) ;
209
228
self
@@ -214,6 +233,11 @@ impl Config {
214
233
self . config . get_hosts ( )
215
234
}
216
235
236
+ /// Gets the hostaddrs that have been added to the configuration with `hostaddr`.
237
+ pub fn get_hostaddrs ( & self ) -> & [ IpAddr ] {
238
+ self . config . get_hostaddrs ( )
239
+ }
240
+
217
241
/// Adds a Unix socket host to the configuration.
218
242
///
219
243
/// Unlike `host`, this method allows non-UTF8 paths.
@@ -226,6 +250,15 @@ impl Config {
226
250
self
227
251
}
228
252
253
+ /// Adds a hostaddr to the configuration.
254
+ ///
255
+ /// Multiple hostaddrs can be specified by calling this method multiple times, and each will be tried in order.
256
+ /// There must be either no hostaddrs, or the same number of hostaddrs as hosts.
257
+ pub fn hostaddr ( & mut self , hostaddr : IpAddr ) -> & mut Config {
258
+ self . config . hostaddr ( hostaddr) ;
259
+ self
260
+ }
261
+
229
262
/// Adds a port to the configuration.
230
263
///
231
264
/// Multiple ports can be specified by calling this method multiple times. There must either be no ports, in which
0 commit comments