-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Initial simple grpc channel implementation #2338
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
Merged
+2,204
−165
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b542d9e
move work in fork to tonic/grpc
dfawley dac9d0a
delete TcpStream type; add allowed external types
dfawley c63ef5f
set rust-version in grpc's Cargo.toml
dfawley 931d7e9
remove version
dfawley 6424342
update rust version to 1.86 for trait upcasting feature
dfawley 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
use std::any::Any; | ||
|
||
use futures_util::stream::StreamExt; | ||
use grpc::service::{Message, Request, Response, Service}; | ||
use grpc::{client::ChannelOptions, inmemory}; | ||
use tonic::async_trait; | ||
|
||
struct Handler {} | ||
|
||
#[derive(Debug)] | ||
struct MyReqMessage(String); | ||
|
||
impl Message for MyReqMessage {} | ||
|
||
#[derive(Debug)] | ||
struct MyResMessage(String); | ||
impl Message for MyResMessage {} | ||
|
||
#[async_trait] | ||
impl Service for Handler { | ||
async fn call(&self, method: String, request: Request) -> Response { | ||
let mut stream = request.into_inner(); | ||
let output = async_stream::try_stream! { | ||
while let Some(req) = stream.next().await { | ||
yield Box::new(MyResMessage(format!( | ||
"Server: responding to: {}; msg: {}", | ||
method, (req as Box<dyn Any>).downcast_ref::<MyReqMessage>().unwrap().0, | ||
))) as Box<dyn Message>; | ||
} | ||
}; | ||
|
||
Response::new(Box::pin(output)) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
inmemory::reg(); | ||
|
||
// Spawn the server. | ||
let lis = inmemory::Listener::new(); | ||
let mut srv = grpc::server::Server::new(); | ||
srv.set_handler(Handler {}); | ||
let lis_clone = lis.clone(); | ||
tokio::task::spawn(async move { | ||
srv.serve(&lis_clone).await; | ||
println!("serve returned for listener 1!"); | ||
}); | ||
|
||
println!("Creating channel for {}", lis.target()); | ||
let chan_opts = ChannelOptions::default(); | ||
let chan = grpc::client::Channel::new(lis.target().as_str(), None, chan_opts); | ||
|
||
let outbound = async_stream::stream! { | ||
yield Box::new(MyReqMessage("My Request 1".to_string())) as Box<dyn Message>; | ||
yield Box::new(MyReqMessage("My Request 2".to_string())); | ||
yield Box::new(MyReqMessage("My Request 3".to_string())); | ||
}; | ||
|
||
let req = Request::new(Box::pin(outbound)); | ||
let res = chan.call("/some/method".to_string(), req).await; | ||
let mut res = res.into_inner(); | ||
|
||
while let Some(resp) = res.next().await { | ||
println!( | ||
"CALL RESPONSE: {}", | ||
(resp.unwrap() as Box<dyn Any>) | ||
.downcast_ref::<MyResMessage>() | ||
.unwrap() | ||
.0, | ||
); | ||
} | ||
lis.close().await; | ||
} |
This file contains hidden or 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,101 @@ | ||
use std::any::Any; | ||
|
||
use futures_util::StreamExt; | ||
use grpc::service::{Message, Request, Response, Service}; | ||
use grpc::{client::ChannelOptions, inmemory}; | ||
use tonic::async_trait; | ||
|
||
struct Handler { | ||
id: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct MyReqMessage(String); | ||
|
||
impl Message for MyReqMessage {} | ||
|
||
#[derive(Debug)] | ||
struct MyResMessage(String); | ||
impl Message for MyResMessage {} | ||
|
||
#[async_trait] | ||
impl Service for Handler { | ||
async fn call(&self, method: String, request: Request) -> Response { | ||
let id = self.id.clone(); | ||
let mut stream = request.into_inner(); | ||
let output = async_stream::try_stream! { | ||
while let Some(req) = stream.next().await { | ||
yield Box::new(MyResMessage(format!( | ||
"Server {}: responding to: {}; msg: {}", | ||
id, method, (req as Box<dyn Any>).downcast_ref::<MyReqMessage>().unwrap().0, | ||
))) as Box<dyn Message>; | ||
} | ||
}; | ||
|
||
Response::new(Box::pin(output)) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
inmemory::reg(); | ||
|
||
// Spawn the first server. | ||
let lis1 = inmemory::Listener::new(); | ||
let mut srv = grpc::server::Server::new(); | ||
srv.set_handler(Handler { id: lis1.id() }); | ||
let lis1_clone = lis1.clone(); | ||
tokio::task::spawn(async move { | ||
srv.serve(&lis1_clone).await; | ||
println!("serve returned for listener 1!"); | ||
}); | ||
|
||
// Spawn the second server. | ||
let lis2 = inmemory::Listener::new(); | ||
let mut srv = grpc::server::Server::new(); | ||
srv.set_handler(Handler { id: lis2.id() }); | ||
let lis2_clone = lis2.clone(); | ||
tokio::task::spawn(async move { | ||
srv.serve(&lis2_clone).await; | ||
println!("serve returned for listener 2!"); | ||
}); | ||
|
||
// Spawn the third server. | ||
let lis3 = inmemory::Listener::new(); | ||
let mut srv = grpc::server::Server::new(); | ||
srv.set_handler(Handler { id: lis3.id() }); | ||
let lis3_clone = lis3.clone(); | ||
tokio::task::spawn(async move { | ||
srv.serve(&lis3_clone).await; | ||
println!("serve returned for listener 3!"); | ||
}); | ||
|
||
let target = String::from("inmemory:///dummy"); | ||
println!("Creating channel for {target}"); | ||
let chan_opts = ChannelOptions::default(); | ||
let chan = grpc::client::Channel::new(target.as_str(), None, chan_opts); | ||
|
||
let outbound = async_stream::stream! { | ||
yield Box::new(MyReqMessage("My Request 1".to_string())) as Box<dyn Message>; | ||
yield Box::new(MyReqMessage("My Request 2".to_string())); | ||
yield Box::new(MyReqMessage("My Request 3".to_string())); | ||
}; | ||
|
||
let req = Request::new(Box::pin(outbound)); | ||
let res = chan.call("/some/method".to_string(), req).await; | ||
let mut res = res.into_inner(); | ||
|
||
while let Some(resp) = res.next().await { | ||
println!( | ||
"CALL RESPONSE: {}", | ||
(resp.unwrap() as Box<dyn Any>) | ||
.downcast_ref::<MyResMessage>() | ||
.unwrap() | ||
.0, | ||
); | ||
} | ||
|
||
lis1.close().await; | ||
lis2.close().await; | ||
lis3.close().await; | ||
} |
This file contains hidden or 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.
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.
In a follow up we should relax the versions required here unless we need a feature in a specific patch.