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

refactor(iroh-net): Proper abstraction around best_addr #1675

Merged
merged 7 commits into from
Oct 23, 2023

Conversation

Frando
Copy link
Member

@Frando Frando commented Oct 19, 2023

Description

This refactors how we use and calculate best_addr. It should not have any functional changes, only refactoring to make things more robust for the future.

Notes & open questions

Change checklist

  • Self-review.
  • Documentation updates if relevant.
  • Tests if relevant.

@Frando Frando changed the title refactor(iroh-net): Proper abstraction around best_addr (WIP) refactor(iroh-net): Proper abstraction around best_addr Oct 19, 2023
@b5 b5 added this to the v0.8.0 milestone Oct 19, 2023
@Frando Frando changed the base branch from net-less-channels to main October 19, 2023 15:59
@Frando Frando changed the title (WIP) refactor(iroh-net): Proper abstraction around best_addr refactor(iroh-net): Proper abstraction around best_addr Oct 19, 2023
Copy link
Contributor

@dignifiedquire dignifiedquire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but should get review from @divagant-martian before merging

Copy link
Contributor

@divagant-martian divagant-martian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall I think it's a great improvement, and moving it to another file seems like the right call to me. I found a bug -not caused by your changes- but that we should address, and otherwise just some comments about info we lost in logging

trust_for = ?trust_best_addr_until.duration_since(Instant::now()),
"new best_addr (candidate address with most recent pong)"
self.best_addr.insert(
pong.from.as_socket_addr(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this matches the previous code but I think this is wrong. The docs for best_addr state that it's

/// Best non-DERP path.

as well as the docs of the new function

/// (...) with the candidate udp addr (..)

but as_socket_addr maps derp addresses to fake socket addresses. In reality as_socket_addr is a bit of a footgun. Fixing this goes beyond a simple refactor so if you think this should go in another pr I'd agree with that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, true that SendAddr::as_socket_addr maps Derp addresses to fake addresses. However, in this place I think it is correct (even though not enforced on the type level), because we only use pongs and thus SendAddrs out of Endpoint::direct_addr_state, which should only ever contain pongs for direct (UDP) addresses. So I do think that this is correct here, unless I'm missing something.

I already thought of abstracing the direct addr state in a similar manner to this PR.

In any case - if the behavior is the same as it is on main, let's leave this to a followup and verify there that no fake Derp addresses can ever end up in the best_addr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, I fixed that in fact. Probably the reason why it's still there is because we store a pong for the derper (in the derp_region) as well

iroh-net/src/magicsock/endpoint.rs Outdated Show resolved Hide resolved
iroh-net/src/magicsock/endpoint.rs Outdated Show resolved Hide resolved
iroh-net/src/magicsock/endpoint.rs Outdated Show resolved Hide resolved
@@ -0,0 +1,202 @@
//! The [`BestAddr`] is the currently active best address for UDP sends.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is not pub but it would be good to document its contents for us

iroh-net/src/magicsock/endpoint.rs Show resolved Hide resolved
iroh-net/src/magicsock/endpoint.rs Show resolved Hide resolved
iroh-net/src/magicsock/endpoint.rs Show resolved Hide resolved
iroh-net/src/magicsock/endpoint.rs Outdated Show resolved Hide resolved
@@ -153,8 +150,9 @@ impl Endpoint {

/// Returns info about this endpoint
pub fn info(&self) -> EndpointInfo {
let (conn_type, latency) = if self.is_best_addr_valid(Instant::now()) {
let addr_info = self.best_addr.as_ref().expect("checked");
// TODO: I think we should return direct here for outdated.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me it makes sense what you say. When it's outdated we return both this address and the derp and metrics are only updated when the address is cleared. So even there, we are counting an outdated conn as a a direct one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it so that we return Direct if best_addr is set but expired.

Actually the more proper fix might be to return something like ConnectionType::Both because if the best_addr is set but expired, we send over both Derp and the Udp address. But not sure how important this is to report to the outside.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the most recent commit I changed this to return ConnectionType::Mixed in that case. Also put it all into a single match statement for clarity.

@Frando Frando added this pull request to the merge queue Oct 23, 2023
Merged via the queue into main with commit 7baff93 Oct 23, 2023
15 checks passed
@dignifiedquire dignifiedquire deleted the refactor-best-addr branch October 23, 2023 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants