diff --git a/promise/src/lib.rs b/promise/src/lib.rs index 6b8574d82..20a783593 100644 --- a/promise/src/lib.rs +++ b/promise/src/lib.rs @@ -199,6 +199,16 @@ impl Future { } } + pub fn is_ready(&self) -> bool { + match &self.state { + FutureState::Waiting(core) => { + let locked = core.data.lock().unwrap(); + locked.result.is_some() + } + FutureState::Ready(_) => true, + } + } + /// When this future resolves, then map the result via the /// supplied lambda, which returns something that is convertible /// to a Future. diff --git a/src/server/client.rs b/src/server/client.rs index ae92adb48..e85446612 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -63,8 +63,23 @@ fn client_thread_inner( ) -> Fallible<()> { let mut next_serial = 0u64; loop { - match rx.try_recv() { - Ok(msg) => match msg { + let msg = if promises.is_empty() { + // If we don't have any results to read back, then we can and + // should block on an incoming request, otherwise we'll busy + // wait in this loop + match rx.recv() { + Ok(msg) => Some(msg), + Err(err) => bail!("Client was destroyed: {}", err), + } + } else { + match rx.try_recv() { + Ok(msg) => Some(msg), + Err(TryRecvError::Empty) => None, + Err(TryRecvError::Disconnected) => bail!("Client was destroyed"), + } + }; + if let Some(msg) = msg { + match msg { ReaderMessage::SendPdu { pdu, promise } => { let serial = next_serial; next_serial += 1; @@ -73,9 +88,7 @@ fn client_thread_inner( pdu.encode(&mut stream, serial)?; stream.flush()?; } - }, - Err(TryRecvError::Empty) => {} - Err(TryRecvError::Disconnected) => bail!("Client was destroyed"), + } } if !promises.is_empty() { diff --git a/src/server/tab.rs b/src/server/tab.rs index b55855cbb..7bfed6bc4 100644 --- a/src/server/tab.rs +++ b/src/server/tab.rs @@ -7,6 +7,7 @@ use failure::Fallible; use filedescriptor::Pipe; use log::error; use portable_pty::PtySize; +use promise::Future; use std::cell::RefCell; use std::cell::RefMut; use std::ops::Range; @@ -41,6 +42,7 @@ impl ClientTab { last_poll: RefCell::new(Instant::now()), dirty_all: RefCell::new(true), dead: RefCell::new(false), + poll_future: RefCell::new(None), }; let reader = Pipe::new().expect("Pipe::new failed"); @@ -157,12 +159,32 @@ struct RenderableState { last_poll: RefCell, dirty_all: RefCell, dead: RefCell, + poll_future: RefCell>>, } const POLL_INTERVAL: Duration = Duration::from_millis(50); impl RenderableState { fn poll(&self) -> Fallible<()> { + let ready = self + .poll_future + .borrow() + .as_ref() + .map(Future::is_ready) + .unwrap_or(false); + if ready { + let coarse = self.poll_future.borrow_mut().take().unwrap().wait()?; + self.coarse.borrow_mut().replace(coarse); + log::trace!( + "poll: got coarse data in {:?}", + self.last_poll.borrow().elapsed() + ); + *self.last_poll.borrow_mut() = Instant::now(); + } else if self.poll_future.borrow().is_some() { + // We have a poll in progress + return Ok(()); + } + let dirty_all = *self.dirty_all.borrow(); if !dirty_all { @@ -174,15 +196,13 @@ impl RenderableState { { let mut client = self.client.client.lock().unwrap(); - let coarse = client - .get_coarse_tab_renderable_data(GetCoarseTabRenderableData { + *self.poll_future.borrow_mut() = Some(client.get_coarse_tab_renderable_data( + GetCoarseTabRenderableData { tab_id: self.remote_tab_id, dirty_all, - }) - .wait()?; - self.coarse.borrow_mut().replace(coarse); + }, + )); } - *self.last_poll.borrow_mut() = Instant::now(); *self.dirty_all.borrow_mut() = false; Ok(()) }