Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not bind a mainline DHT socket #2296

Merged
merged 1 commit into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions iroh-net/src/discovery/pkarr_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::sync::Arc;

use anyhow::Result;
use anyhow::{anyhow, bail, Result};
use pkarr::SignedPacket;
use tokio::{
task::JoinHandle,
Expand Down Expand Up @@ -135,7 +135,7 @@ impl PublisherService {
loop {
if let Some(info) = self.watcher.get() {
if let Err(err) = self.publish_current(info).await {
warn!(?err, url = %self.pkarr_client.pkarr_relay , "Failed to publish to pkarr");
warn!(?err, url = %self.pkarr_client.pkarr_relay_url , "Failed to publish to pkarr");
failed_attemps += 1;
// Retry after increasing timeout
republish
Expand Down Expand Up @@ -177,24 +177,40 @@ impl PublisherService {
/// A pkarr client to publish [`pkarr::SignedPacket`]s to a pkarr relay.
#[derive(Debug, Clone)]
pub struct PkarrRelayClient {
inner: pkarr::PkarrClient,
pkarr_relay: Url,
http_client: reqwest::Client,
pkarr_relay_url: Url,
}

impl PkarrRelayClient {
/// Create a new client.
pub fn new(pkarr_relay: Url) -> Self {
pub fn new(pkarr_relay_url: Url) -> Self {
Self {
inner: pkarr::PkarrClient::builder().build(),
pkarr_relay,
http_client: reqwest::Client::new(),
pkarr_relay_url,
}
}

/// Publish a [`SignedPacket`]
pub async fn publish(&self, signed_packet: &SignedPacket) -> anyhow::Result<()> {
self.inner
.relay_put(&self.pkarr_relay, signed_packet)
let mut url = self.pkarr_relay_url.clone();
url.path_segments_mut()
.map_err(|_| anyhow!("Failed to publish: Invalid relay URL"))?
.push(&signed_packet.public_key().to_z32());

let response = self
.http_client
.put(url)
.body(signed_packet.as_relay_request())
.send()
.await?;

if !response.status().is_success() {
bail!(format!(
"Publish request failed with status {}",
response.status()
))
}

Ok(())
}
}
Loading