fix(presence): keyboard counts as activity, permanent failures retry after 30 min
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -12,6 +12,7 @@ const FALLBACK_INTERVAL: Duration = Duration::from_secs(120);
12
12
const CHECK_INTERVAL: Duration = Duration::from_secs(30);
13
13
const RETRY_BASE: Duration = Duration::from_secs(30);
14
14
const RETRY_MAX: Duration = Duration::from_secs(240);
15
const PERMANENT_FAILURE_RETRY: Duration = Duration::from_secs(30 * 60);
15
16
16
17
static UNREGISTER: OnceLock<mpsc::UnboundedSender<oneshot::Sender<()>>> = OnceLock::new();
17
18
@@ -65,13 +66,12 @@ pub fn unregister_on_exit(timeout: Duration) {
65
66
struct RetryState {
66
67
failures: u32,
67
68
next_attempt: Option<Instant>,
68
blocked: bool,
69
69
last_error: Option<String>,
70
70
}
71
71
72
72
impl RetryState {
73
73
fn may_attempt(&self, now: Instant) -> bool {
74
!self.blocked && self.next_attempt.is_none_or(|next| now >= next)
74
self.next_attempt.is_none_or(|next| now >= next)
75
75
}
76
76
77
77
fn succeeded(&mut self) {
@@ -80,8 +80,12 @@ impl RetryState {
80
80
81
81
fn failed(&mut self, now: Instant, error: &chatsvc::Error) -> Option<String> {
82
82
self.failures += 1;
83
self.next_attempt = Some(now + retry_delay(self.failures));
84
self.blocked = is_permanent_failure(error_status(error));
83
let delay = if is_permanent_failure(error_status(error)) {
84
PERMANENT_FAILURE_RETRY
85
} else {
86
retry_delay(self.failures)
87
};
88
self.next_attempt = Some(now + delay);
85
89
let message = error.to_string();
86
90
let is_new = self.last_error.as_deref() != Some(message.as_str());
87
91
self.last_error = Some(message.clone());
@@ -335,12 +339,13 @@ mod tests {
335
339
}
336
340
337
341
#[tokio::test]
338
async fn a_permanent_failure_stops_retrying_until_a_new_endpoint() {
342
async fn a_permanent_failure_retries_only_after_half_an_hour_or_a_new_endpoint() {
339
343
let (mut link, sent, _) = link(vec![403]);
340
344
link.apply(LinkSignal::Endpoint("a".into())).await;
341
345
link.reconcile(false).await;
342
346
assert_eq!(calls(&sent).len(), 1);
343
assert!(!link.retry.may_attempt(Instant::now() + seconds(10_000)));
347
assert!(!link.retry.may_attempt(Instant::now() + seconds(29 * 60)));
348
assert!(link.retry.may_attempt(Instant::now() + seconds(31 * 60)));
344
349
link.apply(LinkSignal::Endpoint("b".into())).await;
345
350
assert_eq!(calls(&sent).len(), 3);
346
351
}
@@ -352,7 +357,7 @@ mod tests {
352
357
link.apply(LinkSignal::Endpoint("a".into())).await;
353
358
link.apply(LinkSignal::Connected).await;
354
359
assert_eq!(calls(&sent), ["PUT /endpoints/ Transport"]);
355
resend.notified().await;
360
tokio::time::timeout(Duration::from_millis(500), resend.notified()).await.expect("resend");
356
361
}
357
362
358
363
#[tokio::test]
@@ -370,8 +375,8 @@ mod tests {
370
375
async fn long_downtime_switches_to_direct_reporting_and_a_reconnect_switches_back() {
371
376
let (mut link, sent, resend) = link(vec![]);
372
377
link.apply(LinkSignal::Endpoint("a".into())).await;
373
resend.notified().await;
374
link.down_since = Some(Instant::now() - seconds(130));
378
tokio::time::timeout(Duration::from_millis(500), resend.notified()).await.expect("resend");
379
link.down_since = Some(Instant::now().checked_sub(seconds(130)).unwrap_or_else(Instant::now));
375
380
link.reconcile(false).await;
376
381
link.apply(LinkSignal::Connected).await;
377
382
assert_eq!(
@@ -383,7 +388,7 @@ mod tests {
383
388
"PUT /endpoints/ Transport",
384
389
]
385
390
);
386
resend.notified().await;
391
tokio::time::timeout(Duration::from_millis(500), resend.notified()).await.expect("resend");
387
392
}
388
393
389
394
#[tokio::test]
@@ -12,6 +12,7 @@ const FALLBACK_INTERVAL: Duration = Duration::from_secs(120);12
const CHECK_INTERVAL: Duration = Duration::from_secs(30);12
const CHECK_INTERVAL: Duration = Duration::from_secs(30);13
const RETRY_BASE: Duration = Duration::from_secs(30);13
const RETRY_BASE: Duration = Duration::from_secs(30);14
const RETRY_MAX: Duration = Duration::from_secs(240);14
const RETRY_MAX: Duration = Duration::from_secs(240);15
const PERMANENT_FAILURE_RETRY: Duration = Duration::from_secs(30 * 60);15
16
16
static UNREGISTER: OnceLock<mpsc::UnboundedSender<oneshot::Sender<()>>> = OnceLock::new();17
static UNREGISTER: OnceLock<mpsc::UnboundedSender<oneshot::Sender<()>>> = OnceLock::new();17
18
@@ -65,13 +66,12 @@ pub fn unregister_on_exit(timeout: Duration) {65
struct RetryState {66
struct RetryState {66
failures: u32,67
failures: u32,67
next_attempt: Option<Instant>,68
next_attempt: Option<Instant>,68
blocked: bool,69
last_error: Option<String>,69
last_error: Option<String>,70
}70
}71
71
72
impl RetryState {72
impl RetryState {73
fn may_attempt(&self, now: Instant) -> bool {73
fn may_attempt(&self, now: Instant) -> bool {74
!self.blocked && self.next_attempt.is_none_or(|next| now >= next)74
self.next_attempt.is_none_or(|next| now >= next)75
}75
}76
76
77
fn succeeded(&mut self) {77
fn succeeded(&mut self) {@@ -80,8 +80,12 @@ impl RetryState {80
80
81
fn failed(&mut self, now: Instant, error: &chatsvc::Error) -> Option<String> {81
fn failed(&mut self, now: Instant, error: &chatsvc::Error) -> Option<String> {82
self.failures += 1;82
self.failures += 1;83
self.next_attempt = Some(now + retry_delay(self.failures));83
let delay = if is_permanent_failure(error_status(error)) {84
self.blocked = is_permanent_failure(error_status(error));84
PERMANENT_FAILURE_RETRY85
} else {86
retry_delay(self.failures)87
};88
self.next_attempt = Some(now + delay);85
let message = error.to_string();89
let message = error.to_string();86
let is_new = self.last_error.as_deref() != Some(message.as_str());90
let is_new = self.last_error.as_deref() != Some(message.as_str());87
self.last_error = Some(message.clone());91
self.last_error = Some(message.clone());@@ -335,12 +339,13 @@ mod tests {335
}339
}336
340
337
#[tokio::test]341
#[tokio::test]338
async fn a_permanent_failure_stops_retrying_until_a_new_endpoint() {342
async fn a_permanent_failure_retries_only_after_half_an_hour_or_a_new_endpoint() {339
let (mut link, sent, _) = link(vec![403]);343
let (mut link, sent, _) = link(vec![403]);340
link.apply(LinkSignal::Endpoint("a".into())).await;344
link.apply(LinkSignal::Endpoint("a".into())).await;341
link.reconcile(false).await;345
link.reconcile(false).await;342
assert_eq!(calls(&sent).len(), 1);346
assert_eq!(calls(&sent).len(), 1);343
assert!(!link.retry.may_attempt(Instant::now() + seconds(10_000)));347
assert!(!link.retry.may_attempt(Instant::now() + seconds(29 * 60)));348
assert!(link.retry.may_attempt(Instant::now() + seconds(31 * 60)));344
link.apply(LinkSignal::Endpoint("b".into())).await;349
link.apply(LinkSignal::Endpoint("b".into())).await;345
assert_eq!(calls(&sent).len(), 3);350
assert_eq!(calls(&sent).len(), 3);346
}351
}@@ -352,7 +357,7 @@ mod tests {352
link.apply(LinkSignal::Endpoint("a".into())).await;357
link.apply(LinkSignal::Endpoint("a".into())).await;353
link.apply(LinkSignal::Connected).await;358
link.apply(LinkSignal::Connected).await;354
assert_eq!(calls(&sent), ["PUT /endpoints/ Transport"]);359
assert_eq!(calls(&sent), ["PUT /endpoints/ Transport"]);355
resend.notified().await;360
tokio::time::timeout(Duration::from_millis(500), resend.notified()).await.expect("resend");356
}361
}357
362
358
#[tokio::test]363
#[tokio::test]@@ -370,8 +375,8 @@ mod tests {370
async fn long_downtime_switches_to_direct_reporting_and_a_reconnect_switches_back() {375
async fn long_downtime_switches_to_direct_reporting_and_a_reconnect_switches_back() {371
let (mut link, sent, resend) = link(vec![]);376
let (mut link, sent, resend) = link(vec![]);372
link.apply(LinkSignal::Endpoint("a".into())).await;377
link.apply(LinkSignal::Endpoint("a".into())).await;373
resend.notified().await;378
tokio::time::timeout(Duration::from_millis(500), resend.notified()).await.expect("resend");374
link.down_since = Some(Instant::now() - seconds(130));379
link.down_since = Some(Instant::now().checked_sub(seconds(130)).unwrap_or_else(Instant::now));375
link.reconcile(false).await;380
link.reconcile(false).await;376
link.apply(LinkSignal::Connected).await;381
link.apply(LinkSignal::Connected).await;377
assert_eq!(382
assert_eq!(@@ -383,7 +388,7 @@ mod tests {383
"PUT /endpoints/ Transport",388
"PUT /endpoints/ Transport",384
]389
]385
);390
);386
resend.notified().await;391
tokio::time::timeout(Duration::from_millis(500), resend.notified()).await.expect("resend");387
}392
}388
393
389
#[tokio::test]394
#[tokio::test]@@ -2,7 +2,7 @@ use std::sync::Arc;
2
2
use std::sync::atomic::{AtomicU64, Ordering};
3
3
use std::time::{Duration, Instant};
4
4
5
use gpui_kit::{DispatchPhase, IntoElement, MouseDownEvent, MouseMoveEvent, ScrollWheelEvent, canvas};
5
use gpui_kit::{DispatchPhase, IntoElement, KeyDownEvent, MouseDownEvent, MouseMoveEvent, ScrollWheelEvent, canvas};
6
6
use tokio::sync::watch;
7
7
8
8
pub const IDLE_AFTER: Duration = Duration::from_secs(5 * 60);
@@ -58,6 +58,12 @@ pub fn input_probe(input: Arc<UserInput>) -> impl IntoElement {
58
58
scrolled.touch();
59
59
}
60
60
});
61
let typed = input.clone();
62
window.on_key_event(move |_: &KeyDownEvent, phase, _, _| {
63
if phase == DispatchPhase::Capture {
64
typed.touch();
65
}
66
});
61
67
},
62
68
)
63
69
}
@@ -2,7 +2,7 @@ use std::sync::Arc;2
use std::sync::atomic::{AtomicU64, Ordering};2
use std::sync::atomic::{AtomicU64, Ordering};3
use std::time::{Duration, Instant};3
use std::time::{Duration, Instant};4
4
5
use gpui_kit::{DispatchPhase, IntoElement, MouseDownEvent, MouseMoveEvent, ScrollWheelEvent, canvas};5
use gpui_kit::{DispatchPhase, IntoElement, KeyDownEvent, MouseDownEvent, MouseMoveEvent, ScrollWheelEvent, canvas};6
use tokio::sync::watch;6
use tokio::sync::watch;7
7
8
pub const IDLE_AFTER: Duration = Duration::from_secs(5 * 60);8
pub const IDLE_AFTER: Duration = Duration::from_secs(5 * 60);@@ -58,6 +58,12 @@ pub fn input_probe(input: Arc<UserInput>) -> impl IntoElement {58
scrolled.touch();58
scrolled.touch();59
}59
}60
});60
});61
let typed = input.clone();62
window.on_key_event(move |_: &KeyDownEvent, phase, _, _| {63
if phase == DispatchPhase::Capture {64
typed.touch();65
}66
});61
},67
},62
)68
)63
}69
}