mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-14 14:11:34 +00:00
Handle Peer responses using a futures::oneshot instead of postage::mpsc
This commit is contained in:
parent
f2f1a52c7e
commit
917543cc32
1 changed files with 7 additions and 15 deletions
|
@ -1,8 +1,7 @@
|
||||||
use super::proto::{self, AnyTypedEnvelope, EnvelopedMessage, MessageStream, RequestMessage};
|
use super::proto::{self, AnyTypedEnvelope, EnvelopedMessage, MessageStream, RequestMessage};
|
||||||
use super::Connection;
|
use super::Connection;
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use futures::stream::BoxStream;
|
use futures::{channel::oneshot, stream::BoxStream, FutureExt as _, StreamExt};
|
||||||
use futures::{FutureExt as _, StreamExt};
|
|
||||||
use parking_lot::{Mutex, RwLock};
|
use parking_lot::{Mutex, RwLock};
|
||||||
use postage::{
|
use postage::{
|
||||||
barrier, mpsc,
|
barrier, mpsc,
|
||||||
|
@ -92,7 +91,7 @@ pub struct ConnectionState {
|
||||||
outgoing_tx: futures::channel::mpsc::UnboundedSender<proto::Envelope>,
|
outgoing_tx: futures::channel::mpsc::UnboundedSender<proto::Envelope>,
|
||||||
next_message_id: Arc<AtomicU32>,
|
next_message_id: Arc<AtomicU32>,
|
||||||
response_channels:
|
response_channels:
|
||||||
Arc<Mutex<Option<HashMap<u32, mpsc::Sender<(proto::Envelope, barrier::Sender)>>>>>,
|
Arc<Mutex<Option<HashMap<u32, oneshot::Sender<(proto::Envelope, barrier::Sender)>>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const WRITE_TIMEOUT: Duration = Duration::from_secs(10);
|
const WRITE_TIMEOUT: Duration = Duration::from_secs(10);
|
||||||
|
@ -177,18 +176,14 @@ impl Peer {
|
||||||
async move {
|
async move {
|
||||||
if let Some(responding_to) = incoming.responding_to {
|
if let Some(responding_to) = incoming.responding_to {
|
||||||
let channel = response_channels.lock().as_mut()?.remove(&responding_to);
|
let channel = response_channels.lock().as_mut()?.remove(&responding_to);
|
||||||
if let Some(mut tx) = channel {
|
if let Some(tx) = channel {
|
||||||
let mut requester_resumed = barrier::channel();
|
let mut requester_resumed = barrier::channel();
|
||||||
if let Err(error) = tx.send((incoming, requester_resumed.0)).await {
|
if let Err(error) = tx.send((incoming, requester_resumed.0)) {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"received RPC but request future was dropped {:?}",
|
"received RPC but request future was dropped {:?}",
|
||||||
error.0 .0
|
error.0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Drop response channel before awaiting on the barrier. This allows the
|
|
||||||
// barrier to get dropped even if the request's future is dropped before it
|
|
||||||
// has a chance to observe the response.
|
|
||||||
drop(tx);
|
|
||||||
requester_resumed.1.recv().await;
|
requester_resumed.1.recv().await;
|
||||||
} else {
|
} else {
|
||||||
log::warn!("received RPC response to unknown request {}", responding_to);
|
log::warn!("received RPC response to unknown request {}", responding_to);
|
||||||
|
@ -239,7 +234,7 @@ impl Peer {
|
||||||
receiver_id: ConnectionId,
|
receiver_id: ConnectionId,
|
||||||
request: T,
|
request: T,
|
||||||
) -> impl Future<Output = Result<T::Response>> {
|
) -> impl Future<Output = Result<T::Response>> {
|
||||||
let (tx, mut rx) = mpsc::channel(1);
|
let (tx, rx) = oneshot::channel();
|
||||||
let send = self.connection_state(receiver_id).and_then(|connection| {
|
let send = self.connection_state(receiver_id).and_then(|connection| {
|
||||||
let message_id = connection.next_message_id.fetch_add(1, SeqCst);
|
let message_id = connection.next_message_id.fetch_add(1, SeqCst);
|
||||||
connection
|
connection
|
||||||
|
@ -260,10 +255,7 @@ impl Peer {
|
||||||
});
|
});
|
||||||
async move {
|
async move {
|
||||||
send?;
|
send?;
|
||||||
let (response, _barrier) = rx
|
let (response, _barrier) = rx.await.map_err(|_| anyhow!("connection was closed"))?;
|
||||||
.recv()
|
|
||||||
.await
|
|
||||||
.ok_or_else(|| anyhow!("connection was closed"))?;
|
|
||||||
if let Some(proto::envelope::Payload::Error(error)) = &response.payload {
|
if let Some(proto::envelope::Payload::Error(error)) = &response.payload {
|
||||||
Err(anyhow!("RPC request failed - {}", error.message))
|
Err(anyhow!("RPC request failed - {}", error.message))
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue