pub(crate) async fn run_is_master(
    conn: &mut Connection,
    command: Command,
    topology: Option<&Topology>,
    handler: &Option<Arc<dyn SdamEventHandler>>,
) -> Result<IsMasterReply> {
    emit_event(topology, handler, |handler| {
        let event = ServerHeartbeatStartedEvent {
            server_address: conn.address.clone(),
        };
        handler.handle_server_heartbeat_started_event(event);
    });

    let start_time = Instant::now();
    let response_result = conn.send_command(command, None).await;
    let end_time = Instant::now();

    let round_trip_time = end_time.duration_since(start_time);

    match response_result.and_then(|raw_response| {
        let is_master_reply = raw_response.to_is_master_response(round_trip_time)?;
        Ok((raw_response, is_master_reply))
    }) {
        Ok((raw_response, is_master_reply)) => {
            emit_event(topology, handler, |handler| {
                let mut reply = raw_response
                    .body::<Document>()
                    .unwrap_or_else(|e| doc! { "deserialization error": e.to_string() });
                // if this isMaster call is part of a handshake, remove speculative authentication
                // information before publishing an event
                reply.remove("speculativeAuthenticate");
                let event = ServerHeartbeatSucceededEvent {
                    duration: round_trip_time,
                    reply,
                    server_address: conn.address.clone(),
                };
                handler.handle_server_heartbeat_succeeded_event(event);
            });
            Ok(is_master_reply)
        }
        Err(err) => {
            emit_event(topology, handler, |handler| {
                let event = ServerHeartbeatFailedEvent {
                    duration: round_trip_time,
                    failure: err.clone(),
                    server_address: conn.address.clone(),
                };
                handler.handle_server_heartbeat_failed_event(event);
            });
            Err(err)
        }
    }
}
/// Execute an isMaster command, emiting events if a reference to the topology and a handler are
/// provided.
///
/// A strong reference to the topology is used here to ensure it is still in scope and has not yet
/// emitted a `TopologyClosedEvent`.
Execute an isMaster command, emiting events if a reference to the topology and a handler are
provided.

A strong reference to the topology is used here to ensure it is still in scope and has not yet
emitted a `TopologyClosedEvent`.
Execute an isMaster command emiting events if a reference to the topology and a handler are provided . A strong reference to the topology is used here to ensure it is still in scope and has not yet emitted a ` TopologyClosedEvent ` .
pub(crate) async fn run_is_master(
    conn: &mut Connection,
    command: Command,
    topology: Option<&Topology>,
    handler: &Option<Arc<dyn SdamEventHandler>>,
) -> Result<IsMasterReply> {
    emit_event(topology, handler, |handler| {
        let event = ServerHeartbeatStartedEvent {
            server_address: conn.address.clone(),
        };
        handler.handle_server_heartbeat_started_event(event);
    });
    let start_time = Instant::now();
    let response_result = conn.send_command(command, None).await;
    let end_time = Instant::now();
    let round_trip_time = end_time.duration_since(start_time);
    match response_result.and_then(|raw_response| {
        let is_master_reply = raw_response.to_is_master_response(round_trip_time)?;
        Ok((raw_response, is_master_reply))
    }) {
        Ok((raw_response, is_master_reply)) => {
            emit_event(topology, handler, |handler| {
                let mut reply = raw_response
                    .body::<Document>()
                    .unwrap_or_else(|e| doc! { "deserialization error": e.to_string() });
                // if this isMaster call is part of a handshake, remove speculative authentication
                // information before publishing an event
                reply.remove("speculativeAuthenticate");
                let event = ServerHeartbeatSucceededEvent {
                    duration: round_trip_time,
                    reply,
                    server_address: conn.address.clone(),
                };
                handler.handle_server_heartbeat_succeeded_event(event);
            });
            Ok(is_master_reply)
        }
        Err(err) => {
            emit_event(topology, handler, |handler| {
                let event = ServerHeartbeatFailedEvent {
                    duration: round_trip_time,
                    failure: err.clone(),
                    server_address: conn.address.clone(),
                };
                handler.handle_server_heartbeat_failed_event(event);
            });
            Err(err)
        }
    }
}
pub ( crate ) async fn run_is_master ( conn : & mut Connection , command : Command , topology : Option < & Topology > , handler : & Option < Arc < dyn SdamEventHandler > > , ) -> Result < IsMasterReply > { emit_event ( topology , handler , | handler | { let event = ServerHeartbeatStartedEvent { server_address : conn . address . clone ( ) , } ; handler . handle_server_heartbeat_started_event ( event ) ; } ) ; let start_time = Instant :: now ( ) ; let response_result = conn . send_command ( command , None ) . await ; let end_time = Instant :: now ( ) ; let round_trip_time = end_time . duration_since ( start_time ) ; match response_result . and_then ( | raw_response | { let is_master_reply = raw_response . to_is_master_response ( round_trip_time ) ? ; Ok ( ( raw_response , is_master_reply ) ) } ) { Ok ( ( raw_response , is_master_reply ) ) => { emit_event ( topology , handler , | handler | { let mut reply = raw_response . body :: < Document > ( ) . unwrap_or_else ( | e | doc ! { " " e to_string ( ) } ) ; // if this isMaster call is part of a handshake, remove speculative authentication // information before publishing an event reply . remove ( " " ) ; let event = ServerHeartbeatSucceededEvent { duration : round_trip_time , reply , server_address : conn . address . clone ( ) , } ; handler . handle_server_heartbeat_succeeded_event ( event ) ; } ) ; Ok ( is_master_reply ) } Err ( err ) => { emit_event ( topology , handler , | handler | { let event = ServerHeartbeatFailedEvent { duration : round_trip_time , failure : err . clone ( ) , server_address : conn . address . clone ( ) , } ; handler . handle_server_heartbeat_failed_event ( event ) ; } ) ; Err ( err ) } } }
Execute an isMaster command, emiting events if a reference to the topology and a handler are
provided.
Execute an isMaster command emiting events if a reference to the topology and a handler are provided .
conn
&mut Connection
command
Command
topology
Option<&Topology>
handler
&Option<Arc<dyn SdamEventHandler>>
conn
&mut Connection
command
Command
topology
Option<&Topology>
handler
&Option<Arc<dyn SdamEventHandler>>