add a helper function for ActiveSession on_request to safely emit_message (#806)

Co-authored-by: turbofish <junwow@amazon.com>
This commit is contained in:
TurboFish
2023-01-11 02:04:23 -08:00
committed by GitHub
parent e846fbbb80
commit 16d506d21c

View File

@ -105,7 +105,7 @@ impl ActiveSession {
received: Instant::now(),
};
if self
.try_emit_message(PeerMessage::EthRequest(PeerRequest::$req_item {
.safe_emit_message(PeerMessage::EthRequest(PeerRequest::$req_item {
request,
response: tx,
}))
@ -255,6 +255,18 @@ impl ActiveSession {
});
}
/// Send a message back to the [`SessionsManager`]
/// covering both broadcasts and incoming requests
fn safe_emit_message(
&self,
message: PeerMessage,
) -> Result<(), mpsc::error::TrySendError<ActiveSessionMessage>> {
self.to_session
// we want this message to always arrive, so we clone the sender
.clone()
.try_send(ActiveSessionMessage::ValidMessage { peer_id: self.remote_peer_id, message })
}
/// Send a message back to the [`SessionsManager`]
fn try_emit_message(
&self,