chore(webview): log every 10 min which calls keep the web views awake
- label: app plus method, or host and first two path segments of the fetched URL, or "token lookup" - no query strings, ids or tokens Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -25,7 +25,7 @@ use crate::cdp_failure::{CallContext, failure_reason, tab_state};
25
25
use crate::fanout::Subscribers;
26
26
use crate::host_dialog::{self, Dialog};
27
27
use crate::park::{ParkDecision, ParkGate};
28
use crate::sleep::{Activity, Sleep, blocker};
28
use crate::sleep::{Activity, CallTally, Sleep, blocker, call_label};
29
29
use crate::transport::{HostState, UiRequest, WebViewTransport};
30
30
use crate::{BROWSER_ARGUMENTS, FORWARDED_EVENTS};
31
31
@@ -38,6 +38,7 @@ const PARK_TIMER: usize = 2;
38
38
const PARK_DELAY_MS: u32 = 60_000;
39
39
const SLEEP_TIMER: usize = 3;
40
40
const SLEEP_CHECK_MS: u32 = 60_000;
41
const BUSY_REPORT_EVERY: u32 = 10;
41
42
const LOGIN_PROMPT_INTERVAL: Duration = Duration::from_secs(600);
42
43
const WINDOW_WIDTH: i32 = 1100;
43
44
const WINDOW_HEIGHT: i32 = 800;
@@ -68,6 +69,8 @@ pub(crate) struct Shared {
68
69
last_login_prompt: Cell<Option<Instant>>,
69
70
sleep: RefCell<Sleep<TabCommand>>,
70
71
sleep_blocker: Cell<Option<&'static str>>,
72
calls: RefCell<CallTally>,
73
busy_checks: Cell<u32>,
71
74
}
72
75
73
76
thread_local! {
@@ -131,6 +134,8 @@ fn run(
131
134
last_login_prompt: Cell::new(None),
132
135
sleep: RefCell::new(Sleep::default()),
133
136
sleep_blocker: Cell::new(None),
137
calls: RefCell::new(CallTally::default()),
138
busy_checks: Cell::new(0),
134
139
});
135
140
SHARED.with(|slot| *slot.borrow_mut() = Some(shared.clone()));
136
141
load(&shared);
@@ -483,6 +488,7 @@ fn drain(shared: &Rc<Shared>) {
483
488
484
489
fn call(shared: &Rc<Shared>, app: App, command: TabCommand) {
485
490
shared.sleep.borrow_mut().touch(Instant::now());
491
shared.calls.borrow_mut().note(format!("{app} {}", call_label(&command.method, &command.params)));
486
492
wake_views(shared, None);
487
493
let Some(command) = shared.sleep.borrow_mut().admit(app, command) else {
488
494
return;
@@ -627,8 +633,15 @@ fn maybe_sleep(shared: &Rc<Shared>) {
627
633
subscribed: shared.subscribers.borrow_mut().has_live(),
628
634
};
629
635
if !shared.sleep.borrow().quiet(Instant::now()) {
636
let checks = shared.busy_checks.get() + 1;
637
shared.busy_checks.set(checks % BUSY_REPORT_EVERY);
638
if checks == BUSY_REPORT_EVERY && !shared.sleep.borrow().asleep() {
639
let summary = shared.calls.borrow_mut().summary();
640
(shared.config.log)(&format!("webview: busy, calls in the last {BUSY_REPORT_EVERY} min: {summary}"));
641
}
630
642
return;
631
643
}
644
shared.busy_checks.set(0);
632
645
let blocked = blocker(activity);
633
646
if blocked != shared.sleep_blocker.replace(blocked)
634
647
&& let Some(reason) = blocked
@@ -25,7 +25,7 @@ use crate::cdp_failure::{CallContext, failure_reason, tab_state};25
use crate::fanout::Subscribers;25
use crate::fanout::Subscribers;26
use crate::host_dialog::{self, Dialog};26
use crate::host_dialog::{self, Dialog};27
use crate::park::{ParkDecision, ParkGate};27
use crate::park::{ParkDecision, ParkGate};28
use crate::sleep::{Activity, Sleep, blocker};28
use crate::sleep::{Activity, CallTally, Sleep, blocker, call_label};29
use crate::transport::{HostState, UiRequest, WebViewTransport};29
use crate::transport::{HostState, UiRequest, WebViewTransport};30
use crate::{BROWSER_ARGUMENTS, FORWARDED_EVENTS};30
use crate::{BROWSER_ARGUMENTS, FORWARDED_EVENTS};31
31
@@ -38,6 +38,7 @@ const PARK_TIMER: usize = 2;38
const PARK_DELAY_MS: u32 = 60_000;38
const PARK_DELAY_MS: u32 = 60_000;39
const SLEEP_TIMER: usize = 3;39
const SLEEP_TIMER: usize = 3;40
const SLEEP_CHECK_MS: u32 = 60_000;40
const SLEEP_CHECK_MS: u32 = 60_000;41
const BUSY_REPORT_EVERY: u32 = 10;41
const LOGIN_PROMPT_INTERVAL: Duration = Duration::from_secs(600);42
const LOGIN_PROMPT_INTERVAL: Duration = Duration::from_secs(600);42
const WINDOW_WIDTH: i32 = 1100;43
const WINDOW_WIDTH: i32 = 1100;43
const WINDOW_HEIGHT: i32 = 800;44
const WINDOW_HEIGHT: i32 = 800;@@ -68,6 +69,8 @@ pub(crate) struct Shared {68
last_login_prompt: Cell<Option<Instant>>,69
last_login_prompt: Cell<Option<Instant>>,69
sleep: RefCell<Sleep<TabCommand>>,70
sleep: RefCell<Sleep<TabCommand>>,70
sleep_blocker: Cell<Option<&'static str>>,71
sleep_blocker: Cell<Option<&'static str>>,72
calls: RefCell<CallTally>,73
busy_checks: Cell<u32>,71
}74
}72
75
73
thread_local! {76
thread_local! {@@ -131,6 +134,8 @@ fn run(131
last_login_prompt: Cell::new(None),134
last_login_prompt: Cell::new(None),132
sleep: RefCell::new(Sleep::default()),135
sleep: RefCell::new(Sleep::default()),133
sleep_blocker: Cell::new(None),136
sleep_blocker: Cell::new(None),137
calls: RefCell::new(CallTally::default()),138
busy_checks: Cell::new(0),134
});139
});135
SHARED.with(|slot| *slot.borrow_mut() = Some(shared.clone()));140
SHARED.with(|slot| *slot.borrow_mut() = Some(shared.clone()));136
load(&shared);141
load(&shared);@@ -483,6 +488,7 @@ fn drain(shared: &Rc<Shared>) {483
488
484
fn call(shared: &Rc<Shared>, app: App, command: TabCommand) {489
fn call(shared: &Rc<Shared>, app: App, command: TabCommand) {485
shared.sleep.borrow_mut().touch(Instant::now());490
shared.sleep.borrow_mut().touch(Instant::now());491
shared.calls.borrow_mut().note(format!("{app} {}", call_label(&command.method, &command.params)));486
wake_views(shared, None);492
wake_views(shared, None);487
let Some(command) = shared.sleep.borrow_mut().admit(app, command) else {493
let Some(command) = shared.sleep.borrow_mut().admit(app, command) else {488
return;494
return;@@ -627,8 +633,15 @@ fn maybe_sleep(shared: &Rc<Shared>) {627
subscribed: shared.subscribers.borrow_mut().has_live(),633
subscribed: shared.subscribers.borrow_mut().has_live(),628
};634
};629
if !shared.sleep.borrow().quiet(Instant::now()) {635
if !shared.sleep.borrow().quiet(Instant::now()) {636
let checks = shared.busy_checks.get() + 1;637
shared.busy_checks.set(checks % BUSY_REPORT_EVERY);638
if checks == BUSY_REPORT_EVERY && !shared.sleep.borrow().asleep() {639
let summary = shared.calls.borrow_mut().summary();640
(shared.config.log)(&format!("webview: busy, calls in the last {BUSY_REPORT_EVERY} min: {summary}"));641
}630
return;642
return;631
}643
}644
shared.busy_checks.set(0);632
let blocked = blocker(activity);645
let blocked = blocker(activity);633
if blocked != shared.sleep_blocker.replace(blocked)646
if blocked != shared.sleep_blocker.replace(blocked)634
&& let Some(reason) = blocked647
&& let Some(reason) = blocked@@ -17,6 +17,40 @@ pub(crate) struct Activity {
17
17
pub subscribed: bool,
18
18
}
19
19
20
/// Names a protocol call without its content: the method, or for a script the host and first path segments it fetches.
21
pub(crate) fn call_label(method: &str, params: &serde_json::Value) -> String {
22
if method != "Runtime.evaluate" {
23
return method.to_owned();
24
}
25
let expression = params.get("expression").and_then(serde_json::Value::as_str).unwrap_or_default();
26
if let Some(start) = expression.find("\"url\":\"https://") {
27
let rest = &expression[start + "\"url\":\"https://".len()..];
28
let end = rest.find(['"', '?', '\\']).unwrap_or(rest.len());
29
return rest[..end].split('/').take(3).collect::<Vec<_>>().join("/");
30
}
31
if expression.contains("return token;") {
32
return "token lookup".to_owned();
33
}
34
"script".to_owned()
35
}
36
37
#[derive(Debug, Default)]
38
pub(crate) struct CallTally {
39
counts: HashMap<String, u32>,
40
}
41
42
impl CallTally {
43
pub(crate) fn note(&mut self, label: String) {
44
*self.counts.entry(label).or_default() += 1;
45
}
46
47
pub(crate) fn summary(&mut self) -> String {
48
let mut counts: Vec<(String, u32)> = self.counts.drain().collect();
49
counts.sort_by(|first, second| second.1.cmp(&first.1).then_with(|| first.0.cmp(&second.0)));
50
counts.iter().take(6).map(|(label, count)| format!("{label} x{count}")).collect::<Vec<_>>().join(", ")
51
}
52
}
53
20
54
pub(crate) fn blocker(activity: Activity) -> Option<&'static str> {
21
55
[
22
56
(activity.restarting, "restarting"),
@@ -133,6 +167,21 @@ mod tests {
133
167
}
134
168
}
135
169
170
#[test]
171
fn calls_are_named_by_what_they_fetch() {
172
let fetch = serde_json::json!({"expression": "(async () => {})({\"requests\":[{\"method\":\"GET\",\"url\":\"https://teams.cloud.microsoft/api/csa/emea/api/v1/x?y=1\"}]})"});
173
assert_eq!(call_label("Runtime.evaluate", &fetch), "teams.cloud.microsoft/api/csa");
174
let token = serde_json::json!({"expression": "x; return token;"});
175
assert_eq!(call_label("Runtime.evaluate", &token), "token lookup");
176
assert_eq!(call_label("Page.reload", &serde_json::Value::Null), "Page.reload");
177
let mut tally = CallTally::default();
178
tally.note("b".into());
179
tally.note("a".into());
180
tally.note("a".into());
181
assert_eq!(tally.summary(), "a x2, b x1");
182
assert_eq!(tally.summary(), "");
183
}
184
136
185
#[test]
137
186
fn the_first_blocker_is_named() {
138
187
assert_eq!(blocker(idle()), None);
@@ -17,6 +17,40 @@ pub(crate) struct Activity {17
pub subscribed: bool,17
pub subscribed: bool,18
}18
}19
19
20
/// Names a protocol call without its content: the method, or for a script the host and first path segments it fetches.21
pub(crate) fn call_label(method: &str, params: &serde_json::Value) -> String {22
if method != "Runtime.evaluate" {23
return method.to_owned();24
}25
let expression = params.get("expression").and_then(serde_json::Value::as_str).unwrap_or_default();26
if let Some(start) = expression.find("\"url\":\"https://") {27
let rest = &expression[start + "\"url\":\"https://".len()..];28
let end = rest.find(['"', '?', '\\']).unwrap_or(rest.len());29
return rest[..end].split('/').take(3).collect::<Vec<_>>().join("/");30
}31
if expression.contains("return token;") {32
return "token lookup".to_owned();33
}34
"script".to_owned()35
}36
37
#[derive(Debug, Default)]38
pub(crate) struct CallTally {39
counts: HashMap<String, u32>,40
}41
42
impl CallTally {43
pub(crate) fn note(&mut self, label: String) {44
*self.counts.entry(label).or_default() += 1;45
}46
47
pub(crate) fn summary(&mut self) -> String {48
let mut counts: Vec<(String, u32)> = self.counts.drain().collect();49
counts.sort_by(|first, second| second.1.cmp(&first.1).then_with(|| first.0.cmp(&second.0)));50
counts.iter().take(6).map(|(label, count)| format!("{label} x{count}")).collect::<Vec<_>>().join(", ")51
}52
}53
20
pub(crate) fn blocker(activity: Activity) -> Option<&'static str> {54
pub(crate) fn blocker(activity: Activity) -> Option<&'static str> {21
[55
[22
(activity.restarting, "restarting"),56
(activity.restarting, "restarting"),@@ -133,6 +167,21 @@ mod tests {133
}167
}134
}168
}135
169
170
#[test]171
fn calls_are_named_by_what_they_fetch() {172
let fetch = serde_json::json!({"expression": "(async () => {})({\"requests\":[{\"method\":\"GET\",\"url\":\"https://teams.cloud.microsoft/api/csa/emea/api/v1/x?y=1\"}]})"});173
assert_eq!(call_label("Runtime.evaluate", &fetch), "teams.cloud.microsoft/api/csa");174
let token = serde_json::json!({"expression": "x; return token;"});175
assert_eq!(call_label("Runtime.evaluate", &token), "token lookup");176
assert_eq!(call_label("Page.reload", &serde_json::Value::Null), "Page.reload");177
let mut tally = CallTally::default();178
tally.note("b".into());179
tally.note("a".into());180
tally.note("a".into());181
assert_eq!(tally.summary(), "a x2, b x1");182
assert_eq!(tally.summary(), "");183
}184
136
#[test]185
#[test]137
fn the_first_blocker_is_named() {186
fn the_first_blocker_is_named() {138
assert_eq!(blocker(idle()), None);187
assert_eq!(blocker(idle()), None);