BlueGit

ict rusty-teams

Files

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

park.rs

park.rs 3.8 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 gate.command_finished(App::Teams);
86 assert_eq!(gate.decide(App::Teams), ParkDecision::Park);
87 }
88
89 #[test]
90 fn a_command_that_never_answers_stops_blocking_after_a_few_waits() {
91 let mut gate = ParkGate::default();
92 gate.command_started(App::Teams);
93 for _ in 0..MAX_COMMAND_WAITS {
94 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
95 }
96 assert_eq!(gate.decide(App::Teams), ParkDecision::Park);
97 }
98
99 #[test]
100 fn a_held_tab_waits_for_every_hold_to_end() {
101 let mut gate = ParkGate::default();
102 gate.hold(App::Teams, true);
103 gate.hold(App::Teams, true);
104 gate.hold(App::Teams, false);
105 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
106 gate.hold(App::Teams, false);
107 assert_eq!(gate.decide(App::Teams), ParkDecision::Park);
108 }
109
110 #[test]
111 fn extra_releases_do_not_underflow() {
112 let mut gate = ParkGate::default();
113 gate.hold(App::Teams, false);
114 gate.command_finished(App::Teams);
115 gate.hold(App::Teams, true);
116 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
117 }
118
119 #[test]
120 fn a_restart_forgets_unanswered_commands_but_keeps_holds() {
121 let mut gate = ParkGate::default();
122 gate.command_started(App::Teams);
123 gate.forget_commands();
124 assert_eq!(gate.decide(App::Teams), ParkDecision::Park);
125 gate.hold(App::Teams, true);
126 gate.forget_commands();
127 assert_eq!(gate.decide(App::Teams), ParkDecision::Wait);
128 }
129}