cache: add grpc read-only response caching

This commit is contained in:
a.tolmachev
2026-05-04 09:34:34 +00:00
parent b2c5e28cba
commit a61064ba3a
13 changed files with 306 additions and 91 deletions
@@ -1,5 +1,9 @@
use base64::{Engine as _, engine::general_purpose::STANDARD};
use futures_util::stream;
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use tokio::net::TcpListener;
use tonic::{Request, Response, Status, transport::Server};
@@ -121,6 +125,54 @@ pub async fn spawn_metadata_echo_server() -> String {
format!("http://{}", address)
}
pub async fn spawn_counting_unary_echo_server(request_count: Arc<AtomicUsize>) -> String {
struct CountingEchoServiceImpl {
request_count: Arc<AtomicUsize>,
}
#[tonic::async_trait]
impl echo::echo_service_server::EchoService for CountingEchoServiceImpl {
async fn unary_echo(
&self,
request: Request<echo::EchoRequest>,
) -> Result<Response<echo::EchoResponse>, Status> {
let message = request.into_inner().message;
let count = self.request_count.fetch_add(1, Ordering::SeqCst) + 1;
Ok(Response::new(echo::EchoResponse {
message: format!("{message}-{count}"),
}))
}
type ServerEchoStream = std::pin::Pin<
Box<dyn futures_util::Stream<Item = Result<echo::EchoResponse, Status>> + Send>,
>;
async fn server_echo(
&self,
_request: Request<echo::EchoRequest>,
) -> Result<Response<Self::ServerEchoStream>, Status> {
Ok(Response::new(Box::pin(stream::empty())))
}
}
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let incoming = tonic::transport::server::TcpIncoming::from(listener);
tokio::spawn(async move {
Server::builder()
.add_service(echo::echo_service_server::EchoServiceServer::new(
CountingEchoServiceImpl { request_count },
))
.serve_with_incoming(incoming)
.await
.unwrap();
});
format!("http://{}", address)
}
pub fn descriptor_set_b64() -> String {
STANDARD.encode(echo::FILE_DESCRIPTOR_SET)
}