Use an unbounded channel for peer's outgoing messages

Using a bounded channel may have blocked the collaboration server
from making progress handling RPC traffic.

There's no need to apply backpressure to calling code within the
same process - suspending a task that is attempting to call `send` has
an even greater memory cost than just buffering a protobuf message.

We do still want a bounded channel for incoming messages, so that
we provide backpressure to noisy peers - blocking their writes as opposed
to allowing them to buffer arbitrarily many messages in our server.

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-02-07 12:27:13 -08:00
parent 82afacd33d
commit d4fe1115e7
7 changed files with 341 additions and 472 deletions

View file

@ -460,7 +460,7 @@ impl Project {
}
})?;
rpc.send(proto::UnshareProject { project_id }).await?;
rpc.send(proto::UnshareProject { project_id })?;
this.update(&mut cx, |this, cx| {
this.collaborators.clear();
this.shared_buffers.clear();
@ -818,15 +818,13 @@ impl Project {
let this = cx.read(|cx| this.upgrade(cx))?;
match message {
LspEvent::DiagnosticsStart => {
let send = this.update(&mut cx, |this, cx| {
this.update(&mut cx, |this, cx| {
this.disk_based_diagnostics_started(cx);
this.remote_id().map(|project_id| {
if let Some(project_id) = this.remote_id() {
rpc.send(proto::DiskBasedDiagnosticsUpdating { project_id })
})
.log_err();
}
});
if let Some(send) = send {
send.await.log_err();
}
}
LspEvent::DiagnosticsUpdate(mut params) => {
language.process_diagnostics(&mut params);
@ -836,15 +834,13 @@ impl Project {
});
}
LspEvent::DiagnosticsFinish => {
let send = this.update(&mut cx, |this, cx| {
this.update(&mut cx, |this, cx| {
this.disk_based_diagnostics_finished(cx);
this.remote_id().map(|project_id| {
if let Some(project_id) = this.remote_id() {
rpc.send(proto::DiskBasedDiagnosticsUpdated { project_id })
})
.log_err();
}
});
if let Some(send) = send {
send.await.log_err();
}
}
}
}
@ -1311,15 +1307,13 @@ impl Project {
};
if let Some(project_id) = self.remote_id() {
let client = self.client.clone();
let message = proto::UpdateBufferFile {
project_id,
buffer_id: *buffer_id as u64,
file: Some(new_file.to_proto()),
};
cx.foreground()
.spawn(async move { client.send(message).await })
.detach_and_log_err(cx);
self.client
.send(proto::UpdateBufferFile {
project_id,
buffer_id: *buffer_id as u64,
file: Some(new_file.to_proto()),
})
.log_err();
}
buffer.file_updated(Box::new(new_file), cx).detach();
}
@ -1639,8 +1633,7 @@ impl Project {
version: (&version).into(),
mtime: Some(mtime.into()),
},
)
.await?;
)?;
Ok(())
}
@ -1669,16 +1662,13 @@ impl Project {
// associated with formatting.
cx.spawn(|_| async move {
match format {
Ok(()) => rpc.respond(receipt, proto::Ack {}).await?,
Err(error) => {
rpc.respond_with_error(
receipt,
proto::Error {
message: error.to_string(),
},
)
.await?
}
Ok(()) => rpc.respond(receipt, proto::Ack {})?,
Err(error) => rpc.respond_with_error(
receipt,
proto::Error {
message: error.to_string(),
},
)?,
}
Ok::<_, anyhow::Error>(())
})
@ -1712,27 +1702,21 @@ impl Project {
.update(&mut cx, |buffer, cx| buffer.completions(position, cx))
.await
{
Ok(completions) => {
rpc.respond(
receipt,
proto::GetCompletionsResponse {
completions: completions
.iter()
.map(language::proto::serialize_completion)
.collect(),
},
)
.await
}
Err(error) => {
rpc.respond_with_error(
receipt,
proto::Error {
message: error.to_string(),
},
)
.await
}
Ok(completions) => rpc.respond(
receipt,
proto::GetCompletionsResponse {
completions: completions
.iter()
.map(language::proto::serialize_completion)
.collect(),
},
),
Err(error) => rpc.respond_with_error(
receipt,
proto::Error {
message: error.to_string(),
},
),
}
})
.detach_and_log_err(cx);
@ -1767,30 +1751,24 @@ impl Project {
})
.await
{
Ok(edit_ids) => {
rpc.respond(
receipt,
proto::ApplyCompletionAdditionalEditsResponse {
additional_edits: edit_ids
.into_iter()
.map(|edit_id| proto::AdditionalEdit {
replica_id: edit_id.replica_id as u32,
local_timestamp: edit_id.value,
})
.collect(),
},
)
.await
}
Err(error) => {
rpc.respond_with_error(
receipt,
proto::Error {
message: error.to_string(),
},
)
.await
}
Ok(edit_ids) => rpc.respond(
receipt,
proto::ApplyCompletionAdditionalEditsResponse {
additional_edits: edit_ids
.into_iter()
.map(|edit_id| proto::AdditionalEdit {
replica_id: edit_id.replica_id as u32,
local_timestamp: edit_id.value,
})
.collect(),
},
),
Err(error) => rpc.respond_with_error(
receipt,
proto::Error {
message: error.to_string(),
},
),
}
})
.detach_and_log_err(cx);
@ -1836,7 +1814,7 @@ impl Project {
});
}
});
rpc.respond(receipt, response).await?;
rpc.respond(receipt, response)?;
Ok::<_, anyhow::Error>(())
})
.detach_and_log_err(cx);
@ -1872,7 +1850,6 @@ impl Project {
buffer: Some(buffer),
},
)
.await
}
.log_err()
})
@ -2106,28 +2083,21 @@ impl<'a> Iterator for CandidateSetIter<'a> {
impl Entity for Project {
type Event = Event;
fn release(&mut self, cx: &mut gpui::MutableAppContext) {
fn release(&mut self, _: &mut gpui::MutableAppContext) {
match &self.client_state {
ProjectClientState::Local { remote_id_rx, .. } => {
if let Some(project_id) = *remote_id_rx.borrow() {
let rpc = self.client.clone();
cx.spawn(|_| async move {
if let Err(err) = rpc.send(proto::UnregisterProject { project_id }).await {
log::error!("error unregistering project: {}", err);
}
})
.detach();
self.client
.send(proto::UnregisterProject { project_id })
.log_err();
}
}
ProjectClientState::Remote { remote_id, .. } => {
let rpc = self.client.clone();
let project_id = *remote_id;
cx.spawn(|_| async move {
if let Err(err) = rpc.send(proto::LeaveProject { project_id }).await {
log::error!("error leaving project: {}", err);
}
})
.detach();
self.client
.send(proto::LeaveProject {
project_id: *remote_id,
})
.log_err();
}
}
}