|
| 1 | +pub mod grpc_bindings; |
| 2 | + |
| 3 | +use wtx::{ |
| 4 | + codec::format::QuickProtobuf, |
| 5 | + collection::Vector, |
| 6 | + grpc::{GrpcManager, GrpcMiddleware}, |
| 7 | + http::{ |
| 8 | + HttpRecvParams, MsgBufferString, |
| 9 | + server_framework::{Router, ServerFrameworkBuilder, State, post}, |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +fn main() { |
| 14 | + let threads = std::thread::available_parallelism().map(|el| el.get()).unwrap_or(1); |
| 15 | + let mut handlers = Vector::new(); |
| 16 | + for _ in 0..threads { |
| 17 | + let handle = std::thread::spawn(|| { |
| 18 | + tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(serve()) |
| 19 | + }); |
| 20 | + handlers.push(handle).unwrap(); |
| 21 | + } |
| 22 | + for handle in handlers { |
| 23 | + handle.join().unwrap(); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +async fn endpoint_grpc_unary( |
| 28 | + state: State<'_, (), GrpcManager<QuickProtobuf>, MsgBufferString>, |
| 29 | +) -> wtx::Result<()> { |
| 30 | + let sr: grpc_bindings::benchmark::SumRequest = |
| 31 | + state.stream_aux.des_from_req_bytes(&mut state.req.msg_data.body.as_slice())?; |
| 32 | + state.req.clear(); |
| 33 | + let result = sr.a.wrapping_add(sr.b); |
| 34 | + state.stream_aux.ser_to_res_bytes( |
| 35 | + &mut state.req.msg_data.body, |
| 36 | + grpc_bindings::benchmark::SumReply { result }, |
| 37 | + )?; |
| 38 | + Ok(()) |
| 39 | +} |
| 40 | + |
| 41 | +async fn serve() { |
| 42 | + let router = Router::new( |
| 43 | + wtx::paths!(("/benchmark.BenchmarkService/GetSum", post(endpoint_grpc_unary))), |
| 44 | + GrpcMiddleware, |
| 45 | + ) |
| 46 | + .unwrap(); |
| 47 | + let _rslt = ServerFrameworkBuilder::new(HttpRecvParams::with_permissive_params(), router) |
| 48 | + .with_stream_aux(|_| Ok(QuickProtobuf)) |
| 49 | + .tokio( |
| 50 | + "0.0.0.0:8080", |
| 51 | + |_error| println!("{_error}"), |
| 52 | + |_| Ok(()), |
| 53 | + |_stream| Ok(()), |
| 54 | + |_error| println!("{_error}"), |
| 55 | + ) |
| 56 | + .await; |
| 57 | +} |
0 commit comments