Reset receive timeout only on reads from websocket connection, not writes

Also, increase the receive timeout to 30 seconds. We'll still respond immediately
to explicit disconnection, but when there are temporary network blips that
delay pings, we think we should err on the side of keeping the connection
alive. This is in response to a false positive 'host disconnected' state
that we observed when pairing today, while the host (Keith) still clearly
had a working internet connection, because we were screen sharing.

Co-Authored-By: Keith Simmons <keith@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-03-09 11:27:44 -08:00
parent 508c4df79b
commit 3dc100adfb

View File

@ -96,6 +96,7 @@ pub struct ConnectionState {
const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(1);
const WRITE_TIMEOUT: Duration = Duration::from_secs(2);
const RECEIVE_TIMEOUT: Duration = Duration::from_secs(30);
impl Peer {
pub fn new() -> Arc<Self> {
@ -147,14 +148,14 @@ impl Peer {
let keepalive_timer = create_timer(KEEPALIVE_INTERVAL).fuse();
futures::pin_mut!(keepalive_timer);
// Disconnect if we don't receive messages at least this frequently.
let receive_timeout = create_timer(RECEIVE_TIMEOUT).fuse();
futures::pin_mut!(receive_timeout);
loop {
let read_message = reader.read().fuse();
futures::pin_mut!(read_message);
// Disconnect if we don't receive messages at least this frequently.
let receive_timeout = create_timer(3 * KEEPALIVE_INTERVAL).fuse();
futures::pin_mut!(receive_timeout);
loop {
futures::select_biased! {
outgoing = outgoing_rx.next().fuse() => match outgoing {
@ -170,6 +171,7 @@ impl Peer {
},
incoming = read_message => {
let incoming = incoming.context("received invalid RPC message")?;
receive_timeout.set(create_timer(RECEIVE_TIMEOUT).fuse());
if let proto::Message::Envelope(incoming) = incoming {
if incoming_tx.send(incoming).await.is_err() {
return Ok(());