| @@ -17,21 +17,34 @@ |
| 17 | 17 | //! |
| 18 | 18 | //! **Discovery** (`mesh`, `docs/wire-format.md`, *Peers*). A connection |
| 19 | 19 | //! whose two hellos set [`frame::FLAG_PEERS`] carries lists of peers (signal |
| 20 | | −//! op `03`), queued behind what the peer's queue holds: |
| 20 | +//! op `03`), written after what the peer's queue held when they were made: |
| 21 | 21 | //! - A node sends its list to a peer as soon as it is up with it, and to |
| 22 | 22 | //! every such peer when a node comes up or goes down, at most once per |
| 23 | | −//! heartbeat. The list holds this node's advertised address, and each node |
| 24 | | −//! up whose address it holds from the operator (a seed, `connect`) or from |
| 25 | | −//! the node itself (its own entry in its list, or a dial that came up). |
| 23 | +//! heartbeat for each [`RETELL_PEERS`] peers up. The list holds this |
| 24 | +//! node's advertised address, and each node up whose address it holds |
| 25 | +//! from the operator (a seed, `connect`) or from the node itself (its own |
| 26 | +//! entry in its list, or a dial that came up). It is encoded once for |
| 27 | +//! all peers (each ignores its own entry), and one waits at most per |
| 28 | +//! peer: a newer one replaces it ([`Conn::list`]). |
| 26 | 29 | //! - A name a list gives that this node does not know becomes *discovered*, |
| 27 | 30 | //! and [`Thread::tick`] dials it; simultaneous dials resolve as any |
| 28 | | −//! others ([`Thread::arbitrate`]). A third party's address replaces one |
| 29 | | −//! neither the node nor a dial confirmed; a seed's never changes. |
| 31 | +//! others ([`Thread::arbitrate`]). Until the node confirms its address |
| 32 | +//! (its own entry, or a dial that came up), each dial tries the next of |
| 33 | +//! the addresses its listers give, one each, in the order of their |
| 34 | +//! connections: a lister that lies, however often and wherever, holds up |
| 35 | +//! the dial at the honest one by one failed dial. A |
| 36 | +//! seed's address never changes. |
| 30 | 37 | //! - A discovered node is *listed* while it is up or some peer's latest |
| 31 | 38 | //! list holds it. A lost one is dialed again with backoff, and forgotten |
| 32 | 39 | //! once no peer has listed it for [`FORGET_AFTER`] × `node_down_after_ms`. |
| 33 | | −//! At most `max_peers` are known, and a peer's list holds at most that |
| 34 | | −//! many: past it, its frames are dropped and counted. |
| 40 | +//! - At most `max_peers` are discovered, and a peer's list holds at most |
| 41 | +//! that many (past it, the rest is not read). A node never seen up and |
| 42 | +//! never confirmed is *on probation*, held by the lister whose list |
| 43 | +//! brought it ([`Found::lister`]). When none is left, a new name takes |
| 44 | +//! the place of one no lister holds, or else of one held by a lister |
| 45 | +//! holding more than its share (`max_peers` over the peers that list): |
| 46 | +//! one peer's names cannot keep every other node from being discovered |
| 47 | +//! ([`Thread::room`]). |
| 35 | 48 | |
| 36 | 49 | use std::collections::{HashMap, HashSet, VecDeque}; |
| 37 | 50 | use std::io::{ErrorKind, IoSlice, Read, Write}; |
| @@ -75,6 +88,10 @@ const ACCEPTS: usize = 64; |
| 75 | 88 | /// A discovered node no peer lists is forgotten after this many times |
| 76 | 89 | /// `node_down_after_ms`: meanwhile it is dialed again, with backoff. |
| 77 | 90 | pub(super) const FORGET_AFTER: u32 = 10; |
| 91 | +/// Lists of peers go to every peer at most once per heartbeat for each |
| 92 | +/// this many peers up: a round costs a list of `n` names for each of `n` |
| 93 | +/// peers, written to each. |
| 94 | +const RETELL_PEERS: usize = 256; |
| 78 | 95 | |
| 79 | 96 | pub(super) struct Thread { |
| 80 | 97 | config: Config, |
| @@ -98,6 +115,12 @@ pub(super) struct Thread { |
| 98 | 115 | known: HashMap<String, Known>, |
| 99 | 116 | /// How many of `known` are discovered: at most `max_peers`. |
| 100 | 117 | discovered: usize, |
| 118 | + /// By connection: how many discovered nodes on probation its lists |
| 119 | + /// brought and still hold ([`Found::lister`]). |
| 120 | + held: HashMap<Token, usize>, |
| 121 | + /// How many discovered nodes no lister holds ([`Found::lister`]): the |
| 122 | + /// first a new name may take the place of. |
| 123 | + unheld: usize, |
| 101 | 124 | /// Per discovered node, how many up peers' latest lists hold it. |
| 102 | 125 | listings: HashMap<String, usize>, |
| 103 | 126 | /// This node's address in its lists; `None`: it gives none. |
| @@ -134,6 +157,15 @@ struct Found { |
| 134 | 157 | /// a dial to it there came up: another peer's list does not replace |
| 135 | 158 | /// it. |
| 136 | 159 | confirmed: bool, |
| 160 | + /// It came up, or confirmed its address: not on probation. |
| 161 | + proven: bool, |
| 162 | + /// The connection whose lists hold it for the discovery budget |
| 163 | + /// ([`Thread::room`]): the one whose list brought it, or the next to |
| 164 | + /// list it once that one no longer does. `None`: no lister holds it. |
| 165 | + lister: Option<Token>, |
| 166 | + /// Unconfirmed: how many dials were made, to try its listers' |
| 167 | + /// addresses in turn. |
| 168 | + dials: usize, |
| 137 | 169 | /// Since when it is neither up nor in any up peer's list: it is |
| 138 | 170 | /// forgotten [`FORGET_AFTER`] × `node_down_after_ms` later. |
| 139 | 171 | unlisted: Option<Instant>, |
| @@ -189,8 +221,13 @@ struct Conn { |
| 189 | 221 | paused: bool, |
| 190 | 222 | /// Both hellos set [`frame::FLAG_PEERS`]: lists of peers go both ways. |
| 191 | 223 | mesh: bool, |
| 192 | | − /// The discovered nodes its latest list holds. |
| 193 | | − listed: HashSet<String>, |
| 224 | + /// The discovered nodes its latest list holds, at the address it |
| 225 | + /// gives each. |
| 226 | + listed: HashMap<String, SocketAddr>, |
| 227 | + /// This node's latest list for it, not yet handed to [`Conn::out`]: |
| 228 | + /// its frames' bodies, shared by every peer of the same frame limit. A |
| 229 | + /// newer list replaces it, so a peer that does not read holds one. |
| 230 | + list: Option<Arc<[Vec<u8>]>>, |
| 194 | 231 | } |
| 195 | 232 | |
| 196 | 233 | impl Conn { |
| @@ -216,7 +253,8 @@ impl Conn { |
| 216 | 253 | stats: None, |
| 217 | 254 | paused: false, |
| 218 | 255 | mesh: false, |
| 219 | | − listed: HashSet::new(), |
| 256 | + listed: HashMap::new(), |
| 257 | + list: None, |
| 220 | 258 | } |
| 221 | 259 | } |
| 222 | 260 | |
| @@ -386,6 +424,8 @@ impl Thread { |
| 386 | 424 | up: HashMap::new(), |
| 387 | 425 | known, |
| 388 | 426 | discovered: 0, |
| 427 | + held: HashMap::new(), |
| 428 | + unheld: 0, |
| 389 | 429 | listings: HashMap::new(), |
| 390 | 430 | advertise, |
| 391 | 431 | welcome: Vec::new(), |
| @@ -501,8 +541,9 @@ impl Thread { |
| 501 | 541 | known.addr = addr; |
| 502 | 542 | known.due = Some(Instant::now()); |
| 503 | 543 | // Kept from now on, as a seed. |
| 504 | | − if known.found.take().is_some() { |
| 544 | + if let Some(found) = known.found.take() { |
| 505 | 545 | self.discovered -= 1; |
| 546 | + self.release(&found); |
| 506 | 547 | } |
| 507 | 548 | } |
| 508 | 549 | } |
| @@ -558,10 +599,7 @@ impl Thread { |
| 558 | 599 | .map(|(name, _)| name.clone()) |
| 559 | 600 | .collect(); |
| 560 | 601 | for name in forgotten { |
| 561 | | − // A dial in progress ends as one to a node never known. |
| 562 | | − self.known.remove(&name); |
| 563 | | − self.discovered -= 1; |
| 564 | | − self.listings.remove(&name); |
| 602 | + self.forget(&name); |
| 565 | 603 | } |
| 566 | 604 | let due: Vec<String> = self |
| 567 | 605 | .known |
| @@ -626,10 +664,32 @@ impl Thread { |
| 626 | 664 | } |
| 627 | 665 | |
| 628 | 666 | fn dial(&mut self, name: &str) { |
| 667 | + // Unconfirmed: its listers' addresses in turn, one each, in the |
| 668 | + // order of their connections. Not in the addresses' order: a liar |
| 669 | + // could then list, before each dial, one that sorts just where the |
| 670 | + // next turn falls, and hold every turn. |
| 671 | + let mut listers: Vec<(Token, SocketAddr)> = self |
| 672 | + .conns |
| 673 | + .iter() |
| 674 | + .filter_map(|(token, c)| Some((*token, *c.listed.get(name)?))) |
| 675 | + .collect(); |
| 676 | + listers.sort_unstable_by_key(|(token, _)| *token); |
| 677 | + let mut given = HashSet::new(); |
| 678 | + let addrs: Vec<SocketAddr> = listers |
| 679 | + .into_iter() |
| 680 | + .map(|(_, addr)| addr) |
| 681 | + .filter(|addr| given.insert(*addr)) |
| 682 | + .collect(); |
| 629 | 683 | let Some(known) = self.known.get_mut(name) else { |
| 630 | 684 | return; |
| 631 | 685 | }; |
| 632 | 686 | known.due = None; |
| 687 | + if let Some(found) = known.found.as_mut().filter(|f| !f.confirmed) |
| 688 | + && !addrs.is_empty() |
| 689 | + { |
| 690 | + known.addr = addrs[found.dials % addrs.len()]; |
| 691 | + found.dials = found.dials.wrapping_add(1); |
| 692 | + } |
| 633 | 693 | let addr = known.addr; |
| 634 | 694 | let tls = ServerName::try_from(name.to_owned()) |
| 635 | 695 | .map_err(|e| e.to_string()) |
| @@ -644,7 +704,8 @@ impl Thread { |
| 644 | 704 | let (sock, tls) = match opened { |
| 645 | 705 | Ok(opened) => opened, |
| 646 | 706 | Err(e) => { |
| 647 | | − self.inbound.closed(name, &Reason::Closed(e)); |
| 707 | + let hearsay = self.hearsay(name); |
| 708 | + self.inbound.closed(name, &Reason::Closed(e), hearsay); |
| 648 | 709 | self.lost(name); |
| 649 | 710 | return; |
| 650 | 711 | } |
| @@ -741,8 +802,9 @@ impl Thread { |
| 741 | 802 | token, |
| 742 | 803 | Interest::READABLE | Interest::WRITABLE, |
| 743 | 804 | ) { |
| 805 | + // A local failure, not the peers' doing. |
| 744 | 806 | self.inbound |
| 745 | | − .closed(&conn.label(), &Reason::Closed(e.to_string())); |
| 807 | + .closed(&conn.label(), &Reason::Closed(e.to_string()), false); |
| 746 | 808 | if let Some(name) = conn.name().map(str::to_owned) { |
| 747 | 809 | self.lost(&name); |
| 748 | 810 | } |
| @@ -840,12 +902,17 @@ impl Thread { |
| 840 | 902 | let _ = self.poll.registry().deregister(&mut conn.sock); |
| 841 | 903 | let _ = conn.sock.shutdown(Shutdown::Both); |
| 842 | 904 | self.outbox.open.fetch_sub(1, Ordering::AcqRel); |
| 843 | | − self.inbound.closed(&conn.label(), &reason); |
| 905 | + // A dial that never got past its hello, to an address only lists |
| 906 | + // gave: the peers chose it, so it is logged sparingly. |
| 907 | + let hearsay = |
| 908 | + matches!(&conn.role, Role::Dial(name) if conn.peer.is_none() && self.hearsay(name)); |
| 909 | + self.inbound.closed(&conn.label(), &reason, hearsay); |
| 844 | 910 | let now = Instant::now(); |
| 845 | | − for name in std::mem::take(&mut conn.listed) { |
| 846 | | − self.unlist(&name); |
| 911 | + for name in std::mem::take(&mut conn.listed).into_keys() { |
| 912 | + self.unlist(token, &name); |
| 847 | 913 | self.relist(&name, now); |
| 848 | 914 | } |
| 915 | + self.held.remove(&token); |
| 849 | 916 | if conn.phase == Phase::Up |
| 850 | 917 | && let Some(peer) = &conn.peer |
| 851 | 918 | { |
| @@ -1007,7 +1074,7 @@ impl Thread { |
| 1007 | 1074 | } |
| 1008 | 1075 | (Phase::Up, frame::MESSAGE) => self.message(conn, body), |
| 1009 | 1076 | (Phase::Up, frame::SIGNAL) if body.first() == Some(&frame::PEERS) => { |
| 1010 | | − self.listed(conn, &body[1..]); |
| 1077 | + self.listed(token, conn, &body[1..]); |
| 1011 | 1078 | Ok(()) |
| 1012 | 1079 | } |
| 1013 | 1080 | (Phase::Up, frame::SIGNAL) => { |
| @@ -1181,6 +1248,7 @@ impl Thread { |
| 1181 | 1248 | found.confirmed = true; |
| 1182 | 1249 | } |
| 1183 | 1250 | } |
| 1251 | + self.prove(&peer.name); |
| 1184 | 1252 | self.relist(&peer.name, Instant::now()); |
| 1185 | 1253 | if conn.mesh { |
| 1186 | 1254 | self.welcome.push(token); |
| @@ -1193,12 +1261,12 @@ impl Thread { |
| 1193 | 1261 | |
| 1194 | 1262 | /// A peer's list of nodes (op `03`; `fields` after the op): what it |
| 1195 | 1263 | /// names that this node does not know becomes discovered. Dropped and |
| 1196 | | − /// counted if the connection does not carry lists ([`Conn::mesh`]), |
| 1197 | | − /// if it is malformed, or if it takes the peer's list past |
| 1198 | | − /// `max_peers`. |
| 1199 | | − fn listed(&mut self, conn: &mut Conn, fields: &[u8]) { |
| 1264 | + /// counted if the connection does not carry lists ([`Conn::mesh`]), or |
| 1265 | + /// if it is malformed. Past `max_peers` names (this node's own aside), |
| 1266 | + /// the rest of the peer's list is not read. |
| 1267 | + fn listed(&mut self, token: Token, conn: &mut Conn, fields: &[u8]) { |
| 1200 | 1268 | let list = match (&conn.peer, conn.mesh) { |
| 1201 | | − (Some(peer), true) => frame::Peers::decode(fields, self.config.max_peers) |
| 1269 | + (Some(peer), true) => frame::Peers::decode(fields) |
| 1202 | 1270 | .ok() |
| 1203 | 1271 | .map(|list| (peer.name.clone(), list)), |
| 1204 | 1272 | _ => None, |
| @@ -1212,21 +1280,34 @@ impl Thread { |
| 1212 | 1280 | let now = Instant::now(); |
| 1213 | 1281 | let mut changed = Vec::new(); |
| 1214 | 1282 | if list.first { |
| 1215 | | − for name in std::mem::take(&mut conn.listed) { |
| 1216 | | − self.unlist(&name); |
| 1283 | + for name in std::mem::take(&mut conn.listed).into_keys() { |
| 1284 | + self.unlist(token, &name); |
| 1217 | 1285 | changed.push(name); |
| 1218 | 1286 | } |
| 1219 | 1287 | } |
| 1220 | | − if conn.listed.len() + list.nodes.len() > self.config.max_peers { |
| 1221 | | − if let Some(stats) = &conn.stats { |
| 1222 | | − Stats::count(&stats.dropped, 1); |
| 1288 | + for (name, addr) in list.nodes { |
| 1289 | + if name == self.me.name { |
| 1290 | + continue; |
| 1223 | 1291 | } |
| 1224 | | − } else { |
| 1225 | | − for (name, addr) in list.nodes { |
| 1226 | | − if self.learn(&lister, &name, addr, now) && conn.listed.insert(name.clone()) { |
| 1227 | | − *self.listings.entry(name.clone()).or_default() += 1; |
| 1228 | | − changed.push(name); |
| 1229 | | − } |
| 1292 | + if let Some(given) = conn.listed.get_mut(&name) { |
| 1293 | + *given = addr; |
| 1294 | + self.learn(token, &lister, &name, addr, now); |
| 1295 | + continue; |
| 1296 | + } |
| 1297 | + // Past `max_peers` names, the rest of the list is not taken. |
| 1298 | + if conn.listed.len() >= self.config.max_peers { |
| 1299 | + continue; |
| 1300 | + } |
| 1301 | + if !self.known.contains_key(&name) |
| 1302 | + && self.discovered >= self.config.max_peers |
| 1303 | + && !self.room(token, conn) |
| 1304 | + { |
| 1305 | + continue; |
| 1306 | + } |
| 1307 | + if self.learn(token, &lister, &name, addr, now) { |
| 1308 | + conn.listed.insert(name.clone(), addr); |
| 1309 | + *self.listings.entry(name.clone()).or_default() += 1; |
| 1310 | + changed.push(name); |
| 1230 | 1311 | } |
| 1231 | 1312 | } |
| 1232 | 1313 | for name in changed { |
| @@ -1234,15 +1315,21 @@ impl Thread { |
| 1234 | 1315 | } |
| 1235 | 1316 | } |
| 1236 | 1317 | |
| 1237 | | − /// `lister`'s list gives `addr` for `name`: whether `name` is now a |
| 1238 | | − /// discovered node. A name this node does not know becomes one (and is |
| 1239 | | − /// dialed) while fewer than `max_peers` are; a seed's address is kept; |
| 1240 | | − /// a discovered node's is replaced by its own word, or by a third |
| 1241 | | − /// party's while unconfirmed. |
| 1242 | | − fn learn(&mut self, lister: &str, name: &str, addr: SocketAddr, now: Instant) -> bool { |
| 1243 | | − if name == self.me.name { |
| 1244 | | − return false; |
| 1245 | | − } |
| 1318 | + /// The list of connection `token`, from `lister`, gives `addr` for |
| 1319 | + /// `name`: whether `name` is now a discovered node. A name this node |
| 1320 | + /// does not know becomes one (and is dialed) while fewer than |
| 1321 | + /// `max_peers` are, held by `token` ([`Found::lister`]); a seed's |
| 1322 | + /// address is kept; a discovered node's is replaced by its own word, |
| 1323 | + /// or by a third party's while unconfirmed (the address dialed when |
| 1324 | + /// none of its listers gives one). |
| 1325 | + fn learn( |
| 1326 | + &mut self, |
| 1327 | + token: Token, |
| 1328 | + lister: &str, |
| 1329 | + name: &str, |
| 1330 | + addr: SocketAddr, |
| 1331 | + now: Instant, |
| 1332 | + ) -> bool { |
| 1246 | 1333 | let own = name == lister; |
| 1247 | 1334 | if let Some(known) = self.known.get_mut(name) { |
| 1248 | 1335 | let Some(found) = &mut known.found else { |
| @@ -1252,6 +1339,16 @@ impl Thread { |
| 1252 | 1339 | known.addr = addr; |
| 1253 | 1340 | } |
| 1254 | 1341 | found.confirmed |= own; |
| 1342 | + if found.lister.is_none() { |
| 1343 | + found.lister = Some(token); |
| 1344 | + self.unheld = self.unheld.saturating_sub(1); |
| 1345 | + if !found.proven { |
| 1346 | + *self.held.entry(token).or_default() += 1; |
| 1347 | + } |
| 1348 | + } |
| 1349 | + if own { |
| 1350 | + self.prove(name); |
| 1351 | + } |
| 1255 | 1352 | return true; |
| 1256 | 1353 | } |
| 1257 | 1354 | if self.discovered >= self.config.max_peers { |
| @@ -1259,10 +1356,17 @@ impl Thread { |
| 1259 | 1356 | } |
| 1260 | 1357 | self.discovered += 1; |
| 1261 | 1358 | let due = (!self.named(name)).then_some(now); |
| 1359 | + let proven = own || self.up.contains_key(name); |
| 1262 | 1360 | let found = Found { |
| 1263 | 1361 | confirmed: own, |
| 1362 | + proven, |
| 1363 | + lister: Some(token), |
| 1364 | + dials: 0, |
| 1264 | 1365 | unlisted: None, |
| 1265 | 1366 | }; |
| 1367 | + if !proven { |
| 1368 | + *self.held.entry(token).or_default() += 1; |
| 1369 | + } |
| 1266 | 1370 | let known = Known { |
| 1267 | 1371 | addr, |
| 1268 | 1372 | backoff: 1, |
| @@ -1273,14 +1377,134 @@ impl Thread { |
| 1273 | 1377 | true |
| 1274 | 1378 | } |
| 1275 | 1379 | |
| 1276 | | − /// One up peer's list no longer holds `name`. |
| 1277 | | − fn unlist(&mut self, name: &str) { |
| 1380 | + /// Discovered `name` came up, or confirmed its address: off |
| 1381 | + /// probation, its lister holds it no longer against its share. |
| 1382 | + fn prove(&mut self, name: &str) { |
| 1383 | + let Some(found) = self.known.get_mut(name).and_then(|k| k.found.as_mut()) else { |
| 1384 | + return; |
| 1385 | + }; |
| 1386 | + if !found.proven { |
| 1387 | + found.proven = true; |
| 1388 | + if let Some(lister) = found.lister { |
| 1389 | + self.unhold(lister); |
| 1390 | + } |
| 1391 | + } |
| 1392 | + } |
| 1393 | + |
| 1394 | + fn unhold(&mut self, lister: Token) { |
| 1395 | + if let Some(n) = self.held.get_mut(&lister) { |
| 1396 | + *n = n.saturating_sub(1); |
| 1397 | + } |
| 1398 | + } |
| 1399 | + |
| 1400 | + /// Discovered node `found` is forgotten, or no longer discovered: its |
| 1401 | + /// lister holds it no longer. |
| 1402 | + fn release(&mut self, found: &Found) { |
| 1403 | + match found.lister { |
| 1404 | + Some(lister) if !found.proven => self.unhold(lister), |
| 1405 | + Some(_) => {} |
| 1406 | + None => self.unheld = self.unheld.saturating_sub(1), |
| 1407 | + } |
| 1408 | + } |
| 1409 | + |
| 1410 | + /// Forget discovered `name`: a dial in progress ends as one to a node |
| 1411 | + /// never known. |
| 1412 | + fn forget(&mut self, name: &str) { |
| 1413 | + if let Some(found) = self.known.remove(name).and_then(|k| k.found) { |
| 1414 | + self.discovered -= 1; |
| 1415 | + self.release(&found); |
| 1416 | + } |
| 1417 | + self.listings.remove(name); |
| 1418 | + } |
| 1419 | + |
| 1420 | + /// Room for one more discovered node, which connection `token` (out |
| 1421 | + /// of the map, `conn`) lists, all `max_peers` being taken: forget one |
| 1422 | + /// that is not up and no lister holds (no up peer's list holds it), on |
| 1423 | + /// probation first; else, if `token` holds fewer than its share |
| 1424 | + /// (`max_peers` over the connections that carry lists), one on |
| 1425 | + /// probation that the lister holding the most holds, if that is more |
| 1426 | + /// than its share. Whether there is room now. A lister that holds its |
| 1427 | + /// share keeps what it holds, so the slots end up shared, and stay so. |
| 1428 | + fn room(&mut self, token: Token, conn: &mut Conn) -> bool { |
| 1429 | + let unheld = (self.unheld > 0) |
| 1430 | + .then(|| { |
| 1431 | + self.known |
| 1432 | + .iter() |
| 1433 | + .filter_map(|(name, k)| Some((name, k.found.as_ref()?))) |
| 1434 | + .filter(|(name, f)| f.lister.is_none() && !self.up.contains_key(*name)) |
| 1435 | + .min_by_key(|(_, f)| f.proven) |
| 1436 | + .map(|(name, _)| name.clone()) |
| 1437 | + }) |
| 1438 | + .flatten(); |
| 1439 | + let victim = unheld.or_else(|| { |
| 1440 | + let listers = 1 + self |
| 1441 | + .conns |
| 1442 | + .values() |
| 1443 | + .filter(|c| c.mesh && c.phase == Phase::Up) |
| 1444 | + .count(); |
| 1445 | + let share = (self.config.max_peers / listers).max(1); |
| 1446 | + if self.held.get(&token).copied().unwrap_or(0) >= share { |
| 1447 | + return None; |
| 1448 | + } |
| 1449 | + let (most, held) = self |
| 1450 | + .held |
| 1451 | + .iter() |
| 1452 | + .filter(|(t, _)| **t != token) |
| 1453 | + .max_by_key(|(t, n)| (**n, std::cmp::Reverse(**t)))?; |
| 1454 | + if *held <= share { |
| 1455 | + return None; |
| 1456 | + } |
| 1457 | + let most = *most; |
| 1458 | + self.known |
| 1459 | + .iter() |
| 1460 | + .filter_map(|(name, k)| Some((name, k.found.as_ref()?))) |
| 1461 | + .find(|(_, f)| f.lister == Some(most) && !f.proven) |
| 1462 | + .map(|(name, _)| name.clone()) |
| 1463 | + }); |
| 1464 | + let Some(victim) = victim else { |
| 1465 | + return false; |
| 1466 | + }; |
| 1467 | + for c in self.conns.values_mut() { |
| 1468 | + c.listed.remove(&victim); |
| 1469 | + } |
| 1470 | + conn.listed.remove(&victim); |
| 1471 | + self.forget(&victim); |
| 1472 | + true |
| 1473 | + } |
| 1474 | + |
| 1475 | + /// Connection `token`'s list (`token` out of the map) no longer holds |
| 1476 | + /// `name`. If `token` held it, another connection whose list holds it |
| 1477 | + /// does now, or none: a lister cannot leave its names to the others' |
| 1478 | + /// lists for free, nor have a name another peer lists forgotten by |
| 1479 | + /// dropping it ([`Thread::room`]). |
| 1480 | + fn unlist(&mut self, token: Token, name: &str) { |
| 1278 | 1481 | if let Some(n) = self.listings.get_mut(name) { |
| 1279 | 1482 | *n = n.saturating_sub(1); |
| 1280 | 1483 | if *n == 0 { |
| 1281 | 1484 | self.listings.remove(name); |
| 1282 | 1485 | } |
| 1283 | 1486 | } |
| 1487 | + let Some(found) = self.known.get_mut(name).and_then(|k| k.found.as_mut()) else { |
| 1488 | + return; |
| 1489 | + }; |
| 1490 | + if found.lister != Some(token) { |
| 1491 | + return; |
| 1492 | + } |
| 1493 | + let next = self |
| 1494 | + .conns |
| 1495 | + .iter() |
| 1496 | + .find(|(t, c)| **t != token && c.listed.contains_key(name)) |
| 1497 | + .map(|(t, _)| *t); |
| 1498 | + found.lister = next; |
| 1499 | + let proven = found.proven; |
| 1500 | + match next { |
| 1501 | + None => self.unheld += 1, |
| 1502 | + Some(next) if !proven => *self.held.entry(next).or_default() += 1, |
| 1503 | + Some(_) => {} |
| 1504 | + } |
| 1505 | + if !proven { |
| 1506 | + self.unhold(token); |
| 1507 | + } |
| 1284 | 1508 | } |
| 1285 | 1509 | |
| 1286 | 1510 | /// Whether discovered `name` is listed (up, or in an up peer's list) |
| @@ -1296,16 +1520,27 @@ impl Thread { |
| 1296 | 1520 | } |
| 1297 | 1521 | } |
| 1298 | 1522 | |
| 1299 | | − /// Send this node's list to the peers that came up this turn, or, at |
| 1300 | | − /// most once per heartbeat after a node came up or went down, to every |
| 1301 | | − /// peer that takes lists. Each list leaves out its receiver, and is |
| 1302 | | − /// queued behind what the receiver's queue holds. |
| 1523 | + /// Whether `name` is a discovered node whose address only peers' |
| 1524 | + /// lists gave: a failed dial to it is the listers' doing. |
| 1525 | + fn hearsay(&self, name: &str) -> bool { |
| 1526 | + self.known |
| 1527 | + .get(name) |
| 1528 | + .and_then(|k| k.found.as_ref()) |
| 1529 | + .is_some_and(|f| !f.confirmed) |
| 1530 | + } |
| 1531 | + |
| 1532 | + /// Send this node's list to the peers that came up this turn, or, after |
| 1533 | + /// a node came up or went down, to every peer that takes lists: at most |
| 1534 | + /// once per heartbeat for each [`RETELL_PEERS`] peers up. The list is |
| 1535 | + /// encoded once for each frame limit, and replaces the one a peer has |
| 1536 | + /// not taken yet ([`Conn::list`]); each peer ignores its own entry. |
| 1303 | 1537 | fn tell(&mut self, now: Instant) { |
| 1304 | 1538 | if !self.config.mesh { |
| 1305 | 1539 | self.retell = false; |
| 1306 | 1540 | return; |
| 1307 | 1541 | } |
| 1308 | | − let every = self.retell && self.told.is_none_or(|t| now >= t + self.heartbeat); |
| 1542 | + let period = self.heartbeat * self.up.len().div_ceil(RETELL_PEERS).max(1) as u32; |
| 1543 | + let every = self.retell && self.told.is_none_or(|t| now >= t + period); |
| 1309 | 1544 | let welcome = std::mem::take(&mut self.welcome); |
| 1310 | 1545 | if !every && welcome.is_empty() { |
| 1311 | 1546 | return; |
| @@ -1330,25 +1565,19 @@ impl Thread { |
| 1330 | 1565 | trusted.then(|| (name.clone(), known.addr)) |
| 1331 | 1566 | })) |
| 1332 | 1567 | .collect(); |
| 1568 | + let mut encoded: HashMap<u32, Arc<[Vec<u8>]>> = HashMap::new(); |
| 1333 | 1569 | for token in to { |
| 1334 | 1570 | let Some(conn) = self |
| 1335 | 1571 | .conns |
| 1336 | | − .get(&token) |
| 1572 | + .get_mut(&token) |
| 1337 | 1573 | .filter(|c| c.mesh && c.phase == Phase::Up) |
| 1338 | 1574 | else { |
| 1339 | 1575 | continue; |
| 1340 | 1576 | }; |
| 1341 | | − let Some(peer) = conn.peer.as_ref().map(|p| p.name.clone()) else { |
| 1342 | | − continue; |
| 1343 | | − }; |
| 1344 | | − let theirs: Vec<(String, SocketAddr)> = |
| 1345 | | − list.iter().filter(|(n, _)| *n != peer).cloned().collect(); |
| 1346 | | − let frames = frame::Peers::frames(&theirs, conn.peer_max); |
| 1347 | | − if let Some(route) = self.outbox.routes().get_mut(&peer) { |
| 1348 | | − for body in frames { |
| 1349 | | − route.queue.push_back(Entry::new(frame::SIGNAL, body)); |
| 1350 | | − } |
| 1351 | | − } |
| 1577 | + let frames = encoded |
| 1578 | + .entry(conn.peer_max) |
| 1579 | + .or_insert_with(|| frame::Peers::frames(&list, conn.peer_max).into()); |
| 1580 | + conn.list = Some(frames.clone()); |
| 1352 | 1581 | self.drive_write(token); |
| 1353 | 1582 | } |
| 1354 | 1583 | } |
| @@ -1439,6 +1668,13 @@ impl Thread { |
| 1439 | 1668 | } |
| 1440 | 1669 | None => Vec::new(), |
| 1441 | 1670 | }; |
| 1671 | + // Behind the frames just taken from the queue. |
| 1672 | + if let Some(list) = conn.list.take() { |
| 1673 | + let frames = list |
| 1674 | + .iter() |
| 1675 | + .map(|body| Entry::new(frame::SIGNAL, body.clone())); |
| 1676 | + conn.out.extend(frames); |
| 1677 | + } |
| 1442 | 1678 | if !parked.is_empty() { |
| 1443 | 1679 | self.inbound.drained(peer, &parked); |
| 1444 | 1680 | } |