I have this bit of code.
struct IrohDialer {
url: Url,
endpoint: iroh::Endpoint,
endpoint_id: iroh::PublicKey,
to_addr: iroh::EndpointAddr,
conn: Arc<std::sync::Mutex<Option<Arc<iroh::endpoint::Connection>>>>,
}
impl Dialer for IrohDialer {
fn url(&self) -> Url {
self.url.clone()
}
fn connect(
&self,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = Result<Transport, Box<dyn std::error::Error + Send + Sync>>,
> + Send,
>,
> {
let endpoint = self.endpoint.clone();
let addr = self.to_addr.clone();
let endpoint_id = self.endpoint_id;
let conn = Arc::clone(&self.conn);
Box::pin(async move {
let cloned = conn.lock().expect(ERROR_MUTEX).clone();
if let Some(conn) = cloned {
if let Some(close_reason) = conn.close_reason() {
error!(?close_reason, "connection was closed");
match close_reason {
iroh::endpoint::ConnectionError::ApplicationClosed(application_close) => {
if application_close.error_code == 220_u32.into() {
warn!("the peer signalled that they're shutting down");
// FIXME: abort dials
}
}
iroh::endpoint::ConnectionError::LocallyClosed => {
debug!("we're shutting down locally");
// FIXME: abort dials
}
_ => {}
}
} else {
warn!("re-dialing on a still open connection??");
let (tx, rx) = conn.open_bi().await?;
return Ok(Transport::new(
FramedRead::new(rx, Codec::new(endpoint_id)),
FramedWrite::new(tx, Codec::new(endpoint_id)),
));
}
}
let new_conn = endpoint.connect(addr, BigRepo::SYNC_ALPN).await?;
let new_conn = Arc::new(new_conn);
let (tx, rx) = new_conn.open_bi().await?;
conn.lock().expect(ERROR_MUTEX).replace(new_conn);
Ok(Transport::new(
FramedRead::new(rx, Codec::new(endpoint_id)),
FramedWrite::new(tx, Codec::new(endpoint_id)),
))
})
}
}
Maybe I'm holding it wrong but dialer needs a way to signal that it's not shouldn't to do any more dials. An alternative solution here would be to use another option mutex to pass the dialer handle and close it.
I have this bit of code.
Maybe I'm holding it wrong but dialer needs a way to signal that it's not shouldn't to do any more dials. An alternative solution here would be to use another option mutex to pass the dialer handle and close it.