
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1203 · main · 9 min 56 s
1Étoiles
0Forks
0Issues
| 1 | //! The inbound relay: what peers send, handed to the program's [`Inbound`] |
| 2 | //! handler on a thread of its own (`louise-dist-in`), in the order it |
| 3 | //! arrived, so that the `louise-dist` thread only reads, frames and writes. |
| 4 | //! |
| 5 | //! - **Why.** The handler decodes messages, and a frame of a few MiB of |
| 6 | //! boxed leaves (`Array<Dec>`) takes hundreds of milliseconds to decode: |
| 7 | //! on the transport's thread, every other peer's heartbeats waited for it |
| 8 | //! (#404's security review, SEC-6). |
| 9 | //! - **Order.** One queue for every peer and event, in the order the |
| 10 | //! transport met them: a peer's frames after its `node_up` and before its |
| 11 | //! `node_down`, a connection's `closed` before its `node_down`. Only |
| 12 | //! [`Inbound::drained`] is called at once: it wakes local senders, and |
| 13 | //! orders nothing. |
| 14 | //! - **Back-pressure.** Each peer's frames waiting here are counted |
| 15 | //! ([`BACKLOG`]): while they fill it, the transport stops reading that |
| 16 | //! peer's connection, so TCP pushes back on its sender instead of this |
| 17 | //! node buffering without bound. The relay wakes the transport once they |
| 18 | //! are down to half. A paused connection is not judged silent: the |
| 19 | //! transport chose not to read it. |
| 20 | //! - **Stop.** The transport stops first, then the relay handles what is |
| 21 | //! queued (the `node_down`s of the connections the stop closed) and its |
| 22 | //! thread is joined. |
| 23 | |
| 24 | use std::collections::{HashMap, HashSet, VecDeque}; |
| 25 | use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock, PoisonError}; |
| 26 | |
| 27 | use louise_rt_prims::wire::{HEADER_LEN, MessageHeader}; |
| 28 | |
| 29 | use super::{Inbound, Node, Outbox, Reason, Waiter}; |
| 30 | |
| 31 | /// The bytes of one peer's frames waiting for the handler past which its |
| 32 | /// connection is not read; it is read again once they are down to half. |
| 33 | pub(super) const BACKLOG: usize = 4 << 20; |
| 34 | |
| 35 | enum Event { |
| 36 | Up(Node), |
| 37 | Down(Node), |
| 38 | Message(Node, MessageHeader, Vec<u8>), |
| 39 | Signal(Node, Vec<u8>), |
| 40 | /// The label, why, and whether the peers chose the address dialed. |
| 41 | Closed(String, Reason, bool), |
| 42 | } |
| 43 | |
| 44 | impl Event { |
| 45 | /// The peer whose backlog it counts in, and its bytes. |
| 46 | fn backlog(&self) -> Option<(&str, usize)> { |
| 47 | match self { |
| 48 | Event::Message(from, _, payload) => Some((&from.name, HEADER_LEN + payload.len())), |
| 49 | Event::Signal(from, body) => Some((&from.name, body.len())), |
| 50 | _ => None, |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[derive(Default)] |
| 56 | struct Queue { |
| 57 | events: VecDeque<Event>, |
| 58 | /// Bytes of frames queued, per peer name (only those with some). |
| 59 | backlog: HashMap<String, usize>, |
| 60 | /// Peers whose connection the transport stopped reading. |
| 61 | paused: HashSet<String>, |
| 62 | stopping: bool, |
| 63 | } |
| 64 | |
| 65 | pub(super) struct Relay { |
| 66 | handler: Arc<dyn Inbound>, |
| 67 | queue: Mutex<Queue>, |
| 68 | ready: Condvar, |
| 69 | /// The transport's, to wake it when a paused peer may be read again. |
| 70 | outbox: OnceLock<Arc<Outbox>>, |
| 71 | } |
| 72 | |
| 73 | impl Relay { |
| 74 | pub(super) fn new(handler: Arc<dyn Inbound>) -> Relay { |
| 75 | Relay { |
| 76 | handler, |
| 77 | queue: Mutex::default(), |
| 78 | ready: Condvar::new(), |
| 79 | outbox: OnceLock::new(), |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// The transport to wake; set once, before any connection. |
| 84 | pub(super) fn wakes(&self, outbox: Arc<Outbox>) { |
| 85 | let _ = self.outbox.set(outbox); |
| 86 | } |
| 87 | |
| 88 | fn queue(&self) -> MutexGuard<'_, Queue> { |
| 89 | self.queue.lock().unwrap_or_else(PoisonError::into_inner) |
| 90 | } |
| 91 | |
| 92 | fn push(&self, event: Event) { |
| 93 | let mut q = self.queue(); |
| 94 | if let Some((peer, bytes)) = event.backlog() { |
| 95 | *q.backlog.entry(peer.to_owned()).or_default() += bytes; |
| 96 | } |
| 97 | q.events.push_back(event); |
| 98 | drop(q); |
| 99 | self.ready.notify_one(); |
| 100 | } |
| 101 | |
| 102 | /// Whether `peer`'s backlog is full: then the transport stops reading |
| 103 | /// its connection, and is woken once it is down to half. |
| 104 | pub(super) fn full(&self, peer: &str) -> bool { |
| 105 | let mut q = self.queue(); |
| 106 | let full = q.backlog.get(peer).is_some_and(|b| *b >= BACKLOG); |
| 107 | if full { |
| 108 | q.paused.insert(peer.to_owned()); |
| 109 | } |
| 110 | full |
| 111 | } |
| 112 | |
| 113 | /// Whether `peer`'s connection, paused, may be read again. |
| 114 | pub(super) fn resumed(&self, peer: &str) -> bool { |
| 115 | !self.queue().paused.contains(peer) |
| 116 | } |
| 117 | |
| 118 | /// Hand the queued events to the handler until [`Relay::stop`], then |
| 119 | /// those left: the relay thread's body. |
| 120 | pub(super) fn run(&self) { |
| 121 | loop { |
| 122 | let event = { |
| 123 | let mut q = self.queue(); |
| 124 | loop { |
| 125 | if let Some(event) = q.events.pop_front() { |
| 126 | break event; |
| 127 | } |
| 128 | if q.stopping { |
| 129 | return; |
| 130 | } |
| 131 | q = self.ready.wait(q).unwrap_or_else(PoisonError::into_inner); |
| 132 | } |
| 133 | }; |
| 134 | self.handle(&event); |
| 135 | let Some((peer, bytes)) = event.backlog() else { |
| 136 | continue; |
| 137 | }; |
| 138 | let mut q = self.queue(); |
| 139 | let left = match q.backlog.get_mut(peer) { |
| 140 | Some(b) => { |
| 141 | *b -= bytes; |
| 142 | *b |
| 143 | } |
| 144 | None => 0, |
| 145 | }; |
| 146 | if left == 0 { |
| 147 | q.backlog.remove(peer); |
| 148 | } |
| 149 | let resume = left <= BACKLOG / 2 && q.paused.remove(peer); |
| 150 | drop(q); |
| 151 | if resume && let Some(outbox) = self.outbox.get() { |
| 152 | outbox.wake(); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn handle(&self, event: &Event) { |
| 158 | let h = &self.handler; |
| 159 | match event { |
| 160 | Event::Up(peer) => h.node_up(peer), |
| 161 | Event::Down(peer) => h.node_down(peer), |
| 162 | Event::Message(from, header, payload) => h.message(from, header, payload), |
| 163 | Event::Signal(from, body) => h.signal(from, body), |
| 164 | Event::Closed(peer, reason, hearsay) => h.closed(peer, reason, *hearsay), |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// Let [`Relay::run`] return once the queue is empty. |
| 169 | pub(super) fn stop(&self) { |
| 170 | self.queue().stopping = true; |
| 171 | self.ready.notify_one(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | impl Inbound for Relay { |
| 176 | fn node_up(&self, peer: &Node) { |
| 177 | self.push(Event::Up(peer.clone())); |
| 178 | } |
| 179 | |
| 180 | fn node_down(&self, peer: &Node) { |
| 181 | self.push(Event::Down(peer.clone())); |
| 182 | } |
| 183 | |
| 184 | fn message(&self, from: &Node, header: &MessageHeader, payload: &[u8]) { |
| 185 | self.push(Event::Message(from.clone(), *header, payload.to_vec())); |
| 186 | } |
| 187 | |
| 188 | fn signal(&self, from: &Node, body: &[u8]) { |
| 189 | self.push(Event::Signal(from.clone(), body.to_vec())); |
| 190 | } |
| 191 | |
| 192 | fn drained(&self, peer: &Node, waiters: &[Waiter]) { |
| 193 | self.handler.drained(peer, waiters); |
| 194 | } |
| 195 | |
| 196 | fn closed(&self, peer: &str, reason: &Reason, hearsay: bool) { |
| 197 | self.push(Event::Closed(peer.to_owned(), reason.clone(), hearsay)); |
| 198 | } |
| 199 | } |