Skip to content

Commit

Permalink
feat(swarm): allow both dial conditions combined
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Jul 20, 2023
1 parent b18c77e commit 5547acc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 7 additions & 3 deletions swarm/src/dial_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,18 @@ impl WithoutPeerIdWithAddress {
#[derive(Debug, Copy, Clone, Default)]
pub enum PeerCondition {
/// A new dialing attempt is initiated _only if_ the peer is currently
/// considered disconnected, i.e. there is no established connection
/// and no ongoing dialing attempt.
#[default]
/// considered disconnected, i.e. there is no established connection.
Disconnected,
/// A new dialing attempt is initiated _only if_ there is currently
/// no ongoing dialing attempt, i.e. the peer is either considered
/// disconnected or connected but without an ongoing dialing attempt.
NotDialing,
/// A combination of [`Disconnected`](PeerCondition::Disconnected) and
/// [`NotDialing`](PeerCondition::NotDialing). A new dialing attempt is
/// iniated _only if_ the peer is both considered disconnected and there
/// is currently no ongoing dialing attempt.
#[default]
DisconnectedAndNotDialing,
/// A new dialing attempt is always initiated, only subject to the
/// configured connection limits.
Always,
Expand Down
6 changes: 4 additions & 2 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,13 @@ where
let connection_id = dial_opts.connection_id();

let should_dial = match (condition, peer_id) {
(_, None) => true,
(PeerCondition::Always, _) => true,
(PeerCondition::Disconnected, None) => true,
(PeerCondition::NotDialing, None) => true,
(PeerCondition::Disconnected, Some(peer_id)) => !self.pool.is_connected(peer_id),
(PeerCondition::NotDialing, Some(peer_id)) => !self.pool.is_dialing(peer_id),
(PeerCondition::DisconnectedAndNotDialing, Some(peer_id)) => {
!self.pool.is_dialing(peer_id) || !self.pool.is_connected(peer_id)
}
};

if !should_dial {
Expand Down

0 comments on commit 5547acc

Please sign in to comment.