Skip to content

Commit

Permalink
Work around libp2p bug where a node cannot publish to a topic if rest…
Browse files Browse the repository at this point in the history
…arted too quickly

See libp2p/rust-libp2p#5097
  • Loading branch information
romac committed Oct 16, 2024
1 parent 1e36445 commit 907ee98
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions code/crates/gossip-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use futures::StreamExt;
use libp2p::metrics::{Metrics, Recorder};
use libp2p::request_response::InboundRequestId;
use libp2p::swarm::{self, SwarmEvent};
use libp2p::{gossipsub, identify, SwarmBuilder};
use libp2p::{gossipsub, identify, quic, SwarmBuilder};
use libp2p_broadcast as broadcast;
use tokio::sync::mpsc;
use tracing::{debug, error, error_span, trace, Instrument};
Expand Down Expand Up @@ -66,9 +66,18 @@ pub struct Config {
}

impl Config {
fn apply(&self, cfg: swarm::Config) -> swarm::Config {
fn apply_to_swarm(&self, cfg: swarm::Config) -> swarm::Config {
cfg.with_idle_connection_timeout(self.idle_connection_timeout)
}

fn apply_to_quic(&self, mut cfg: quic::Config) -> quic::Config {
// NOTE: This is set low due to quic transport not properly resetting
// connection state when reconnecting before connection timeout.
// See https://github.com/libp2p/rust-libp2p/issues/5097
cfg.max_idle_timeout = 300;
cfg.keep_alive_interval = Duration::from_millis(100);
cfg
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -125,14 +134,14 @@ pub async fn spawn(
.with_dns()?
.with_bandwidth_metrics(registry)
.with_behaviour(|kp| Behaviour::new_with_metrics(config.protocol, kp, registry))?
.with_swarm_config(|cfg| config.apply(cfg))
.with_swarm_config(|cfg| config.apply_to_swarm(cfg))
.build()),
TransportProtocol::Quic => Ok(builder
.with_quic()
.with_quic_config(|cfg| config.apply_to_quic(cfg))
.with_dns()?
.with_bandwidth_metrics(registry)
.with_behaviour(|kp| Behaviour::new_with_metrics(config.protocol, kp, registry))?
.with_swarm_config(|cfg| config.apply(cfg))
.with_swarm_config(|cfg| config.apply_to_swarm(cfg))
.build()),
}
})?;
Expand Down

0 comments on commit 907ee98

Please sign in to comment.