Skip to content

Commit

Permalink
feat(iroh-net): allow customizing republish delay for the pkarr publi…
Browse files Browse the repository at this point in the history
…sher (#2637)

## Description

The pkarr dht publisher has two delays, the initial delay and the
republish delay. These were constants so far, but now are configurable
in the builder.

This allows us to set the delay to 0s in the dht_discovery_smoke test.

## Breaking Changes

None

## Notes & open questions

Will this fix the flakiness?

## Change checklist

- [x] Self-review.
- [x] Documentation updates following the [style
guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text),
if relevant.
- [ ] Tests if relevant.
- [x] All breaking changes documented.

Co-authored-by: Friedel Ziegelmayer <[email protected]>
  • Loading branch information
rklaehn and dignifiedquire authored Aug 19, 2024
1 parent b17bf1d commit 134dbee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
26 changes: 5 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion iroh-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ iroh = { version = "0.22.0", path = "../iroh", features = ["metrics"] }
iroh-gossip = { version = "0.22.0", path = "../iroh-gossip" }
iroh-metrics = { version = "0.22.0", path = "../iroh-metrics" }
parking_lot = "0.12.1"
pkarr = { version = "1.1.5", default-features = false }
pkarr = { version = "2.0.0", default-features = false }
portable-atomic = "1"
postcard = "1.0.8"
quic-rpc = { version = "0.11", features = ["flume-transport", "quinn-transport"] }
Expand Down
2 changes: 1 addition & 1 deletion iroh-cli/src/commands/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ fn bold<T: Display>(x: T) -> String {
}

fn to_z32(node_id: NodeId) -> String {
pkarr::PublicKey::try_from(*node_id.as_bytes())
pkarr::PublicKey::try_from(node_id.as_bytes())
.unwrap()
.to_z32()
}
Expand Down
31 changes: 27 additions & 4 deletions iroh-net/src/discovery/pkarr/dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ struct Inner {
ttl: u32,
/// True to include the direct addresses in the DNS packet.
include_direct_addresses: bool,
/// Initial delay before the first publish.
initial_publish_delay: Duration,
/// Republish delay for the DHT.
republish_delay: Duration,
}

/// Builder for PkarrNodeDiscovery.
Expand All @@ -86,6 +90,8 @@ pub struct Builder {
pkarr_relay: Option<Url>,
dht: bool,
include_direct_addresses: bool,
initial_publish_delay: Duration,
republish_delay: Duration,
}

impl Default for Builder {
Expand All @@ -97,6 +103,8 @@ impl Default for Builder {
pkarr_relay: None,
dht: true,
include_direct_addresses: false,
initial_publish_delay: INITIAL_PUBLISH_DELAY,
republish_delay: REPUBLISH_DELAY,
}
}
}
Expand Down Expand Up @@ -146,6 +154,18 @@ impl Builder {
self
}

/// Set the initial delay before the first publish.
pub fn initial_publish_delay(mut self, initial_publish_delay: Duration) -> Self {
self.initial_publish_delay = initial_publish_delay;
self
}

/// Set the republish delay for the DHT.
pub fn republish_delay(mut self, republish_delay: Duration) -> Self {
self.republish_delay = republish_delay;
self
}

/// Build the discovery mechanism.
pub fn build(self) -> anyhow::Result<DhtDiscovery> {
let pkarr = self
Expand Down Expand Up @@ -175,11 +195,13 @@ impl Builder {
Ok(DhtDiscovery(Arc::new(Inner {
pkarr,
pkarr_relay,
secret_key: self.secret_key,
ttl,
relay_url,
dht,
include_direct_addresses,
secret_key: self.secret_key,
initial_publish_delay: self.initial_publish_delay,
republish_delay: self.republish_delay,
task: Default::default(),
})))
}
Expand All @@ -199,7 +221,7 @@ impl DhtDiscovery {
.to_z32();
// initial delay. If the task gets aborted before this delay is over,
// we have not published anything to the DHT yet.
tokio::time::sleep(INITIAL_PUBLISH_DELAY).await;
tokio::time::sleep(this.0.initial_publish_delay).await;
loop {
// publish to the DHT if enabled
let dht_publish = async {
Expand Down Expand Up @@ -240,7 +262,7 @@ impl DhtDiscovery {
};
// do both at the same time
tokio::join!(relay_publish, dht_publish);
tokio::time::sleep(REPUBLISH_DELAY).await;
tokio::time::sleep(this.0.republish_delay).await;
}
}

Expand Down Expand Up @@ -378,7 +400,7 @@ mod tests {
let _ = tracing_subscriber::fmt::try_init();
let ep = crate::Endpoint::builder().bind(0).await?;
let secret = ep.secret_key().clone();
let testnet = mainline::dht::Testnet::new(5);
let testnet = mainline::dht::Testnet::new(2);
let settings = pkarr::Settings {
dht: DhtSettings {
bootstrap: Some(testnet.bootstrap.clone()),
Expand All @@ -389,6 +411,7 @@ mod tests {
let client = PkarrClient::new(settings)?;
let discovery = DhtDiscovery::builder()
.secret_key(secret.clone())
.initial_publish_delay(Duration::ZERO)
.client(client)
.build()?;
let relay_url: RelayUrl = Url::parse("https://example.com")?.into();
Expand Down

0 comments on commit 134dbee

Please sign in to comment.