improve latency for server -> client push
This commit is contained in:
1 parent
97daa8b246
commit
26cdb6c87f
5 files changed
+74
-16
No files matched your search
+28
-3
@@ -1,5 +1,9 @@
|
||||
use crate::config::Config;
|
||||
use crate::frontend::gui_executor;
|
||||
use crate::mux::tab::{Tab, TabId};
|
||||
use crate::mux::window::{Window, WindowId};
|
||||
use crate::server::pollable::{pollable_channel, PollableReceiver, PollableSender};
|
||||
use domain::{Domain, DomainId};
|
||||
use failure::{format_err, Error, Fallible};
|
||||
use failure_derive::*;
|
||||
use log::{debug, error, warn};
|
||||
@@ -9,6 +13,7 @@ use std::cell::{Ref, RefCell, RefMut};
|
||||
use std::collections::HashMap;
|
||||
use std::io::Read;
|
||||
use std::rc::Rc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use term::TerminalHost;
|
||||
@@ -19,9 +24,14 @@ pub mod renderable;
|
||||
pub mod tab;
|
||||
pub mod window;
|
||||
|
||||
use crate::mux::tab::{Tab, TabId};
|
||||
use crate::mux::window::{Window, WindowId};
|
||||
use domain::{Domain, DomainId};
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum MuxNotification {
|
||||
TabOutput(TabId),
|
||||
}
|
||||
|
||||
static SUB_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
pub type MuxSubscriber = PollableReceiver<MuxNotification>;
|
||||
|
||||
pub struct Mux {
|
||||
tabs: RefCell<HashMap<TabId, Rc<dyn Tab>>>,
|
||||
@@ -29,6 +39,7 @@ pub struct Mux {
|
||||
config: Arc<Config>,
|
||||
default_domain: Arc<dyn Domain>,
|
||||
domains: RefCell<HashMap<DomainId, Arc<dyn Domain>>>,
|
||||
subscribers: RefCell<HashMap<usize, PollableSender<MuxNotification>>>,
|
||||
}
|
||||
|
||||
fn read_from_tab_pty(tab_id: TabId, mut reader: Box<dyn std::io::Read>) {
|
||||
@@ -56,6 +67,7 @@ fn read_from_tab_pty(tab_id: TabId, mut reader: Box<dyn std::io::Read>) {
|
||||
writer: &mut *tab.writer(),
|
||||
},
|
||||
);
|
||||
mux.notify(MuxNotification::TabOutput(tab_id));
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
@@ -116,9 +128,22 @@ impl Mux {
|
||||
config: Arc::clone(config),
|
||||
default_domain: Arc::clone(default_domain),
|
||||
domains: RefCell::new(domains),
|
||||
subscribers: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscribe(&self) -> Fallible<MuxSubscriber> {
|
||||
let sub_id = SUB_ID.fetch_add(1, Ordering::Relaxed);
|
||||
let (tx, rx) = pollable_channel()?;
|
||||
self.subscribers.borrow_mut().insert(sub_id, tx);
|
||||
Ok(rx)
|
||||
}
|
||||
|
||||
pub fn notify(&self, notification: MuxNotification) {
|
||||
let mut subscribers = self.subscribers.borrow_mut();
|
||||
subscribers.retain(|_, tx| tx.send(notification.clone()).is_ok());
|
||||
}
|
||||
|
||||
pub fn default_domain(&self) -> &Arc<dyn Domain> {
|
||||
&self.default_domain
|
||||
}
|
||||
|
||||
@@ -102,11 +102,6 @@ fn client_thread(
|
||||
|
||||
let mut poll_array = [rx.as_poll_fd(), stream.as_poll_fd()];
|
||||
poll_for_read(&mut poll_array);
|
||||
log::trace!(
|
||||
"out: {}, in: {}",
|
||||
poll_array[0].revents,
|
||||
poll_array[1].revents
|
||||
);
|
||||
|
||||
if poll_array[1].revents != 0 || stream.has_read_buffered() {
|
||||
// When TLS is enabled on a stream, it may require a mixture of
|
||||
@@ -132,7 +127,6 @@ fn client_thread(
|
||||
);
|
||||
}
|
||||
} else {
|
||||
log::trace!("spurious/incomplete read wakeup");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+35
-4
@@ -1,6 +1,6 @@
|
||||
use crate::config::Config;
|
||||
use crate::mux::tab::{Tab, TabId};
|
||||
use crate::mux::Mux;
|
||||
use crate::mux::{Mux, MuxNotification, MuxSubscriber};
|
||||
use crate::server::codec::*;
|
||||
use crate::server::pollable::*;
|
||||
use crate::server::UnixListener;
|
||||
@@ -40,8 +40,11 @@ impl LocalListener {
|
||||
match stream {
|
||||
Ok(stream) => {
|
||||
let executor = self.executor.clone_executor();
|
||||
let mut session = ClientSession::new(stream, executor);
|
||||
thread::spawn(move || session.run());
|
||||
Future::with_executor(executor.clone_executor(), move || {
|
||||
let mut session = ClientSession::new(stream, executor);
|
||||
thread::spawn(move || session.run());
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
Err(err) => {
|
||||
error!("accept failed: {}", err);
|
||||
@@ -195,6 +198,7 @@ pub struct ClientSession<S: ReadAndWrite> {
|
||||
surfaces_by_tab: Arc<Mutex<HashMap<TabId, ClientSurfaceState>>>,
|
||||
to_write_rx: PollableReceiver<DecodedPdu>,
|
||||
to_write_tx: PollableSender<DecodedPdu>,
|
||||
mux_rx: MuxSubscriber,
|
||||
}
|
||||
|
||||
fn maybe_push_tab_changes(
|
||||
@@ -325,12 +329,15 @@ impl<S: ReadAndWrite> ClientSession<S> {
|
||||
fn new(stream: S, executor: Box<dyn Executor>) -> Self {
|
||||
let (to_write_tx, to_write_rx) =
|
||||
pollable_channel().expect("failed to create pollable_channel");
|
||||
let mux = Mux::get().expect("to be running on gui thread");
|
||||
let mux_rx = mux.subscribe().expect("Mux::subscribe to succeed");
|
||||
Self {
|
||||
stream,
|
||||
executor,
|
||||
surfaces_by_tab: Arc::new(Mutex::new(HashMap::new())),
|
||||
to_write_rx,
|
||||
to_write_tx,
|
||||
mux_rx,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,8 +361,32 @@ impl<S: ReadAndWrite> ClientSession<S> {
|
||||
Err(TryRecvError::Disconnected) => bail!("ClientSession was destroyed"),
|
||||
};
|
||||
}
|
||||
loop {
|
||||
match self.mux_rx.try_recv() {
|
||||
Ok(notif) => match notif {
|
||||
MuxNotification::TabOutput(tab_id) => {
|
||||
let surfaces = Arc::clone(&self.surfaces_by_tab);
|
||||
let sender = self.to_write_tx.clone();
|
||||
Future::with_executor(self.executor.clone_executor(), move || {
|
||||
let mux = Mux::get().unwrap();
|
||||
let tab = mux
|
||||
.get_tab(tab_id)
|
||||
.ok_or_else(|| format_err!("no such tab {}", tab_id))?;
|
||||
maybe_push_tab_changes(&surfaces, &tab, sender)?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
},
|
||||
Err(TryRecvError::Empty) => break,
|
||||
Err(TryRecvError::Disconnected) => bail!("ClientSession was destroyed"),
|
||||
};
|
||||
}
|
||||
|
||||
let mut poll_array = [self.to_write_rx.as_poll_fd(), self.stream.as_poll_fd()];
|
||||
let mut poll_array = [
|
||||
self.to_write_rx.as_poll_fd(),
|
||||
self.stream.as_poll_fd(),
|
||||
self.mux_rx.as_poll_fd(),
|
||||
];
|
||||
poll_for_read(&mut poll_array);
|
||||
|
||||
if poll_array[1].revents != 0 || self.stream.has_read_buffered() {
|
||||
|
||||
@@ -83,7 +83,7 @@ impl<T> AsPollFd for PollableReceiver<T> {
|
||||
/// windows 10 w/unix domain socket support.
|
||||
pub fn pollable_channel<T>() -> Fallible<(PollableSender<T>, PollableReceiver<T>)> {
|
||||
let (sender, receiver) = channel();
|
||||
let (write, read) = UnixStream::pair()?;
|
||||
let (write, read) = socketpair()?;
|
||||
Ok((
|
||||
PollableSender {
|
||||
sender,
|
||||
|
||||
+10
-2
@@ -152,6 +152,7 @@ impl ClientTab {
|
||||
last_poll: RefCell::new(Instant::now()),
|
||||
dead: RefCell::new(false),
|
||||
poll_future: RefCell::new(None),
|
||||
poll_interval: RefCell::new(BASE_POLL_INTERVAL),
|
||||
surface: RefCell::new(Surface::new(80, 24)),
|
||||
remote_sequence: RefCell::new(0),
|
||||
local_sequence: RefCell::new(0),
|
||||
@@ -288,17 +289,20 @@ struct RenderableState {
|
||||
surface: RefCell<Surface>,
|
||||
remote_sequence: RefCell<SequenceNo>,
|
||||
local_sequence: RefCell<SequenceNo>,
|
||||
poll_interval: RefCell<Duration>,
|
||||
selection_range: Arc<Mutex<Option<SelectionRange>>>,
|
||||
something_changed: Arc<Mutex<bool>>,
|
||||
}
|
||||
|
||||
const POLL_INTERVAL: Duration = Duration::from_millis(50);
|
||||
const MAX_POLL_INTERVAL: Duration = Duration::from_secs(30);
|
||||
const BASE_POLL_INTERVAL: Duration = Duration::from_millis(20);
|
||||
|
||||
impl RenderableState {
|
||||
fn apply_changes_to_surface(&self, remote_seq: SequenceNo, changes: Vec<Change>) {
|
||||
if let Some(first) = changes.first().as_ref() {
|
||||
log::trace!("{:?}", first);
|
||||
}
|
||||
*self.poll_interval.borrow_mut() = BASE_POLL_INTERVAL;
|
||||
self.surface.borrow_mut().add_changes(changes);
|
||||
*self.remote_sequence.borrow_mut() = remote_seq;
|
||||
}
|
||||
@@ -312,6 +316,10 @@ impl RenderableState {
|
||||
.unwrap_or(false);
|
||||
if ready {
|
||||
self.poll_future.borrow_mut().take().unwrap().wait()?;
|
||||
let interval = *self.poll_interval.borrow();
|
||||
let interval = (interval + interval).min(MAX_POLL_INTERVAL);
|
||||
*self.poll_interval.borrow_mut() = interval;
|
||||
|
||||
*self.last_poll.borrow_mut() = Instant::now();
|
||||
} else if self.poll_future.borrow().is_some() {
|
||||
// We have a poll in progress
|
||||
@@ -319,7 +327,7 @@ impl RenderableState {
|
||||
}
|
||||
|
||||
let last = *self.last_poll.borrow();
|
||||
if last.elapsed() < POLL_INTERVAL {
|
||||
if last.elapsed() < *self.poll_interval.borrow() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user