use std::time::Duration;
use crate::{is_master::IsMasterReply, sdam::ServerType};
#[derive(Debug, Default, Clone)]
pub(crate) struct StreamDescription {
pub(crate) initial_server_type: ServerType,
pub(crate) max_wire_version: Option<i32>,
pub(crate) min_wire_version: Option<i32>,
pub(crate) sasl_supported_mechs: Option<Vec<String>>,
pub(crate) logical_session_timeout: Option<Duration>,
}
impl StreamDescription {
pub(crate) fn from_is_master(reply: IsMasterReply) -> Self {
Self {
initial_server_type: reply.command_response.server_type(),
max_wire_version: reply.command_response.max_wire_version,
min_wire_version: reply.command_response.min_wire_version,
sasl_supported_mechs: reply.command_response.sasl_supported_mechs,
logical_session_timeout: reply
.command_response
.logical_session_timeout_minutes
.map(|mins| Duration::from_secs(mins as u64 * 60)),
}
}
pub(crate) fn supports_retryable_writes(&self) -> bool {
self.initial_server_type != ServerType::Standalone
&& self.logical_session_timeout.is_some()
&& self.max_wire_version.map_or(false, |version| version >= 6)
}
#[cfg(test)]
pub(crate) fn new_testing() -> Self {
Self {
initial_server_type: Default::default(),
max_wire_version: Some(8),
min_wire_version: Some(8),
sasl_supported_mechs: Default::default(),
logical_session_timeout: Some(Duration::from_secs(30 * 60)),
}
}
}