Skip to content

Commit 113f500

Browse files
committed
node: adjust resolve_hostname to functional programming
Notice that using `find_or_last` is equivalent to previous version.
1 parent 104236b commit 113f500

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

scylla/src/transport/node.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use itertools::Itertools;
12
use tokio::net::lookup_host;
23
use tracing::warn;
34
use uuid::Uuid;
@@ -270,7 +271,6 @@ pub(crate) struct ResolvedContactPoint {
270271
// The resolution may return multiple IPs and the function returns one of them.
271272
// It prefers to return IPv4s first, and only if there are none, IPv6s.
272273
pub(crate) async fn resolve_hostname(hostname: &str) -> Result<SocketAddr, io::Error> {
273-
let mut ret = None;
274274
let addrs = match lookup_host(hostname).await {
275275
Ok(addrs) => itertools::Either::Left(addrs),
276276
// Use a default port in case of error, but propagate the original error on failure
@@ -279,21 +279,15 @@ pub(crate) async fn resolve_hostname(hostname: &str) -> Result<SocketAddr, io::E
279279
itertools::Either::Right(addrs)
280280
}
281281
};
282-
for a in addrs {
283-
match a {
284-
SocketAddr::V4(_) => return Ok(a),
285-
_ => {
286-
ret = Some(a);
287-
}
288-
}
289-
}
290282

291-
ret.ok_or_else(|| {
292-
io::Error::new(
293-
io::ErrorKind::Other,
294-
format!("Empty address list returned by DNS for {}", hostname),
295-
)
296-
})
283+
addrs
284+
.find_or_last(|addr| matches!(addr, SocketAddr::V4(_)))
285+
.ok_or_else(|| {
286+
io::Error::new(
287+
io::ErrorKind::Other,
288+
format!("Empty address list returned by DNS for {}", hostname),
289+
)
290+
})
297291
}
298292

299293
/// Transforms the given [`InternalKnownNode`]s into [`ContactPoint`]s.

0 commit comments

Comments
 (0)