Comments (12)
@zackangelo is correct!
So pretty much this comes down to how tower handles back pressure. Internally we need a &mut self within the future to keep checking if our inner service is ready to accept the next request. To do this we need to borrow the service exclusively because only one access to that service can submit the request when its ready. To fix this internally we use a buffer channel to allow you to multiplex this concept of polling ready and handling back pressure. Hopefully this explains it a bit better.
from tonic.
@frol this works:
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let main_client = GreeterClient::connect("http://[::1]:50051")?;
let mut client1 = main_client.clone();
let mut client2 = main_client.clone();
let res1 = client1.say_hello(tonic::Request::new(HelloRequest {
name: "hello".into(),
}));
let res2 = client2.say_hello(tonic::Request::new(HelloRequest {
name: "world".into(),
}));
let responses = futures::future::try_join_all(vec![res1, res2]).await?;
println!("RESPONSE={:?}", responses);
Ok(())
}
from tonic.
@blittable see #44 (comment) for a possible solution
from tonic.
@gabrik see previous info #33 (comment)
from tonic.
@frol I believe that the .clone()
operation on a tonic client should be relatively lightweight (just a handle to the underlying channel). You should be able to dispatch a request in parallel by cloning the client before executing its method.
from tonic.
Thank you for the thorough explanation!
Just to confirm, I was able to use .clone()
just fine:
use futures::join;
pub mod hello_world {
tonic::include_proto!("helloworld");
}
use hello_world::{client::GreeterClient, HelloRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let main_client = GreeterClient::connect("http://[::1]:50051")?;
let mut client = main_client.clone();
let res1 = client.say_hello(tonic::Request::new(HelloRequest { name: "hello1".into() }));
let mut client = main_client.clone();
let res2 = client.say_hello(tonic::Request::new(HelloRequest { name: "hello2".into() }));
println!("RESPONSES={:?}", join!(res1, res2));
Ok(())
}
Could you help me with a solution to fetch a list of resources concurrently (e.g. I have a vector of tonic::Requests
)? I have tried to use futures::future::join_all
:
pub mod hello_world {
tonic::include_proto!("helloworld");
}
use hello_world::{client::GreeterClient, HelloRequest};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let main_client = GreeterClient::connect("http://[::1]:50051")?;
let mut client = main_client.clone();
let res1 = client.say_hello(tonic::Request::new(HelloRequest { name: "hello1".into() }));
let mut client = main_client.clone();
let res2 = client.say_hello(tonic::Request::new(HelloRequest { name: "hello2".into() }));
println!("RESPONSES={:?}", futures::future::join_all(vec![res1, res2]).await);
Ok(())
}
error[E0597]: `client` does not live long enough
--> tonic-examples/src/helloworld/client.rs:17:16
|
17| let res2 = client.say_hello(tonic::Request::new(HelloRequest { name: "hello2".into() }));
| ^^^^^^ borrowed value does not live long enough
...
22| }
| -
| |
| `client` dropped here while still borrowed
| borrow might be used here, when `res1` is dropped and runs the destructor for type `impl core::future::future::Future`
And even if that would work, I would still need to store the clients
somewhere in a list, right?
from tonic.
Super-helpful... thank you.
If it was in a loop? I'm generating the req/resp from the protobufs, and tonic::response::Response - I cannot clone it.
This definitely inspired me to test the streaming approach, which works swimmingly. But, a batch of requests and handling their responses, does this require an Arc<Mutex>
and pinning around the data-structure?
from tonic.
I think we can close this, feel free to reopen if you have any more questions :)
from tonic.
Rust and tonic noob here, but if client.clone()
is cheap and required frequently, wouldn't it be easier if client types had the Copy
trait and client calls would take self
by value instead of mutable reference?
from tonic.
@ettersi Copy
is for types that can be copied by simplying copying their byte representation, such as i32
. Two i32
s are identical if the contain the same bytes. Whereas something like Box<String>
is different. You cannot get a clone of the box by just copying the boxes bytes on the stack. There is memory on the heap to deal with. So Copy
doesn't simply mean "cheap to clone".
from tonic.
Right, I forgot that it has to be a bitwise copy. Sorry about the spam, then.
from tonic.
Quick question, given that the Clone
is lightweight, why does the client need &mut self
instead of &self
?
from tonic.
Related Issues (20)
- Content-Type Validation Missing: Requests Not Returning HTTP 415 When Content-Type Is Not Start with `application/grpc`
- http2_keep_alive_interval doesn't work HOT 3
- Current "uds" example does not compile HOT 3
- Release 0.12.0 HOT 6
- `tonic::transport::Channel` struct is not as safe to clone for parallel use HOT 2
- Error when building excamples
- how to manually fail a client streaming?
- Add debug spans for codec tracing
- test codec::prost::tests::encode_too_big panicks on 32-bit architectures
- Inserting metadata in a test panics with index out of bounds HOT 2
- Tonic 0.12.0 compiles tokio even when no features are enabled HOT 4
- Upgrading to tonic-reflection 0.12 breaks reflection HOT 2
- use of undeclared crate or module `tonic_reflection` HOT 2
- How does tonic library dynamically set up custom middleware HOT 1
- Naming collisions with std::marker::Sync/Send
- How to make tonic support tls HOT 3
- nit: inconsistent use of crate::Error vs Error in single transport src file
- calling grpc server hosted on cloud run with https fails with transport error HOT 2
- version 0.12.1 causes ClientTlsConfig to explode with invalid CryptoProvider HOT 1
- Configuring Timeout for Graceful Shutdown HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tonic.