Skip to content

Commit

Permalink
Merge pull request LesnyRumcajs#96 from trezm/feat/add_mt_and_st_for_…
Browse files Browse the repository at this point in the history
…thruster

Add multi and single threaded thruster examples
  • Loading branch information
LesnyRumcajs authored Aug 19, 2020
2 parents dc43d0a + 0a39bb1 commit ed21853
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 1,453 deletions.
1,437 changes: 0 additions & 1,437 deletions rust_thruster_bench/Cargo.lock

This file was deleted.

10 changes: 0 additions & 10 deletions rust_thruster_bench/Dockerfile

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ lto = true

[dependencies]
async-trait = "0.1"
bytes = "0.5.3"
dotenv = "0.13.0"
env_logger = "0.7.1"
log = "0.4"
Expand All @@ -22,6 +23,8 @@ thruster = { version = "1.0.0", features = ["hyper_server"] }
thruster-grpc = { git = "https://github.com/thruster-rs/thruster-grpc" }
tokio = { version = "0.2", features = ["full"] }

futures = "0.3.5"

[build-dependencies]
prost-build = "0.6"

Expand Down
13 changes: 13 additions & 0 deletions rust_thruster_mt_bench/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM rust:1.44.1-stretch

WORKDIR /app
COPY rust_thruster_mt_bench /app
COPY proto /app/proto

RUN rustup component add rustfmt --toolchain 1.44.1-x86_64-unknown-linux-gnu

RUN cat Cargo.toml
RUN cargo build --release
RUN cargo build --release

ENTRYPOINT cargo run --release
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ mod hello_world {
}

#[middleware_fn]
pub async fn say_hello(context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
let hello_world_request = context_to_message::<hello_world::HelloRequest>(context)
pub async fn say_hello(mut context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
let hello_world_request = context_to_message::<hello_world::HelloRequest>(&mut context)
.await
.unwrap();

Ok(message_to_context(
Ctx::default(),
context,
hello_world::HelloReply {
message: hello_world_request.name,
},
Expand All @@ -37,14 +37,13 @@ async fn main() {
let host = env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string());
let port = env::var("PORT").unwrap_or_else(|_| "50051".to_string());

info!("Starting server at {}:{}", host, port);
info!("Starting server at {}:{}!", host, port);

let mut app = App::<HyperRequest, Ctx, ()>::create(generate_context, ());
app.post(
"/helloworld.Greeter/SayHello",
async_middleware!(Ctx, [say_hello]),
);

let server = ProtoServer::new(app);
server.build(&host, port.parse::<u16>().unwrap()).await;
ProtoServer::new(app).build(&host, port.parse::<u16>().unwrap()).await;
}
1 change: 1 addition & 0 deletions rust_thruster_st_bench/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
33 changes: 33 additions & 0 deletions rust_thruster_st_bench/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "rust_thruster"
version = "0.1.0"
authors = ["trezm <[email protected]>"]
edition = "2018"

[profile.release]
opt-level = 3
codegen-units = 1
lto = true

[dependencies]
async-trait = "0.1"
bytes = "0.5.3"
dotenv = "0.13.0"
env_logger = "0.7.1"
log = "0.4"
hyper = "0.13.1"
http = "0.2"
http-body = "0.3.1"
prost = "0.6"
thruster = { version = "1.0.0", features = ["hyper_server"] }
thruster-grpc = { git = "https://github.com/thruster-rs/thruster-grpc" }
tokio = { version = "0.2", features = ["full"] }

futures = "0.3.5"

[build-dependencies]
prost-build = "0.6"

[[bin]]
name = "helloworld-server"
path = "src/main.rs"
11 changes: 11 additions & 0 deletions rust_thruster_st_bench/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM rust:1.44.1-stretch

WORKDIR /app
COPY rust_thruster_st_bench /app
COPY proto /app/proto

RUN rustup component add rustfmt --toolchain 1.44.1-x86_64-unknown-linux-gnu
RUN cargo build --release
RUN cargo build --release

ENTRYPOINT cargo run --release
3 changes: 3 additions & 0 deletions rust_thruster_st_bench/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
prost_build::compile_protos(&["proto/helloworld/helloworld.proto"], &["proto/"]).unwrap();
}
49 changes: 49 additions & 0 deletions rust_thruster_st_bench/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use dotenv::dotenv;
use log::info;
use std::env;
use thruster::context::hyper_request::HyperRequest;
use thruster::{async_middleware, middleware_fn};
use thruster::{App, ThrusterServer};
use thruster::{MiddlewareNext, MiddlewareResult};
use thruster_grpc::context::{generate_context, ProtoContext as Ctx};
use thruster_grpc::server::ProtoServer;
use thruster_grpc::util::{context_to_message, message_to_context};

mod hello_world {
include!(concat!(env!("OUT_DIR"), "/helloworld.rs"));
}

#[middleware_fn]
pub async fn say_hello(mut context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
let hello_world_request = context_to_message::<hello_world::HelloRequest>(&mut context)
.await
.unwrap();

Ok(message_to_context(
context,
hello_world::HelloReply {
message: hello_world_request.name,
},
)
.await)
}

#[tokio::main(core_threads = 1)]
async fn main() {
let _ = dotenv();

env_logger::init();

let host = env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string());
let port = env::var("PORT").unwrap_or_else(|_| "50051".to_string());

info!("Starting server at {}:{}!", host, port);

let mut app = App::<HyperRequest, Ctx, ()>::create(generate_context, ());
app.post(
"/helloworld.Greeter/SayHello",
async_middleware!(Ctx, [say_hello]),
);

ProtoServer::new(app).build(&host, port.parse::<u16>().unwrap()).await;
}

0 comments on commit ed21853

Please sign in to comment.