mux: fix poll interval. show update indicator

Add an update indicator to the top right of client tabs; this is
overlaid on top of the surface when the last update from the server was
more than ~3s ago and if we expected it sooner than that.

While making this work, I noticed that the exponential poll backoff
had gotten broken in an earlier refactor; instead of a series of polls
backing off slowly, we were aggressively running the backoff up to the
max 30 second interval over the span of a few ms.   This commit fixes
up the backoff computation to only happen when we are ready to send
a poll.

refs: https://github.com/wez/wezterm/issues/127
This commit is contained in:
Wez Furlong committed 2020-02-23 09:08:04 -08:00
1 parent c9064c3f90
commit 65749f06a3
4 files changed
+114 -10

No files matched your search

+18 -1
View File
@@ -146,6 +146,11 @@ impl Tab for ClientTab {
})
.await
});
self.renderable
.borrow()
.inner
.borrow_mut()
.update_last_send();
Ok(())
}
@@ -183,6 +188,7 @@ impl Tab for ClientTab {
})
.await
});
inner.update_last_send();
}
Ok(())
}
@@ -202,12 +208,23 @@ impl Tab for ClientTab {
})
.await
});
self.renderable
.borrow()
.inner
.borrow_mut()
.update_last_send();
Ok(())
}
fn mouse_event(&self, event: MouseEvent, _host: &mut dyn TerminalHost) -> anyhow::Result<()> {
self.mouse.borrow_mut().append(event);
MouseState::next(Rc::clone(&self.mouse));
if MouseState::next(Rc::clone(&self.mouse)) {
self.renderable
.borrow()
.inner
.borrow_mut()
.update_last_send();
}
Ok(())
}
+4 -1
View File
@@ -64,7 +64,7 @@ impl MouseState {
}
}
pub fn next(state: Rc<RefCell<Self>>) {
pub fn next(state: Rc<RefCell<Self>>) -> bool {
let mut mouse = state.borrow_mut();
if let Some(event) = mouse.pop() {
let client = mouse.client.clone();
@@ -89,6 +89,9 @@ impl MouseState {
Self::next(Rc::clone(&state));
Ok::<(), anyhow::Error>(())
});
true
} else {
false
}
}
}
+73 -8
View File
@@ -16,6 +16,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use term::{Line, StableRowIndex};
use termwiz::cell::CellAttributes;
use termwiz::color::AnsiColor;
use url::Url;
const MAX_POLL_INTERVAL: Duration = Duration::from_secs(30);
@@ -67,6 +69,10 @@ pub struct RenderableInner {
pub working_dir: Option<Url>,
fetch_limiter: RateLimiter,
last_send_time: Instant,
last_recv_time: Instant,
last_late_dirty: Instant,
}
pub struct RenderableState {
@@ -82,11 +88,13 @@ impl RenderableInner {
title: &str,
fetch_limiter: RateLimiter,
) -> Self {
let now = Instant::now();
Self {
client: Arc::clone(client),
remote_tab_id,
local_tab_id,
last_poll: Instant::now(),
last_poll: now,
dead: false,
poll_in_progress: AtomicBool::new(false),
poll_interval: BASE_POLL_INTERVAL,
@@ -96,11 +104,34 @@ impl RenderableInner {
title: title.to_string(),
working_dir: None,
fetch_limiter,
last_send_time: now,
last_recv_time: now,
last_late_dirty: now,
}
}
pub fn apply_changes_to_surface(&mut self, delta: GetTabRenderChangesResponse) {
/// Returns true if we think we should display the laggy connection
/// indicator. If we're past our poll interval and more recently
/// tried to send something than receive something, the UI is worth
/// showing.
fn is_tardy(&self) -> bool {
let elapsed = self.last_recv_time.elapsed();
if elapsed > self.poll_interval.max(Duration::from_secs(3)) {
self.last_send_time > self.last_recv_time
} else {
false
}
}
pub fn update_last_send(&mut self) {
self.last_send_time = Instant::now();
self.poll_interval = BASE_POLL_INTERVAL;
}
pub fn apply_changes_to_surface(&mut self, delta: GetTabRenderChangesResponse) {
let now = Instant::now();
self.poll_interval = BASE_POLL_INTERVAL;
self.last_recv_time = now;
let mut dirty = RangeSet::new();
for r in delta.dirty_lines {
@@ -130,7 +161,6 @@ impl RenderableInner {
.notify(crate::mux::MuxNotification::TabOutput(self.local_tab_id));
}
let now = Instant::now();
let mut to_fetch = RangeSet::new();
for r in dirty.iter() {
for stable_row in r.clone() {
@@ -334,15 +364,14 @@ impl RenderableInner {
return Ok(());
}
if self.last_poll.elapsed() < self.poll_interval {
return Ok(());
}
let interval = self.poll_interval;
let interval = (interval + interval).min(MAX_POLL_INTERVAL);
self.poll_interval = interval;
let last = self.last_poll;
if last.elapsed() < self.poll_interval {
return Ok(());
}
self.last_poll = Instant::now();
self.poll_in_progress.store(true, Ordering::SeqCst);
let remote_tab_id = self.remote_tab_id;
@@ -366,6 +395,7 @@ impl RenderableInner {
let mut inner = renderable.inner.borrow_mut();
inner.dead = !alive;
inner.last_recv_time = Instant::now();
inner.poll_in_progress.store(false, Ordering::SeqCst);
}
Ok::<(), anyhow::Error>(())
@@ -415,6 +445,30 @@ impl Renderable for RenderableState {
LineEntry::Fetching(now)
}
};
if idx == inner.dimensions.physical_top {
if inner.is_tardy() {
let status = format!(
"wezterm: {:.0?}⏳since last response",
inner.last_recv_time.elapsed()
);
// Right align it in the tab
let col = inner
.dimensions
.cols
.saturating_sub(term::unicode_column_width(&status));
let mut attr = CellAttributes::default();
attr.foreground = AnsiColor::White.into();
attr.background = AnsiColor::Blue.into();
result
.last_mut()
.unwrap()
.overlay_text_with_attribute(col, &status, attr);
}
}
inner.lines.put(idx, entry);
}
@@ -445,6 +499,17 @@ impl Renderable for RenderableState {
}
}
// If we're behind receiving an update, invalidate the top row so
// that the indicator will update in a more timely fashion
if inner.is_tardy() {
// ... but take care to avoid always reporting it as dirty, so
// that we don't end up busy looping just to repaint it
if inner.last_late_dirty.elapsed() >= Duration::from_secs(1) {
result.add(inner.dimensions.physical_top);
inner.last_late_dirty = Instant::now();
}
}
if !result.is_empty() {
log::trace!("get_dirty_lines: {:?}", result);
}
+19
View File
@@ -304,6 +304,25 @@ impl Line {
&self.cells[idx]
}
/// Place text starting at the specified column index.
/// Each grapheme of the text run has the same attributes.
pub fn overlay_text_with_attribute(
&mut self,
mut start_idx: usize,
text: &str,
attr: CellAttributes,
) {
for (i, c) in text.graphemes(true).enumerate() {
let cell = Cell::new_grapheme(c, attr.clone());
let width = cell.width();
self.set_cell(i + start_idx, cell);
// Compensate for required spacing/placement of
// double width characters
start_idx += width.saturating_sub(1);
}
}
fn invalidate_grapheme_at_or_before(&mut self, idx: usize) {
// Assumption: that the width of a grapheme is never > 2.
// This constrains the amount of look-back that we need to do here.