ratelimit unilateral pushes

This commit is contained in:
Wez Furlong committed 2019-06-21 07:56:09 -07:00
1 parent 9a4ae0a06b
commit 4ddef39b52
3 files changed
+46 -13

No files matched your search

+13
View File
@@ -139,6 +139,18 @@ pub struct Config {
/// The default value is 2MB/s.
pub ratelimit_output_bytes_per_second: Option<u32>,
/// Constrains the rate at which the multiplexer server will
/// unilaterally push data to the client.
/// This helps to avoid saturating the link between the client
/// and server.
/// Each time the screen is updated as a result of the child
/// command outputting data (rather than in response to input
/// from the client), the server considers whether to push
/// the result to the client.
/// That decision is throttled by this configuration value
/// which has a default value of 10/s
pub ratelimit_mux_output_pushes_per_second: Option<u32>,
#[serde(default)]
pub keys: Vec<Key>,
}
@@ -403,6 +415,7 @@ impl Default for Config {
mux_client_accept_invalid_hostnames: None,
mux_client_expected_cn: None,
ratelimit_output_bytes_per_second: None,
ratelimit_mux_output_pushes_per_second: None,
mux_pem_root_certs: None,
keys: vec![],
}
+4
View File
@@ -15,6 +15,10 @@ impl RateLimiter {
}
}
pub fn non_blocking_admittance_check(&mut self, amount: u32) -> bool {
self.lim.check_n(amount).is_ok()
}
pub fn blocking_admittance_check(&mut self, amount: u32) {
loop {
match self.lim.check_n(amount) {
+29 -13
View File
@@ -1,6 +1,7 @@
use crate::config::Config;
use crate::mux::tab::{Tab, TabId};
use crate::mux::{Mux, MuxNotification, MuxSubscriber};
use crate::ratelim::RateLimiter;
use crate::server::codec::*;
use crate::server::pollable::*;
use crate::server::UnixListener;
@@ -12,7 +13,7 @@ use log::{debug, error};
use native_tls::Identity;
use portable_pty::PtySize;
use promise::{Executor, Future};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::fs::{remove_file, DirBuilder};
use std::io::Read;
@@ -381,6 +382,7 @@ pub struct ClientSession<S: ReadAndWrite> {
to_write_rx: PollableReceiver<DecodedPdu>,
to_write_tx: PollableSender<DecodedPdu>,
mux_rx: MuxSubscriber,
push_limiter: RateLimiter,
}
fn maybe_push_tab_changes(
@@ -535,6 +537,11 @@ impl<S: ReadAndWrite> ClientSession<S> {
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");
let push_limiter = RateLimiter::new(
mux.config()
.ratelimit_mux_output_pushes_per_second
.unwrap_or(10),
);
Self {
stream,
executor,
@@ -542,6 +549,7 @@ impl<S: ReadAndWrite> ClientSession<S> {
to_write_rx,
to_write_tx,
mux_rx,
push_limiter,
}
}
@@ -553,6 +561,8 @@ impl<S: ReadAndWrite> ClientSession<S> {
fn process(&mut self) -> Result<(), Error> {
let mut read_buffer = Vec::with_capacity(1024);
let mut tabs_to_output = HashSet::new();
loop {
loop {
match self.to_write_rx.try_recv() {
@@ -568,22 +578,28 @@ impl<S: ReadAndWrite> ClientSession<S> {
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(())
});
}
// Coalesce multiple TabOutputs for the same tab
MuxNotification::TabOutput(tab_id) => tabs_to_output.insert(tab_id),
},
Err(TryRecvError::Empty) => break,
Err(TryRecvError::Disconnected) => bail!("ClientSession was destroyed"),
};
// Process any TabOutput notifications, subject to throttling
for tab_id in tabs_to_output.drain() {
if self.push_limiter.non_blocking_admittance_check(1) {
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(())
});
}
}
}
let mut poll_array = [