Skip to content
Merged
Show file tree
Hide file tree
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
157 changes: 157 additions & 0 deletions crates/mousse-helper/tests/app_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,39 @@ impl Resolver for Answers {
}
}

/// A resolver that answers `answer` after `after`.
struct Late {
after: Duration,
answer: Vec<SocketAddr>,
}

impl Resolver for Late {
fn resolve<'a>(&'a self, _name: &'a str, _port: u16) -> ResolveFuture<'a> {
Box::pin(async move {
tokio::time::sleep(self.after).await;
Ok(self.answer.clone())
})
}
}

/// A resolver that answers each name after its own wait.
struct ByName(Vec<(&'static str, Duration, Vec<SocketAddr>)>);

impl Resolver for ByName {
fn resolve<'a>(&'a self, name: &'a str, _port: u16) -> ResolveFuture<'a> {
let (after, answer) = self
.0
.iter()
.find(|(n, _, _)| *n == name)
.map(|(_, after, answer)| (*after, answer.clone()))
.unwrap_or((Duration::ZERO, Vec::new()));
Box::pin(async move {
tokio::time::sleep(after).await;
Ok(answer)
})
}
}

async fn within<T>(what: &str, limit: Duration, fut: impl std::future::Future<Output = T>) -> T {
tokio::time::timeout(limit, fut)
.await
Expand Down Expand Up @@ -828,6 +861,130 @@ async fn a_name_is_ranked_by_the_address_it_resolves_to() {
);
}

/// A name that is still being resolved may be LAN, so it holds a ready
/// tailnet candidate back for the LAN head start, and no longer: a slow
/// lookup of an unrelated name does not delay the tailnet until it ends.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_slow_name_holds_the_tailnet_back_for_the_lan_head_start_only() {
let e = engine().await;
let (tail, _) = relay(e.addr).await;
let mut caller = granted(
&e,
Arc::new(ManualClock::new()),
&[("LAN_HEAD_START_MS", 1_000), ("DIAL_TIMEOUT_MS", 10_000)],
);
with_tailnet(&mut caller, &[tail]);
caller.use_resolver(Arc::new(Late {
after: Duration::from_secs(60),
answer: Vec::new(),
}));
let named = route("tcp://slow.test:31753");
let t0 = Instant::now();
let (_conn, reached) = within(
"the search",
Duration::from_secs(15),
caller.search(Search::of(vec![tcp(tail), named]), e.host.identity()),
)
.await
.unwrap();
let took = t0.elapsed();
assert_eq!(reached.route, tcp(tail));
assert_eq!(reached.class, RouteClass::Tailnet);
assert!(
took >= Duration::from_secs(1),
"no LAN head start: {took:?}"
);
assert!(
took < Duration::from_secs(5),
"waited for the name: {took:?}"
);
}

/// A name that resolves to the LAN within the head start still keeps the
/// LAN's place: the faster tailnet candidate is never dialled.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_name_resolved_to_the_lan_within_the_head_start_beats_the_tailnet() {
let e = engine().await;
let (lan, _) = slow_relay(e.addr, Duration::from_millis(300)).await;
let (tail, tail_taken) = relay(e.addr).await;
let mut caller = granted(
&e,
Arc::new(ManualClock::new()),
&[("LAN_HEAD_START_MS", 3_000)],
);
with_tailnet(&mut caller, &[tail]);
caller.use_resolver(Arc::new(Late {
after: Duration::from_millis(300),
answer: vec![lan],
}));
let named = route(&format!("tcp://desk.test:{}", lan.port()));
let (_conn, reached) = within(
"the search",
Duration::from_secs(15),
caller.search(
Search::of(vec![tcp(tail), named.clone()]),
e.host.identity(),
),
)
.await
.unwrap();
assert_eq!(reached.route, named);
assert_eq!(reached.class, RouteClass::Lan);
assert_eq!(
tail_taken.load(Ordering::SeqCst),
0,
"the tailnet was dialled"
);
}

