1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
//! The intervals the protocol runs on, derived from the keepalive.
use std::time::Duration;
use crate::backoff::Backoff;
/// First wait for a sync answer, unless a transport needs longer
/// (`Transport::sync_reply_timeout`); generous, since a transport may take a while with
/// large messages (Mycelium sends them in many small packets).
pub(crate) const SYNC_REPLY_TIMEOUT: Duration = Duration::from_secs(10);
/// Upper bound, as a multiple of the first wait: 5 minutes, about as long as a transport
/// tries to deliver a message (`Transport::delivery_limit`).
const SYNC_REPLY_TIMEOUT_MAX_FACTOR: u32 = 30;
/// Upper bound on the wait between probes of an unreachable peer, as a multiple of
/// `dormant_probe`, unless `--dormant-probe-max` says otherwise: an hour at the default
/// keepalive (1m). A returning node HELLOs its own neighbours, and once one pair across
/// a partition meets, the rest are probed at once (`handshake::probe_now`), so this
/// bounds how long one we never hear from stays out.
const PROBE_MAX_FACTOR: u32 = 6;
#[derive(Clone, Copy, Debug)]
pub(crate) struct Timing {
pub(crate) keepalive: Duration,
/// How often a quiet pair's keepalive is a STATUS rather than a PING
/// (`PeerState::take_keepalive_due`): its state (heads, readiness, epoch) for the heal
/// and against lost announcements. Changes go out as they happen anyway.
pub(crate) status_refresh: Duration,
pub(crate) dormant_after: Duration,
pub(crate) resync_after: Duration,
pub(crate) dormant_probe: Duration,
/// The longest wait between probes (`probe_backoff`).
pub(crate) probe_max: Duration,
/// While catching up: send the source a STATUS after this much silence (`swarm::source`).
pub(crate) catch_up_probe: Duration,
/// First wait for an answer to a sync message before sending again; doubles while
/// answers stay out (`SyncSession::reply_backoff`).
pub(crate) sync_reply_timeout: Duration,
/// How long what we sent a peer may go unanswered, with nothing else heard from it,
/// before we don't count it as reached (`swarm::reach`, `PeerState::answer_overdue`):
/// twice the first sync reply timeout, so one lost message (or its answer) doesn't
/// do it.
pub(crate) unreached_after: Duration,
/// How long the transport tries to deliver a message before dropping it
/// (`Transport::delivery_limit`): a peer silent for longer has nothing on the way to
/// us any more.
pub(crate) delivery_limit: Duration,
/// HyParView (`swarm::hyparview`): how long a member asked to be a neighbour has to
/// agree, the handshake before included. Twice the first sync reply timeout: over
/// Tor, a few round trips of up to 3 s.
pub(crate) ask_for: Duration,
/// HyParView: a member that refused, didn't answer, or left isn't asked again for this
/// long. A keepalive interval.
pub(crate) refused_for: Duration,
/// HyParView: a member one neighbour short asks this seldom (five keepalive
/// intervals): in a swarm whose members mostly have `k`, asking each in turn kept
/// NEIGHBOURs going in a quiet swarm (up to 300 in 20 s at 50 members). Two or more
/// short, it asks at once. A member with `k` takes it (it says it is one short), so one
/// ask is enough but where those asked have `k + 1`.
pub(crate) one_short_every: Duration,
/// HyParView: each neighbour is asked again this often whether it still is (NEIGHBOUR,
/// as a new ask would be), a keepalive interval: a NEIGHBOUR_REPLY or DISCONNECT lost
/// on a lossy link leaves a link in one end's view only, and nothing else would tell
/// that end: the other still answers it. A neighbour that has us agrees at once; one
/// that doesn't takes us if it has room, else refuses, and we drop it.
pub(crate) reaffirm_every: Duration,
}
impl Timing {
/// How long a round-trip sample stays meaningful (`swarm::rtt`): a few keepalive
/// intervals, since nodes move.
pub(crate) fn rtt_fresh_for(&self) -> Duration {
self.keepalive * 5
}
/// [`new`](Self::new) with the default first sync reply timeout.
#[cfg(test)]
pub(crate) fn from_keepalive(keepalive: Duration, delivery_limit: Duration) -> Self {
Self::new(keepalive, SYNC_REPLY_TIMEOUT, delivery_limit)
}
/// The timings for `--keepalive`.
///
/// Each node keeps about `k` neighbours (`--neighbours`) whatever the swarm's size,
/// and only one side of a pair sends them, so idle, a node sends about `k` messages and
/// receives about `k` per keepalive interval (more on a stable member others prefer):
/// PINGs and PONGs (a few bytes), every third a STATUS and STATUS_REPLY (heads,
/// readiness, epoch) instead. The interval trades that against how soon a silent peer is
/// noticed (`dormant_after`, 4 intervals) and a gap closes (within an interval).
/// Recommended:
///
/// | Environment | Keepalive | Notes |
/// |---|---|---|
/// | Tests | 250 ms – 2 s | dormancy within seconds keeps tests short |
/// | LAN, servers | 15–30 s | a returning or silent peer is noticed within a minute or two |
/// | WAN, mobile, metered (the default, 1m) | 60–120 s | about 0.4 messages per second per node at `k` = 12 and 1m |
///
/// `sync_reply_timeout`: the transports' (`Transport::sync_reply_timeout`), or for
/// tests, whose messages don't travel for seconds, a shorter one. `delivery_limit`:
/// the transports' (`Transport::delivery_limit`).
pub(crate) fn new(
keepalive: Duration,
sync_reply_timeout: Duration,
delivery_limit: Duration,
) -> Self {
Self {
keepalive,
// Every third keepalive; with `resync_after` at two keepalives, a heal needs
// two STATUSes that differ: within about two refreshes.
status_refresh: keepalive * 3,
dormant_after: keepalive * 4, // dormant_after >= 4* keepalive to tolerate three lost keepalives in a row
// >= 2 keepalives, so a second exchange confirms the difference; and >= twice
// the first sync reply timeout, so a lost message's sender resends it first.
resync_after: (keepalive * 2).max(sync_reply_timeout * 2),
dormant_probe: keepalive * 10, // >= 10*: dormant peers cost almost nothing, but still rejoin within minutes
probe_max: keepalive * 10 * PROBE_MAX_FACTOR,
// Seconds, not a keepalive interval: a dead source should cost little time.
// Only the source gets these, and only while catching up.
catch_up_probe: (keepalive / 30).clamp(Duration::from_secs(1), Duration::from_secs(5)),
sync_reply_timeout,
unreached_after: sync_reply_timeout * 2,
delivery_limit,
ask_for: sync_reply_timeout * 2,
refused_for: keepalive,
one_short_every: keepalive * 5,
reaffirm_every: keepalive,
}
}
/// `--dormant-probe-max`, if given: the longest wait between probes.
pub(crate) fn with_probe_max(mut self, max: Option<Duration>) -> Self {
if let Some(max) = max {
self.probe_max = max;
}
self
}
/// Waits between probes of an unreachable peer: `dormant_probe`, doubling up to
/// `probe_max` (all of them `probe_max` if that is shorter).
pub(crate) fn probe_backoff(&self) -> Backoff {
Backoff::new(self.dormant_probe.min(self.probe_max), self.probe_max)
}
/// Waits for sync answers: `sync_reply_timeout`, doubling up to 30 times as much.
pub(crate) fn sync_reply_backoff(&self) -> Backoff {
Backoff::new(
self.sync_reply_timeout,
self.sync_reply_timeout * SYNC_REPLY_TIMEOUT_MAX_FACTOR,
)
}
}
|