Skip to content

Commit 9e57c15

Browse files
committed
[Rust] Add WTX - gRPC
1 parent e2f6beb commit 9e57c15

8 files changed

Lines changed: 377 additions & 0 deletions

File tree

frameworks/wtx-grpc/Cargo.lock

Lines changed: 235 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/wtx-grpc/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "httparena-wtx-grpc"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[build-dependencies]
7+
pb-rs = { default-features = false, version = "0.10" }
8+
9+
[dependencies]
10+
quick-protobuf = { default-features = false, version = "0.8" }
11+
tokio = { default-features = false, features = ["macros", "rt-multi-thread"], version = "1.0" }
12+
wtx = { default-features = false, features = ["grpc-server", "http-server-framework", "optimizations", "optioned-server", "quick-protobuf", "tokio"], version = "0.47" }
13+
14+
[profile.release]
15+
codegen-units = 1
16+
lto = "thin"
17+
opt-level = 3
18+
panic = "abort"

frameworks/wtx-grpc/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM rust:1.95 AS build
2+
RUN rustup default nightly-2026-05-07
3+
WORKDIR /app
4+
COPY Cargo.toml build.rs ./
5+
COPY proto ./proto
6+
RUN mkdir src && echo "fn main() {}" > src/main.rs && cargo build --release && rm -rf src/ target/release/httparena-wtx-grpc* target/release/deps/httparena_wtx-grpc*
7+
COPY src ./src
8+
RUN RUSTFLAGS="-C target-cpu=native" cargo build --release
9+
10+
FROM debian:bookworm-slim
11+
COPY --from=build /app/target/release/httparena-wtx-grpc /server
12+
EXPOSE 8080
13+
CMD ["/server"]

frameworks/wtx-grpc/build.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use pb_rs::{ConfigBuilder, types::FileDescriptor};
2+
use std::{
3+
fs::{DirBuilder, remove_dir_all},
4+
path::Path,
5+
};
6+
7+
fn main() {
8+
let cmd = std::env::var("CARGO_MANIFEST_DIR").unwrap();
9+
let in_dir = Path::new(&cmd).join("proto");
10+
let out_dir = Path::new(&std::env::var("OUT_DIR").unwrap()).join("proto");
11+
if out_dir.exists() {
12+
remove_dir_all(&out_dir).unwrap();
13+
}
14+
DirBuilder::new().create(&out_dir).unwrap();
15+
FileDescriptor::run(
16+
&ConfigBuilder::new(
17+
&[Path::new(&cmd).join("proto/benchmark.proto").as_path()],
18+
None,
19+
Some(&out_dir.as_path()),
20+
&[in_dir.as_path()],
21+
)
22+
.unwrap()
23+
.build(),
24+
)
25+
.unwrap();
26+
}

frameworks/wtx-grpc/meta.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"display_name": "wtx",
3+
"language": "Rust",
4+
"type": "production",
5+
"engine": "wtx",
6+
"description": "WTX - gRPC Server",
7+
"repo": "https://github.com/c410-f3r/wtx",
8+
"enabled": true,
9+
"tests": [
10+
"unary-grpc"
11+
]
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
syntax = "proto3";
2+
package benchmark;
3+
4+
service BenchmarkService {
5+
rpc GetSum (SumRequest) returns (SumReply);
6+
}
7+
8+
message SumRequest {
9+
int32 a = 1;
10+
int32 b = 2;
11+
}
12+
13+
message SumReply {
14+
int32 result = 1;
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!(concat!(env!("OUT_DIR"), "/proto/mod.rs"));

frameworks/wtx-grpc/src/main.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)