/// Only numeric tailnet candidates skip the wait for names being resolved.
/// A name resolved to the tailnet may also hold public addresses, so it
/// waits for every name: its public address is not dialled while an
/// unrelated name is still being resolved.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_name_resolved_to_the_tailnet_waits_for_the_other_names() {
let e = engine().await;
// A tailnet address that refuses, then a public one that answers.
let dead = {
let gone = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
gone.local_addr().unwrap()
};
let (public, public_taken) = relay(e.addr).await;
let mut caller = granted(
&e,
Arc::new(ManualClock::new()),
&[("LAN_HEAD_START_MS", 500), ("DIAL_TIMEOUT_MS", 10_000)],
);
caller.use_addr_policy(
AddrPolicy::debug()
.with_class_map(&format!("{}=tailnet,{}=public", dead.port(), public.port()))
.unwrap(),
);
caller.use_resolver(Arc::new(ByName(vec![
("mixed.test", Duration::ZERO, vec![dead, public]),
("slow.test", Duration::from_secs(4), Vec::new()),
])));
let mixed = route(&format!("tcp://mixed.test:{}", public.port()));
let slow = route("tcp://slow.test:31753");
let t0 = Instant::now();
let (_conn, reached) = within(
"the search",
Duration::from_secs(15),
caller.search(Search::of(vec![mixed.clone(), slow]), e.host.identity()),
)
.await
.unwrap();
let took = t0.elapsed();
assert_eq!(reached.route, mixed);
assert_eq!(public_taken.load(Ordering::SeqCst), 1);
// The slow name resolves to nothing after 4 s; only then may the mixed
// name, and so its public address, be dialled.
assert!(
took >= Duration::from_millis(3_900),
"dialled before the other name was done: {took:?}"
);
}

/// `ws` candidates start TIER_HEAD_START_MS after `tcp`, with the address
/// the caller classified, and the ws pipe carries the session.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
Expand Down
29 changes: 23 additions & 6 deletions crates/mousse-net-io/src/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@
//! join as they arrive) still fit. LAN starts at once, `DIAL_PARALLEL`
//! tries at a time per class, each `DIAL_TIMEOUT_MS`. Tailnet starts
//! `LAN_HEAD_START_MS` later when a LAN candidate was known at the start,
//! or a name resolved to a LAN address before it started (at once
//! otherwise: a browse still running never holds it back). Classes after
//! LAN wait while names are being resolved.
//! a name was still being resolved (it may be LAN), or a name resolved
//! to a LAN address before it started (at once otherwise: a browse still
//! running never holds it back). A name still being resolved after that
//! head start no longer holds numeric tailnet candidates back; a name
//! resolved to the tailnet (it may also hold public addresses), public,
//! tunnel and ssh wait while names are being resolved.
//! Public and tunnel candidates start `TIER_HEAD_START_MS` after
//! tailnet (at once with no LAN or tailnet candidate), each `ws` try
//! `WS_DIAL_TIMEOUT_MS`. `ssh` runs one try at a time, each
Expand Down Expand Up @@ -1352,7 +1355,14 @@ where
|| later.is_some();
let mut at = [
start,
tailnet_at,
// A name being resolved may be LAN: it holds numeric tailnet
// candidates back for the LAN's head start at most, or less once
// it is resolved and every LAN candidate is done.
if ranking.is_empty() {
tailnet_at
} else {
start + lan_head
},
if nearer { tailnet_at + head } else { start },
start + ms(io.dial_fast_tiers_ms()),
];
Expand Down Expand Up @@ -1380,8 +1390,15 @@ where
}
for t in 0..CLASSES {
let cap = if t == 3 { 1 } else { parallel };
// Names being resolved may be LAN: the others wait for them.
while now >= at[t] && running[t] < cap && (t == 0 || ranking.is_empty()) {
// Names being resolved may be LAN: the others wait for them,
// except numeric tailnet candidates after the start above. A
// name resolved to the tailnet may also hold public
// addresses, so it waits too.
while now >= at[t] && running[t] < cap {
let numeric = matches!(queues[t].front(), Some((_, None)));
if !(t == 0 || ranking.is_empty() || (t == 1 && numeric)) {
break;
}
let Some((route, targets)) = queues[t].pop_front() else {
break;
};
Expand Down
2 changes: 1 addition & 1 deletion docs/limits.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ spec's value: it can only be tightened.
| `MOUSSE_NET_DIAL_TIMEOUT_MS` | 2000 | 100 to 60000 | Caller: time one candidate address has to connect and pass step 1 before it is given up (Phase 1a §6). |
| `MOUSSE_NET_DIAL_MAX_CANDIDATES` | 16 | 1 to 64 | Caller: candidates dialled in one search, in class order: LAN, tailnet, internet and tunnel, then SSH (Phase 1b §1.4). A hostname counts with the class it resolves to. |
| `MOUSSE_NET_DIAL_LAN_RESERVED` | 4 | 0 to 32 | Caller: of those, places only LAN candidates may take, so tailnet and internet hints or a browse result never crowd the LAN out. At most half the candidates. |
| `MOUSSE_NET_LAN_HEAD_START_MS` | 1000 | 0 to 10000 | Caller: wait after LAN candidates start before tailnet ones start, when a LAN candidate is known at the start (a stored or owner address, a fresh browse, a name that resolves to the LAN). At once when there is none or once every LAN candidate failed; a browse still running never holds the tailnet back. |
| `MOUSSE_NET_LAN_HEAD_START_MS` | 1000 | 0 to 10000 | Caller: wait after LAN candidates start before tailnet ones start, when a LAN candidate is known at the start (a stored or owner address, a fresh browse, a name that resolves to the LAN), or while a name is still being resolved (it may be LAN). At once when there is none or once every name is resolved and every LAN candidate failed; a browse still running never holds the tailnet back, and a name still resolving holds numeric tailnet candidates back for this long at most (a name resolved to the tailnet waits for every name). |
| `MOUSSE_NET_TIER_HEAD_START_MS` | 500 | 0 to 10000 | Caller: wait after the tailnet class starts before internet and tunnel candidates start; at once when there is no LAN or tailnet candidate. |
| `MOUSSE_NET_DIAL_FAST_TIERS_MS` | 12000 | 1000 to 60000 | Caller: latest start of SSH candidates, from the start of the search; they start earlier once every other class is done, so an SSH login never races a LAN path. The last working route only orders candidates within their class. |
| `MOUSSE_NET_DIAL_SEARCH_MS` | 45000 | 5000 to 300000 | Caller: one whole search across every tier; past it the search ends unreachable. |
Expand Down
24 changes: 14 additions & 10 deletions docs/phase1b-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,15 @@ is an allowlist: an address in none of the first three rows is
search, never from a remembered list (§1.5), and filtered through the
approvals (§1.3). Each candidate gets the class of its address
(`AddrPolicy::class_of`); a hostname gets the class of the best address
it resolves to, within the try's timeout, and the classes after LAN wait
while names resolve. The order is by class: LAN, tailnet, internet and
tunnel, then SSH. Within a class the last working route comes first,
then owner addresses, approved hints, then browse results. At most
`DIAL_MAX_CANDIDATES` are dialled, and `DIAL_LAN_RESERVED` of those
places (at most half) are kept for LAN candidates, browse results
included.
it resolves to, within the try's timeout. Numeric tailnet candidates
wait for names being resolved for `LAN_HEAD_START_MS` at most; a name
resolved to the tailnet (it may also hold public addresses) and the
classes after the tailnet wait while names resolve. The order is by
class: LAN, tailnet, internet and tunnel, then SSH. Within a class the
last working route comes first, then owner addresses, approved hints,
then browse results. At most `DIAL_MAX_CANDIDATES` are dialled, and
`DIAL_LAN_RESERVED` of those places (at most half) are kept for LAN
candidates, browse results included.
- **Concurrency.** Each class keeps 1a's race, `DIAL_PARALLEL` tries at
once. A `tcp` try gets `DIAL_TIMEOUT_MS`, a `ws`/`wss` one
`WS_DIAL_TIMEOUT_MS`, an `ssh` one `SSH_DIAL_TIMEOUT_MS`.
Expand All @@ -198,9 +200,11 @@ is an allowlist: an address in none of the first three rows is
results are candidates from the start.
2. **Tailnet**, `LAN_HEAD_START_MS` after LAN when a LAN candidate was
known at the start (stored, owner-entered, a fresh browse, or a name
that resolved to the LAN). With no known LAN candidate it starts at
once: a browse still running never holds the tailnet back. It starts
early once every LAN candidate failed.
that resolved to the LAN), or while a name is still being resolved
(it may be LAN). With no known LAN candidate and no name to resolve it
starts at once: a browse still running never holds the tailnet back.
It starts early once every name is resolved and every LAN candidate
failed.
3. **Internet and tunnel**, `TIER_HEAD_START_MS` after the tailnet (at
once if there is no LAN or tailnet candidate), or earlier once every
class before it is done.
Expand Down
Loading