Skip to content

Commit

Permalink
gracefull drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Apr 14, 2024
1 parent 9d7f3cb commit d2633d5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ starknet = "0.9.0"
tempfile = "3.10.1"
thiserror = "1.0.58"
tokio = { version = "1.36", features = ["full"] }
tokio-util = "0.7.10"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

Expand Down
1 change: 1 addition & 0 deletions crates/peer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ futures.workspace = true
libp2p.workspace = true
sharp-p2p-common.workspace = true
starknet.workspace = true
tokio-util.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
14 changes: 12 additions & 2 deletions crates/peer/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use libp2p::{mdns, noise, tcp, yamux, Swarm, SwarmBuilder};
use std::error::Error;
use std::pin::Pin;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error};

#[derive(NetworkBehaviour)]
Expand All @@ -18,6 +19,7 @@ struct PeerBehaviour {

pub struct SwarmRunner {
swarm: Swarm<PeerBehaviour>,
cancellation_token: CancellationToken,
}

impl SwarmRunner {
Expand All @@ -43,7 +45,7 @@ impl SwarmRunner {
swarm.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

Ok(SwarmRunner { swarm })
Ok(SwarmRunner { swarm, cancellation_token: CancellationToken::new() })
}

fn init_gossip(
Expand Down Expand Up @@ -106,10 +108,18 @@ impl SwarmRunner {
}
_ => {}
},
else => break
_ = self.cancellation_token.cancelled() => {
break
}
}
}
};
Box::pin(stream)
}
}

impl Drop for SwarmRunner {
fn drop(&mut self) {
self.cancellation_token.cancel();
}
}

0 comments on commit d2633d5

Please sign in to comment.