Skip to content

Commit 272b6c4

Browse files
authored
refactor(iroh, iroh-relay)!: Do reconnection in ActiveRelayActor (#3058)
## Description This moves the reconnection logic ingo ActiveRelayActor instead of doing this in the RelayActor. The logic itself is not really changed. This does simplify things a fair bit though, it is a lot easier to do this in one place where all the state needs to be. In the grand scheme of things this moves "logical" control over the relay Client entirely into the ActiveRelayActor. Some small side effects: - The ActiveRelayMessage::Ping and ActiveRelayMessage::GetLocalAddr messages are no longer needed. Though I decided to keep the latter for testing purposes. - ActiveRelayMessage::NotePreferred has been rename to SetHomeRelay. Since the ActiveRelayActor now reconnects to relays on its own it needs to keep track of whether it is the home relay itself as well. - The duplicate ping task tracking in the RelayActor is removed. This is still not ideal in the ActiveRelayActor because it still blocks this actor. But that's no regression and to be addressed when the relay Client is no longer an actor. - RelayActor::close_and_reconnect_relay and RelayActor::send_to_active_relay are no longer needed. Because we await all the ActiveRelayActor tasks already we do not need to manually track them in case they need to be closed. - The ClientBuilder was no longer using is_preferred, so remove this. ## Breaking Changes ### iroh-relay - `ClientBuilder::is_preferred` no longer exists. Set the preference once the client exists instead. ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## 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. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent dfa0a2c commit 272b6c4

File tree

4 files changed

+277
-133
lines changed

4 files changed

+277
-133
lines changed

iroh-relay/src/client.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@ impl PingTracker {
192192
/// Build a Client.
193193
#[derive(derive_more::Debug)]
194194
pub struct ClientBuilder {
195-
/// Default is false
196-
is_preferred: bool,
197195
/// Default is None
198196
#[debug("address family selector callback")]
199197
address_family_selector: Option<Box<dyn Fn() -> bool + Send + Sync>>,
@@ -218,7 +216,6 @@ impl ClientBuilder {
218216
/// Create a new [`ClientBuilder`]
219217
pub fn new(url: impl Into<RelayUrl>) -> Self {
220218
ClientBuilder {
221-
is_preferred: false,
222219
address_family_selector: None,
223220
is_prober: false,
224221
server_public_key: None,
@@ -258,13 +255,6 @@ impl ClientBuilder {
258255
self
259256
}
260257

261-
/// Indicate this client is the preferred way to communicate
262-
/// to the peer with this client's [`PublicKey`]
263-
pub fn is_preferred(mut self, is: bool) -> Self {
264-
self.is_preferred = is;
265-
self
266-
}
267-
268258
/// Indicates this client is a prober
269259
pub fn is_prober(mut self, is: bool) -> Self {
270260
self.is_prober = is;
@@ -320,7 +310,7 @@ impl ClientBuilder {
320310

321311
let inner = Actor {
322312
secret_key: key,
323-
is_preferred: self.is_preferred,
313+
is_preferred: false,
324314
relay_conn: None,
325315
is_closed: false,
326316
address_family_selector: self.address_family_selector,
@@ -432,6 +422,8 @@ impl Client {
432422

433423
/// Send a ping to the server. Return once we get an expected pong.
434424
///
425+
/// This has a built-in timeout `crate::defaults::timeouts::PING_TIMEOUT`.
426+
///
435427
/// There must be a task polling `recv_detail` to process the `pong` response.
436428
pub async fn ping(&self) -> Result<Duration, ClientError> {
437429
self.send_actor(ActorMessage::Ping).await

iroh-relay/src/client/conn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Conn {
9898
///
9999
/// Errors if the packet is larger than [`MAX_PACKET_SIZE`]
100100
pub async fn send(&self, dst: NodeId, packet: Bytes) -> Result<()> {
101-
trace!(%dst, len = packet.len(), "[RELAY] send");
101+
trace!(dst = dst.fmt_short(), len = packet.len(), "[RELAY] send");
102102

103103
self.inner
104104
.writer_channel

iroh-relay/src/server/actor.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ impl Actor {
142142
async fn handle_message(&mut self, msg: Message) {
143143
match msg {
144144
Message::SendPacket { dst, data, src } => {
145-
trace!(?src, ?dst, len = data.len(), "send packet");
145+
trace!(
146+
src = src.fmt_short(),
147+
dst = dst.fmt_short(),
148+
len = data.len(),
149+
"send packet"
150+
);
146151
if self.clients.contains_key(&dst) {
147152
match self.clients.send_packet(&dst, Packet { data, src }).await {
148153
Ok(()) => {

0 commit comments

Comments
 (0)