tor_circmgr/timeouts/
readonly.rs1use crate::timeouts::{pareto::ParetoTimeoutState, Action, TimeoutEstimator};
4use std::time::Duration;
5
6pub(crate) struct ReadonlyTimeoutEstimator {
9 using_estimates: bool,
11 latest_timeout: Option<Duration>,
13 default_timeout: Duration,
15}
16
17impl ReadonlyTimeoutEstimator {
18 pub(crate) fn new() -> Self {
20 ReadonlyTimeoutEstimator {
21 using_estimates: true,
22 latest_timeout: None,
23 default_timeout: Duration::from_secs(60),
24 }
25 }
26
27 pub(crate) fn from_state(s: &ParetoTimeoutState) -> Self {
29 let mut est = Self::new();
30 est.update_from_state(s);
31 est
32 }
33
34 pub(crate) fn update_from_state(&mut self, s: &ParetoTimeoutState) {
36 self.latest_timeout = s.latest_estimate();
37 }
38}
39
40impl TimeoutEstimator for ReadonlyTimeoutEstimator {
41 fn note_hop_completed(&mut self, _hop: u8, _delay: Duration, _is_last: bool) {
42 }
44
45 fn note_circ_timeout(&mut self, _hop: u8, _delay: Duration) {
46 }
48
49 fn timeouts(&mut self, action: &Action) -> (Duration, Duration) {
50 let base = match (self.using_estimates, self.latest_timeout) {
51 (true, Some(d)) => d,
52 (_, _) => self.default_timeout,
53 };
54
55 let reference_action = Action::BuildCircuit { length: 3 };
56 debug_assert!(reference_action.timeout_scale() > 0);
57
58 let multiplier =
59 (action.timeout_scale() as f64) / (reference_action.timeout_scale() as f64);
60
61 use super::mul_duration_f64_saturating as mul;
62 let timeout = mul(base, multiplier);
63
64 (timeout, timeout)
71 }
72
73 fn learning_timeouts(&self) -> bool {
74 false
75 }
76
77 fn update_params(&mut self, params: &tor_netdir::params::NetParameters) {
78 self.using_estimates = !bool::from(params.cbt_learning_disabled);
79 self.default_timeout = params
80 .cbt_initial_timeout
81 .try_into()
82 .unwrap_or_else(|_| Duration::from_secs(60));
83 }
84
85 fn build_state(&mut self) -> Option<ParetoTimeoutState> {
86 None
87 }
88}