Skip to content

Commit cb726ba

Browse files
authored
RUST-926 Allow direct connections to hidden nodes (#423)
1 parent 1e19c88 commit cb726ba

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

src/client/executor.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::{
3131
SelectedServer,
3232
ServerType,
3333
SessionSupportStatus,
34+
TopologyType,
3435
TransactionSupportStatus,
3536
},
3637
selection_criteria::ReadPreference,
@@ -573,6 +574,17 @@ impl Client {
573574
}
574575
}
575576

577+
async fn select_data_bearing_server(&self) -> Result<()> {
578+
let topology_type = self.inner.topology.topology_type().await;
579+
let criteria = SelectionCriteria::Predicate(Arc::new(move |server_info| {
580+
let server_type = server_info.server_type();
581+
(matches!(topology_type, TopologyType::Single) && server_type.is_available())
582+
|| server_type.is_data_bearing()
583+
}));
584+
let _: SelectedServer = self.select_server(Some(&criteria)).await?;
585+
Ok(())
586+
}
587+
576588
/// Gets whether the topology supports sessions, and if so, returns the topology's logical
577589
/// session timeout. If it has yet to be determined if the topology supports sessions, this
578590
/// method will perform a server selection that will force that determination to be made.
@@ -583,10 +595,7 @@ impl Client {
583595
// sessions are supported or not.
584596
match initial_status {
585597
SessionSupportStatus::Undetermined => {
586-
let criteria = SelectionCriteria::Predicate(Arc::new(move |server_info| {
587-
server_info.server_type().is_data_bearing()
588-
}));
589-
let _: SelectedServer = self.select_server(Some(&criteria)).await?;
598+
self.select_data_bearing_server().await?;
590599
Ok(self.inner.topology.session_support_status().await)
591600
}
592601
_ => Ok(initial_status),
@@ -603,10 +612,7 @@ impl Client {
603612
// sessions are supported or not.
604613
match initial_status {
605614
TransactionSupportStatus::Undetermined => {
606-
let criteria = SelectionCriteria::Predicate(Arc::new(move |server_info| {
607-
server_info.server_type().is_data_bearing()
608-
}));
609-
let _: SelectedServer = self.select_server(Some(&criteria)).await?;
615+
self.select_data_bearing_server().await?;
610616
Ok(self.inner.topology.transaction_support_status().await)
611617
}
612618
_ => Ok(initial_status),

src/client/options/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub struct ClientOptions {
370370
///
371371
/// Note that by default, the driver will autodiscover other nodes in the cluster. To connect
372372
/// directly to a single server (rather than autodiscovering the rest of the cluster), set the
373-
/// `direct` field to `true`.
373+
/// `direct_connection` field to `true`.
374374
#[builder(default_code = "vec![ServerAddress::Tcp {
375375
host: \"localhost\".to_string(),
376376
port: Some(27017),

src/sdam/description/server.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ impl ServerType {
4747
| ServerType::LoadBalancer
4848
)
4949
}
50+
51+
pub(crate) fn is_available(self) -> bool {
52+
!matches!(self, ServerType::Unknown)
53+
}
5054
}
5155

5256
impl Default for ServerType {
@@ -185,7 +189,7 @@ impl ServerDescription {
185189

186190
/// Whether this server is "available" as per the definition in the server selection spec.
187191
pub(crate) fn is_available(&self) -> bool {
188-
!matches!(self.server_type, ServerType::Unknown)
192+
self.server_type.is_available()
189193
}
190194

191195
pub(crate) fn compatibility_error_message(&self) -> Option<String> {

src/sdam/description/topology/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ impl TopologyDescription {
272272
/// Updates the topology's logical session timeout value based on the server's value for it.
273273
fn update_session_support_status(&mut self, server_description: &ServerDescription) {
274274
if !server_description.server_type.is_data_bearing() {
275+
if let TopologyType::Single = self.topology_type {
276+
self.session_support_status = SessionSupportStatus::Unsupported {
277+
logical_session_timeout: None,
278+
};
279+
}
275280
return;
276281
}
277282

src/sdam/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub(crate) use self::{
1717
server_selection::SelectedServer,
1818
SessionSupportStatus,
1919
TopologyDescription,
20+
TopologyType,
2021
TransactionSupportStatus,
2122
},
2223
message_manager::TopologyMessageManager,

src/sdam/state/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl Topology {
440440
.transaction_support_status()
441441
}
442442

443-
pub(super) async fn topology_type(&self) -> TopologyType {
443+
pub(crate) async fn topology_type(&self) -> TopologyType {
444444
self.state.read().await.description.topology_type()
445445
}
446446

0 commit comments

Comments
 (0)