Skip to content

Commit

Permalink
feat: add IncomingConnections::try_recv()
Browse files Browse the repository at this point in the history
This enables a non-blocking method to check for incoming connections
and messages.

This can be useful when using multiple Endpoint in one process or
even one thread.
  • Loading branch information
dan-da authored and dirvine committed Feb 13, 2022
1 parent b7b76dd commit a7562e9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/endpoint.rs
Expand Up @@ -25,7 +25,7 @@ use std::{
sync::Arc,
};
use tokio::sync::broadcast::{self, Sender};
use tokio::sync::mpsc::{self, Receiver as MpscReceiver};
use tokio::sync::mpsc::{self, error::TryRecvError, Receiver as MpscReceiver};
use tokio::time::{timeout, Duration};
use tracing::{error, info, trace, warn};

Expand All @@ -49,6 +49,12 @@ impl IncomingConnections {
pub async fn next(&mut self) -> Option<(Connection, ConnectionIncoming)> {
self.0.recv().await
}

/// Non-blocking method to receive the next incoming connection if present.
/// See tokio::sync::mpsc::Receiver::try_recv()
pub fn try_recv(&mut self) -> Result<(Connection, ConnectionIncoming), TryRecvError> {
self.0.try_recv()
}
}

/// Endpoint instance which can be used to communicate with peers.
Expand Down

0 comments on commit a7562e9

Please sign in to comment.