From 56fc787f7a8a5da2aa7d18c2867ac5fdf8a46a89 Mon Sep 17 00:00:00 2001 From: charles Date: Sat, 16 May 2026 17:24:46 -0700 Subject: [PATCH] Use roto_tonic types in hello_world example Fix the client's poll_ready implementation to properly propagate readiness. --- examples/hello_world/src/bin/client.rs | 9 ++++- examples/hello_world/src/bin/server.rs | 53 +++----------------------- 2 files changed, 13 insertions(+), 49 deletions(-) diff --git a/examples/hello_world/src/bin/client.rs b/examples/hello_world/src/bin/client.rs index fe3e1b0..49103b8 100644 --- a/examples/hello_world/src/bin/client.rs +++ b/examples/hello_world/src/bin/client.rs @@ -5,6 +5,8 @@ use roto_runtime::RotoOwned; use std::task::{Context, Poll}; use tower::Service; +pub use roto_tonic::{BufferPool, StatusBody}; + pub mod hello { include!(concat!(env!("OUT_DIR"), "/hello.rs")); } @@ -19,11 +21,14 @@ where type Error = S::Error; type Future = S::Future; - fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(())) + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + self.0.poll_ready(cx) } fn call(&mut self, req: Req) -> S::Future { + let waker = futures_util::task::noop_waker(); + let mut cx = std::task::Context::from_waker(&waker); + let _ = self.poll_ready(&mut cx); self.0.call(req) } } diff --git a/examples/hello_world/src/bin/server.rs b/examples/hello_world/src/bin/server.rs index 9aa1486..77c1396 100644 --- a/examples/hello_world/src/bin/server.rs +++ b/examples/hello_world/src/bin/server.rs @@ -13,35 +13,12 @@ use roto_runtime::{RotoOwned, RotoMessage}; use http_body_util::BodyExt; use http_body::Body; +pub use roto_tonic::{BufferPool, StatusBody}; + pub mod hello { include!(concat!(env!("OUT_DIR"), "/hello.rs")); } -struct BufferPool { - pool: Mutex>, - default_capacity: usize, -} - -impl BufferPool { - fn new(default_capacity: usize) -> Self { - Self { - pool: Mutex::new(Vec::new()), - default_capacity, - } - } - - fn get(&self) -> BytesMut { - self.pool.lock().unwrap().pop().unwrap_or_else(|| BytesMut::with_capacity(self.default_capacity)) - } - - fn put(&self, mut buf: BytesMut) { - buf.clear(); - if buf.capacity() >= self.default_capacity { - self.pool.lock().unwrap().push(buf); - } - } -} - #[derive(Clone)] pub struct MyHelloWorld { pool: Arc, @@ -99,24 +76,6 @@ impl tonic::server::NamedService for HelloWorldServer { const NAME: &'static str = "hello.HelloWorldService"; } -struct StatusBody(Option); - -impl Body for StatusBody { - type Data = Bytes; - type Error = Status; - - fn poll_frame( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll, Self::Error>>> { - if let Some(data) = self.0.take() { - Poll::Ready(Some(Ok(http_body::Frame::data(data)))) - } else { - Poll::Ready(None) - } - } -} - impl Service> for HelloWorldServer { type Response = http::Response; type Error = std::convert::Infallible; @@ -152,7 +111,7 @@ impl Service> for HelloWorldServer { if bytes_vec.len() < 5 { println!("Body too short: {} bytes", bytes_vec.len()); - let res_body = BoxBody::new(StatusBody(Some(Bytes::from_static(&[0, 0, 0, 0, 0])))); + let res_body = BoxBody::new(StatusBody::new(Some(Bytes::from_static(&[0, 0, 0, 0, 0])), 0)); return Ok(http::Response::builder() .status(200) .body(res_body) @@ -164,7 +123,7 @@ impl Service> for HelloWorldServer { Ok(msg) => msg, Err(e) => { println!("Decode error: {}", e); - let res_body = BoxBody::new(StatusBody(Some(Bytes::from_static(&[0, 0, 0, 0, 0])))); + let res_body = BoxBody::new(StatusBody::new(Some(Bytes::from_static(&[0, 0, 0, 0, 0])), 0)); return Ok(http::Response::builder().status(200).body(res_body).unwrap()); } }; @@ -174,7 +133,7 @@ impl Service> for HelloWorldServer { Ok(res) => res, Err(e) => { println!("Service error: {}", e); - let res_body = BoxBody::new(StatusBody(Some(Bytes::from_static(&[0, 0, 0, 0, 0])))); + let res_body = BoxBody::new(StatusBody::new(Some(Bytes::from_static(&[0, 0, 0, 0, 0])), 0)); return Ok(http::Response::builder().status(200).body(res_body).unwrap()); } }; @@ -193,7 +152,7 @@ impl Service> for HelloWorldServer { let frame = res_buf.split_to(frame_len).freeze(); pool.put(res_buf); - let res_body = BoxBody::new(StatusBody(Some(frame))); + let res_body = BoxBody::new(StatusBody::new(Some(frame), 0)); Ok(http::Response::builder() .status(200) .header("content-type", "application/grpc")