-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rust support #226
Closed
Closed
Rust support #226
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a2c0e84
Add cargo-raze build files for grpc-rust crate
damienmg 20d33f0
Add Rust support
damienmg 62e32fd
Add an integration test for the Rust example
damienmg aad76a2
Point rules_rust to the upstream repository
damienmg 421d7f6
Move the test to examples/helloworld/rust as this seems to be the con…
damienmg 21e7fbf
Add a dummy Cargo craze to use cargo raze on it.
damienmg 8017e7c
Add generate files generated with the new raze
damienmg 6fad944
Use the new generated files from cargo raze
damienmg 083230a
Remove generated files from raze in the old format
damienmg f01c1fe
Address acmcarther@ suggestions
damienmg 61f52c2
Regenerate files with cargo-raze HEAD
damienmg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("//rust:rules.bzl", "rust_proto_library") | ||
|
||
rust_proto_library( | ||
name = "helloworld", | ||
proto_deps = [ | ||
"//examples/proto:rust", | ||
], | ||
protos = ["//examples/helloworld/proto:protos"], | ||
verbose = 0, | ||
visibility = ["//examples/helloworld/rust:__subpackages__"], | ||
with_grpc = True, | ||
) | ||
|
||
sh_test( | ||
name = "test_rust", | ||
srcs = ["test_rust.sh"], | ||
data = [ | ||
"//examples/helloworld/rust/greeter_client", | ||
"//examples/helloworld/rust/greeter_server", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary") | ||
load("//rust:rules.bzl", "GRPC_COMPILE_DEPS", "rust_proto_library") | ||
|
||
rust_binary( | ||
name = "greeter_client", | ||
srcs = ["greeter_client.rs"], | ||
deps = ["//examples/helloworld/rust:helloworld"] + GRPC_COMPILE_DEPS, | ||
visibility = ["//examples/helloworld/rust:__subpackages__"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
extern crate grpc; | ||
extern crate helloworld; | ||
|
||
use std::env; | ||
use std::str::FromStr; | ||
|
||
use helloworld::*; | ||
|
||
fn parse_args() -> (String, u16) { | ||
let mut name = "world".to_owned(); | ||
let mut port = 50051; | ||
for arg in env::args().skip(1) { | ||
if arg.starts_with("-p=") { | ||
port = u16::from_str(&arg[3..]).unwrap() | ||
} else { | ||
name = arg.to_owned(); | ||
} | ||
} | ||
(name, port) | ||
} | ||
|
||
fn main() { | ||
let (name, port) = parse_args(); | ||
let client = GreeterClient::new_plain("::1", port, Default::default()).unwrap(); | ||
let mut req = HelloRequest::new(); | ||
req.set_name(name); | ||
let resp = client.say_hello(grpc::RequestOptions::new(), req); | ||
println!("{:?}", resp.wait()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary") | ||
load("//rust:rules.bzl", "GRPC_COMPILE_DEPS", "rust_proto_library") | ||
|
||
rust_binary( | ||
name = "greeter_server", | ||
srcs = ["greeter_server.rs"], | ||
deps = ["//examples/helloworld/rust:helloworld"] + GRPC_COMPILE_DEPS, | ||
visibility = ["//examples/helloworld/rust:__subpackages__"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
extern crate helloworld; | ||
extern crate grpc; | ||
extern crate tls_api_stub; | ||
|
||
use std::thread; | ||
use std::env; | ||
use std::str::FromStr; | ||
|
||
use helloworld::*; | ||
|
||
struct GreeterImpl; | ||
|
||
impl Greeter for GreeterImpl { | ||
fn say_hello(&self, _m: grpc::RequestOptions, req: HelloRequest) -> grpc::SingleResponse<HelloReply> { | ||
let mut r = HelloReply::new(); | ||
let name = if req.get_name().is_empty() { "world" } else { req.get_name() }; | ||
println!("greeting request from {}", name); | ||
r.set_message(format!("Hello {}", name)); | ||
grpc::SingleResponse::completed(r) | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut server = grpc::ServerBuilder::<tls_api_stub::TlsAcceptor>::new(); | ||
let port = u16::from_str(&env::args().nth(1).unwrap_or("50051".to_owned())).unwrap(); | ||
server.http.set_port(port); | ||
server.add_service(GreeterServer::new_service_def(GreeterImpl)); | ||
server.http.set_cpu_pool_threads(4); | ||
let server = server.build().expect("server"); | ||
let port = server.local_addr().port().unwrap(); | ||
println!("greeter server started on port {}", port); | ||
|
||
loop { | ||
thread::park(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/bin/bash | ||
# A simple test for the rust client-server. | ||
set -eu | ||
set -o pipefail | ||
|
||
# Try to locate the runfiles directory | ||
get_runfiles_dir() { | ||
if [ -d "${BASH_SOURCE[0]}.runfiles" ]; then | ||
cd "${BASH_SOURCE[0]}.runfiles/org_pubref_rules_protobuf" | ||
elif ! [ -d "${PWD}/../org_pubref_rules_protobuf" ]; then | ||
cd "$(readlink ${BASH_SOURCE[0]}).runfiles/org_pubref_rules_protobuf" | ||
fi | ||
pwd -P | ||
} | ||
|
||
RUNFILES="${RUNFILES:-"$(get_runfiles_dir)"}" | ||
TEST_TMPDIR="${TEST_TMPDIR:-$(mktemp -d ${TMP:-/tmp}/tmp.XXXXXXXX)}" | ||
SERVER="${RUNFILES}/examples/helloworld/rust/greeter_server/greeter_server" | ||
CLIENT="${RUNFILES}/examples/helloworld/rust/greeter_client/greeter_client" | ||
|
||
SERVER_LOG="${TEST_TMPDIR}/server.log" | ||
CLIENT_LOG="${TEST_TMPDIR}/client.log" | ||
|
||
# Simply match a string from the log | ||
expect_log() { | ||
local log="${2-${CLIENT_LOG}}" | ||
if ! grep -qF "$1" "${log}"; then | ||
echo "Substring '$1' not found in ${log}." >&2 | ||
echo "=== ${log} ===" >&2 | ||
cat "${log}" >&2 | ||
echo "=== end of ${log} ===" >&2 | ||
fi | ||
} | ||
|
||
# Start the server and get its pid and its port number | ||
"${SERVER}" 0 | tee "${SERVER_LOG}" & | ||
server_pid=$? | ||
trap "kill ${server_pid}" EXIT | ||
|
||
while ! grep -q "greeter server started on port" "${SERVER_LOG}" &>/dev/null; do | ||
sleep 1 | ||
done | ||
|
||
PORT=$(grep "greeter server started on port" "${SERVER_LOG}" \ | ||
| sed -E 's/^.*port //') | ||
|
||
# Now run the client | ||
"${CLIENT}" -p="${PORT}" | tee "${CLIENT_LOG}" | ||
expect_log 'message: "Hello world"' | ||
expect_log 'greeting request from world' "${SERVER_LOG}" | ||
|
||
"${CLIENT}" -p="${PORT}" thou | tee "${CLIENT_LOG}" | ||
expect_log 'message: "Hello thou"' | ||
expect_log 'greeting request from thou' "${SERVER_LOG}" | ||
|
||
echo "" | ||
echo "SUCCESS" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add rust_proto_library here as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.