Skip to content

Commit 7e73e25

Browse files
authored
Merge pull request #1108 from muzarski/remove-needless-clone-in-host-resolution
node: tiny optimizations and code refactors
2 parents 0e0b754 + 81d9576 commit 7e73e25

File tree

2 files changed

+30
-35
lines changed

2 files changed

+30
-35
lines changed

scylla/src/transport/connection_pool.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ impl PoolRefiller {
988988
// `last_error` must not be `None` if there is a possibility of the pool
989989
// being empty.
990990
fn update_shared_conns(&mut self, last_error: Option<ConnectionError>) {
991-
let new_conns = if !self.has_connections() {
991+
let new_conns = if self.is_empty() {
992992
Arc::new(MaybePoolConnections::Broken(last_error.unwrap()))
993993
} else {
994994
let new_conns = if let Some(sharder) = self.sharder.as_ref() {
@@ -1046,7 +1046,7 @@ impl PoolRefiller {
10461046
self.conns[shard_id].len(),
10471047
self.active_connection_count(),
10481048
);
1049-
if !self.has_connections() {
1049+
if self.is_empty() {
10501050
let _ = self.pool_empty_notifier.send(());
10511051
}
10521052
self.update_shared_conns(Some(last_error));
@@ -1152,10 +1152,6 @@ impl PoolRefiller {
11521152
);
11531153
}
11541154

1155-
fn has_connections(&self) -> bool {
1156-
self.conns.iter().any(|v| !v.is_empty())
1157-
}
1158-
11591155
fn active_connection_count(&self) -> usize {
11601156
self.conns.iter().map(Vec::len).sum::<usize>()
11611157
}

scylla/src/transport/node.rs

Lines changed: 28 additions & 29 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,27 +271,23 @@ 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;
274-
let addrs: Vec<SocketAddr> = match lookup_host(hostname).await {
275-
Ok(addrs) => addrs.collect(),
274+
let addrs = match lookup_host(hostname).await {
275+
Ok(addrs) => itertools::Either::Left(addrs),
276276
// Use a default port in case of error, but propagate the original error on failure
277-
Err(e) => lookup_host((hostname, 9042)).await.or(Err(e))?.collect(),
278-
};
279-
for a in addrs {
280-
match a {
281-
SocketAddr::V4(_) => return Ok(a),
282-
_ => {
283-
ret = Some(a);
284-
}
277+
Err(e) => {
278+
let addrs = lookup_host((hostname, 9042)).await.or(Err(e))?;
279+
itertools::Either::Right(addrs)
285280
}
286-
}
281+
};
287282

288-
ret.ok_or_else(|| {
289-
io::Error::new(
290-
io::ErrorKind::Other,
291-
format!("Empty address list returned by DNS for {}", hostname),
292-
)
293-
})
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+
})
294291
}
295292

296293
/// Transforms the given [`InternalKnownNode`]s into [`ContactPoint`]s.
@@ -323,18 +320,20 @@ pub(crate) async fn resolve_contact_points(
323320
}) => to_resolve.push((hostname, Some(datacenter.clone()))),
324321
};
325322
}
326-
let resolve_futures = to_resolve.iter().map(|(hostname, datacenter)| async move {
327-
match resolve_hostname(hostname).await {
328-
Ok(address) => Some(ResolvedContactPoint {
329-
address,
330-
datacenter: datacenter.clone(),
331-
}),
332-
Err(e) => {
333-
warn!("Hostname resolution failed for {}: {}", hostname, &e);
334-
None
323+
let resolve_futures = to_resolve
324+
.into_iter()
325+
.map(|(hostname, datacenter)| async move {
326+
match resolve_hostname(hostname).await {
327+
Ok(address) => Some(ResolvedContactPoint {
328+
address,
329+
datacenter,
330+
}),
331+
Err(e) => {
332+
warn!("Hostname resolution failed for {}: {}", hostname, &e);
333+
None
334+
}
335335
}
336-
}
337-
});
336+
});
338337
let resolved: Vec<_> = futures::future::join_all(resolve_futures).await;
339338
initial_peers.extend(resolved.into_iter().flatten());
340339

0 commit comments

Comments
 (0)