BlueGit

ict rusty-teams

Files

rusty-teams / crates / webview / src / park.rs

park.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
park.rs 3.9 KiB Download Raw
1use std::collections::HashMap;
2
3use session::App;
4
5const MAX_COMMAND_WAITS: usize = 5;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub(crate) enum ParkDecision {
9 Park,
10 Wait,
11}
12
13/// A navigation of the app tab destroys the page context of every running protocol call
14/// and the Trouter sockets of a live call, so parking waits until the tab is idle.
15#[derive(Debug, Default)]
16pub(crate) struct ParkGate {
17 holds: HashMap<App, usize>,
18 in_flight: HashMap<App, usize>,
19 command_waits: HashMap<App, usize>,
20}
21
22impl ParkGate {
23 pub(crate) fn hold(&mut self, app: App, held: bool) {
24 adjust(&mut self.holds, app, held);
25 }
26
27 pub(crate) fn held(&self, app: App) -> bool {
28 self.holds.get(&app).copied().unwrap_or(0) > 0
29 }
30
31 pub(crate) fn command_started(&mut self, app: App) {
32 adjust(&mut self.in_flight, app, true);
33 }
34
35 pub(crate) fn command_finished(&mut self, app: App) {
36 adjust(&mut self.in_flight, app, false);
37 }
38
39 pub(crate) fn forget_commands(&mut self) {
40 self.in_flight.clear();
41 }
42
43 pub(crate) fn in_flight(&self, app: App) -> usize {
44 self.in_flight.get(&app).copied().unwrap_or(0)
45 }
46
47 /// A protocol call whose page promise never settles stays in flight; after a few waits it no longer blocks the park.
48 pub(crate) fn decide(&mut self, app: App) -> ParkDecision {
49 if self.held(app) {
50 return ParkDecision::Wait;
51 }
52 let waits = self.command_waits.entry(app).or_default();
53 if self.in_flight.get(&app).copied().unwrap_or(0) > 0 && *waits < MAX_COMMAND_WAITS {
54 *waits += 1;
55 return ParkDecision::Wait;
56 }
57 *waits = 0;
58 ParkDecision::Park
59 }
60}
61
62fn adjust(counts: &mut HashMap<App, usize>, app: App, up: bool) {
63 let count = counts.entry(app).or_default();
64 *count = if up {
65 *count + 1
66 } else {
67 count.saturating_sub(1)
68 };
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn an_idle_tab_parks() {
77 assert_eq!(ParkGate::default().decide(App::Teams), ParkDecision::Park);
78 }
79
80 #[test]
81 fn a_running_command_delays_the_park_until_it_answers() {
82 let mut gate = ParkGate::default();
83 gate.command_started(App::Teams);
84 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
85 assert_eq!(gate.decide(App::Outlook), ParkDecision::Park);
86 gate.command_finished(App::Teams);
87 assert_eq!(gate.decide(App::Teams), ParkDecision::Park);
88 }
89
90 #[test]
91 fn a_command_that_never_answers_stops_blocking_after_a_few_waits() {
92 let mut gate = ParkGate::default();
93 gate.command_started(App::Teams);
94 for _ in 0..MAX_COMMAND_WAITS {
95 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
96 }
97 assert_eq!(gate.decide(App::Teams), ParkDecision::Park);
98 }
99
100 #[test]
101 fn a_held_tab_waits_for_every_hold_to_end() {
102 let mut gate = ParkGate::default();
103 gate.hold(App::Teams, true);
104 gate.hold(App::Teams, true);
105 gate.hold(App::Teams, false);
106 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
107 gate.hold(App::Teams, false);
108 assert_eq!(gate.decide(App::Teams), ParkDecision::Park);
109 }
110
111 #[test]
112 fn extra_releases_do_not_underflow() {
113 let mut gate = ParkGate::default();
114 gate.hold(App::Teams, false);
115 gate.command_finished(App::Teams);
116 gate.hold(App::Teams, true);
117 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
118 }
119
120 #[test]
121 fn a_restart_forgets_unanswered_commands_but_keeps_holds() {
122 let mut gate = ParkGate::default();
123 gate.command_started(App::Outlook);
124 gate.hold(App::Teams, true);
125 gate.forget_commands();
126 assert_eq!(gate.decide(App::Outlook), ParkDecision::Park);
127 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
128 }
129}