Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 26 additions & 22 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ use tokio::net::{TcpStream, UnixListener};
use tokio::runtime::{Handle, Runtime};
use tokio::task::JoinHandle;
use tokio::try_join;
use tokio_util::codec::{FramedRead, FramedWrite};
use wasmtime::component::{Component, Linker, ResourceTable};
use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView};
use wrpc_transport::{FrameDecoder, FrameEncoder, Header, Listener};
use wrpc_transport::{Framed, Header, Listener};

mod ping_bindings_wrpc {
wit_bindgen_wrpc::generate!({
Expand Down Expand Up @@ -84,19 +83,17 @@ enum Func {
impl Func {
async fn call(
self,
tx: &mut FramedWrite<impl AsyncWrite + Send + Unpin, FrameEncoder>,
rx: &mut FramedRead<impl AsyncRead + Send + Unpin, FrameDecoder>,
clt: &mut Framed<impl AsyncRead + Send + Unpin, impl AsyncWrite + Send + Unpin>,
) -> anyhow::Result<()> {
match self {
Self::Ping => ping_bindings_wrpc::wrpc_bench::bench::ping::ping(&mut *tx, &mut *rx)
Self::Ping => ping_bindings_wrpc::wrpc_bench::bench::ping::ping(&mut *clt)
.await
.context("failed to call `ping`"),
Self::Greet => {
let greeting = greet_bindings_wrpc::wrpc_bench::bench::greet::greet(
&mut *tx, &mut *rx, "test",
)
.await
.context("failed to call `greet`")?;
let greeting =
greet_bindings_wrpc::wrpc_bench::bench::greet::greet(&mut *clt, "test")
.await
.context("failed to call `greet`")?;
ensure!(
greeting == "Hello, test",
"unexpected greeting `{greeting}`"
Expand Down Expand Up @@ -299,15 +296,23 @@ impl Tasks {
) {
self.spawn(async move {
let mut lis = Listener::new(tx, rx);
while let Some((Header { instance, name }, tx, rx)) = lis.accept().await? {
let res = match func {
Func::Ping => ping_bindings_wrpc::handle(&handler, &instance, &name, tx, rx),
Func::Greet => greet_bindings_wrpc::handle(&handler, &instance, &name, tx, rx),
};
let Ok(fut) = res else {
bail!("`{instance}#{name}` is not exported");
};
fut.await?;
while let Some((Header { instance, name }, srv)) = lis.accept().await? {
match func {
Func::Ping => {
let Ok(fut) = ping_bindings_wrpc::handle(&handler, &instance, &name, srv)
else {
bail!("`{instance}#{name}` is not exported");
};
fut.await?;
}
Func::Greet => {
let Ok(fut) = greet_bindings_wrpc::handle(&handler, &instance, &name, srv)
else {
bail!("`{instance}#{name}` is not exported");
};
fut.await?;
}
}
}
Ok(())
});
Expand Down Expand Up @@ -336,14 +341,13 @@ fn bench_invoke(
>,
) -> anyhow::Result<()> {
let (tx, rx) = rt.block_on(connect)?;
let mut tx = FramedWrite::new(tx, FrameEncoder);
let mut rx = FramedRead::new(rx, FrameDecoder::default());
let mut clt = Framed::new(tx, rx);
g.bench_function(transport, |b| {
b.iter_custom(|n| {
rt.block_on(async {
let start = Instant::now();
for _ in 0..n {
if let Err(err) = func.call(&mut tx, &mut rx).await {
if let Err(err) = func.call(&mut clt).await {
panic!("{:?}", tasks.check().err().unwrap_or(err));
}
}
Expand Down
19 changes: 10 additions & 9 deletions crates/http/tests/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use hyper_util::rt::TokioIo;
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
use tokio::sync::mpsc;
use tokio::{spawn, try_join};
use tokio_util::codec::{FramedRead, FramedWrite};
use wrpc_http::OutgoingBody;
use wrpc_transport::{FrameDecoder, FrameEncoder, Listener};
use wrpc_transport::{Framed, Listener};

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn http2() -> anyhow::Result<()> {
Expand Down Expand Up @@ -113,8 +112,8 @@ async fn buffered() -> anyhow::Result<()> {
let ((), body) = try_join!(
async {
let mut lis = Listener::new(tx, rx);
let (tx, rx) = wrpc_test::next_invocation(&mut lis, "foo", "bar").await?;
wrpc_transport::serve::<Bytes, Bytes, _, _, _, _>(tx, rx, async |params| {
let srv = wrpc_test::next_invocation(&mut lis, "foo", "bar").await?;
wrpc_transport::serve::<Bytes, Bytes, _, _, _>(srv, async |params| {
anyhow::Ok(params)
})
.await?;
Expand Down Expand Up @@ -142,11 +141,13 @@ async fn buffered() -> anyhow::Result<()> {
let (parts, body) = transport.request(req).await?;
Ok::<_, anyhow::Error>(http::Response::from_parts(parts, Full::new(body)))
});
let tx = FramedWrite::new(tx, FrameEncoder);
let rx = FramedRead::new(rx, FrameDecoder::default());
let results =
wrpc_transport::invoke_blocking::<_, Bytes, _, _>(tx, rx, "foo", "bar", params.clone())
.await?;
let results = wrpc_transport::invoke_blocking::<_, Bytes, _>(
Framed::new(tx, rx),
"foo",
"bar",
params.clone(),
)
.await?;
assert_eq!(results, params);
Ok(())
}
36 changes: 18 additions & 18 deletions crates/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use tokio::time::timeout;
use tokio::try_join;
use tokio_util::codec::{Encoder as _, FramedRead, FramedWrite};
use wrpc_transport::{
Decode, FrameDecoder, FrameEncoder, FrameRef, Header, HeaderEncoder, HeaderRef, Listener,
invoke, receive, serve,
Codec, Decode, Encode, FrameDecoder, FrameEncoder, FrameRef, Framed, Header, HeaderEncoder,
HeaderRef, Listener, invoke, receive, serve,
};

/// Maximum number of connections [`serve_until`] serves concurrently
Expand Down Expand Up @@ -67,10 +67,7 @@ pub async fn next_invocation<'a, I, O>(
lis: &'a mut Listener<I, O>,
instance: &str,
func: &str,
) -> anyhow::Result<(
&'a mut FramedWrite<O, FrameEncoder>,
&'a mut FramedRead<I, FrameDecoder>,
)>
) -> anyhow::Result<&'a mut Framed<I, O>>
where
I: AsyncRead + Unpin,
{
Expand All @@ -83,14 +80,13 @@ where
instance: inst,
name,
},
tx,
rx,
srv,
) = next.context("byte stream ended")?;
ensure!(
inst == instance && name == func,
"unexpected invocation of `{inst}.{name}`",
);
Ok((tx, rx))
Ok(srv)
}

/// Reads `rx` to EOF, ensuring that no frames are left
Expand Down Expand Up @@ -207,7 +203,11 @@ pub async fn assert_single_invocation(
Box::pin(stream::iter([Bytes::from_static(b"client->server")]));
let tx = FramedWrite::new(tx, FrameEncoder);
let mut rx = FramedRead::new(rx, FrameDecoder::default());
let ((results, s), io) = invoke::<_, (Bytes, BoxStream<'static, Bytes>), _, _, _, _>(
let ((results, s), io) = Codec::<
<(Bytes, BoxStream<'static, Bytes>) as Encode>::Encoder,
<(Bytes, BoxStream<'static, Bytes>) as Decode>::Decoder,
>::default()
.invoke(
tx,
&mut rx,
"foo",
Expand All @@ -228,15 +228,14 @@ pub async fn assert_single_invocation(
async {
let (tx, rx) = accept.await.context("failed to accept")?;
let mut lis = Listener::new(tx, rx);
let (tx, rx) = next_invocation(&mut lis, "foo", "bar").await?;
let srv = next_invocation(&mut lis, "foo", "bar").await?;
serve::<
(Bytes, BoxStream<'static, Bytes>),
(Bytes, BoxStream<'static, Bytes>),
_,
_,
_,
_,
>(tx, rx, async |(testbar, s)| {
>(srv, async |(testbar, s)| {
assert_eq!(testbar, b"testbar".as_slice());
let buf = s.collect::<Vec<_>>().await.concat();
assert_eq!(buf, b"client->server");
Expand Down Expand Up @@ -307,7 +306,7 @@ where
anyhow::Ok(Listener::new(tx, rx))
},
)?;
let (_, srv_rx) = next_invocation(&mut lis, "foo", "baz").await?;
let Framed { rx: srv_rx, .. } = next_invocation(&mut lis, "foo", "baz").await?;
let res = receive(<() as Decode>::Decoder::default(), srv_rx).await;
ensure!(res.is_err(), "invalid data must fail server ingress");
drop(lis);
Expand All @@ -323,16 +322,17 @@ where
try_join!(
async {
let (tx, rx) = connect().await.context("failed to connect")?;
let tx = FramedWrite::new(tx, FrameEncoder);
let rx = FramedRead::new(rx, FrameDecoder::default());
let res = invoke::<(), (), _, _, _, _>(tx, rx, "foo", "baz", ()).await;
let res = invoke::<(), (), _>(Framed::new(tx, rx), "foo", "baz", ()).await;
ensure!(res.is_err(), "invalid data must fail client ingress");
anyhow::Ok(())
},
async {
let (tx, rx) = accept().await.context("failed to accept")?;
let mut lis = Listener::new(tx, rx);
let (srv_tx, srv_rx) = next_invocation(&mut lis, "foo", "baz").await?;
let Framed {
tx: srv_tx,
rx: srv_rx,
} = next_invocation(&mut lis, "foo", "baz").await?;
let srv_tx = srv_tx.get_mut();
srv_tx
.write_all(&invalid)
Expand Down
Loading
Loading