Diffinery

homeflare/celldpublic

celld/crates/logic/routing.rs
92 lines · 3.7 KBRaw History
1// Copyright 2026 Deno Land Inc. Apache-2.0 license.
2
3//! Whether a failed peer dispatch may be re-sent.
4//!
5//! A cell runs on exactly one node, so every other node forwards to the
6//! owner. When that forward fails, celld must decide between two bad
7//! options: fail the caller, or re-send and risk running the request twice.
8//! The deciding fact is how far the attempt got. A connection that was never
9//! established carried no request bytes, so the owner's cell is untouched and
10//! a fresh attempt cannot double-apply. Every other failure — a timeout after
11//! the request was written, a truncated body, a decode error — leaves the
12//! outcome ambiguous: the owner may have run it and lost the reply. Re-sending
13//! those would break at-most-once execution.
14//!
15//! The case this exists for is a burst of cold activations, where TCP
16//! handshakes stall past the client's connect timeout. Those forwards never
17//! reached the owner, so surfacing them to the caller is a self-inflicted
18//! error.
19
20/// How far a dispatch attempt got before it failed.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum Attempt {
23 /// The peer answered that it no longer owns the cell. Nothing ran.
24 NotOwner,
25 /// The connection was never established. No bytes reached the owner.
26 NeverConnected,
27 /// The request was on the wire when the attempt failed. The owner may or
28 /// may not have run it.
29 Ambiguous,
30}
31
32/// Redispatches allowed per failure class, counted separately so a route that
33/// is both stale and unreachable still terminates. The unreachable budget is
34/// deliberately one: every attempt of that class costs a full connect timeout,
35/// so each retry is also the latency a caller pays when the owner is genuinely
36/// down, not just briefly congested.
37pub const MAX_NOT_OWNER: u32 = 1;
38pub const MAX_NEVER_CONNECTED: u32 = 1;
39
40/// May a request that failed with `attempt` be dispatched again, given how
41/// many redispatches of that class it has already spent?
42pub fn may_redispatch(attempt: Attempt, spent: u32) -> bool {
43 match attempt {
44 Attempt::Ambiguous => false,
45 Attempt::NotOwner => spent < MAX_NOT_OWNER,
46 Attempt::NeverConnected => spent < MAX_NEVER_CONNECTED,
47 }
48}
49
50/// What the router does after a failed attempt.
51#[derive(Clone, Copy, Debug, PartialEq, Eq)]
52pub enum Next {
53 /// Resolve the owner again and re-send. `forget_node` additionally drops
54 /// the cached node record, whose address is otherwise trusted until the
55 /// node's lease expires — re-resolving without it returns the identical
56 /// failed route, so the redispatch is wasted.
57 Redispatch { forget_node: bool },
58 /// Give up and answer the caller.
59 Fail,
60}
61
62/// Per-request redispatch state. Budgets are counted per failure class so a
63/// route that is both stale and unreachable still terminates.
64#[derive(Clone, Copy, Debug, Default)]
65pub struct Dispatcher {
66 spent_not_owner: u32,
67 spent_never_connected: u32,
68}
69
70impl Dispatcher {
71 /// Record a failed attempt and decide what happens next.
72 pub fn on_failure(&mut self, attempt: Attempt) -> Next {
73 let spent = match attempt {
74 Attempt::NotOwner => self.spent_not_owner,
75 _ => self.spent_never_connected,
76 };
77 if !may_redispatch(attempt, spent) {
78 return Next::Fail;
79 }
80 match attempt {
81 Attempt::NotOwner => {
82 self.spent_not_owner += 1;
83 // Ownership moved; the address is still good.
84 Next::Redispatch { forget_node: false }
85 }
86 _ => {
87 self.spent_never_connected += 1;
88 Next::Redispatch { forget_node: true }
89 }
90 }
91 }
92}