Comments (10)
In 2023 I don't think much has changed in terms of making naming client types any easier. Is there any interest in having the generated client implement the generated service trait?
If so, I'm happy to contribute.
from tonic.
Just chiming in, I'm also hitting this issue in the context of trying to store the tonic client inside of my own struct.
pub struct SchemaClient<T> {
inner: SchemaServiceClient<T>
}
I then use a tower ServiceBuilder
to setup the channel. I want to be able to configure some layers to be added/removed based on on some configs, but changing the layers seems to change my T
which makes it really hard to store one of these things.
from tonic.
Interceptors can be made using named types by implementing the Interceptor
trait. There are a few examples here. It's not the most ergonomic thing ever but at least its possible and can be cleaned up by defining your own type
alias.
from tonic.
An easy workaround is to use a concrete type instead of the type parameter. Currently, I believe the only concrete type that can be used to construct a client is a Channel
, so you could do:
async fn foo(mut client: MyClient<Channel>) {...}
Here's an example:
https://github.com/hyperium/tonic/blob/master/tonic-examples/src/routeguide/client.rs#L17
from tonic.
Huh, that totally solves my particular issue. Not sure why I didn't think of that earlier!
Thanks! 😄
That said, there are times when it might be useful to write transport-independent functions. For example, a API might require first calling client.update_foo()
, and if that fails, falling back to client.create_foo()
. It would be nice to easily write a helper function update_or_create_foo<C>(client: FooClient<C>)
that orchestrates those gRPC calls.
from tonic.
I agree, you raise a good point. I believe there are plans (and maybe even some initial work) to provide different transports, in which case we may need to find a different solution.
from tonic.
@daniel5151 yeah, so I tried to get https://docs.rs/tonic/0.1.0-alpha.5/tonic/client/trait.GrpcService.html to do as much of that work as possible but it seemed that rust was not happen. Sounds like this would be a good thing to revisit.
from tonic.
So, what @alce said (about Channel
being the only thing you can create a client with) is not really true anymore and with Interceptors
this quickly becomes unwieldy since you can get types like: Client<tonic::codegen::InterceptedService<HttpStatusInterceptor, [closure@src/registry.rs:83:9: 126:10]>>
. Even returning this from a function is a no-go :)
from tonic.
Sure, but that will only get rid of the closure
part so it is (as you said) still not really "ergonomic" :)
from tonic.
I'm running into this while trying to use Tower middleware. Using a service builder means that I now have a pretty deeply nested type instead of a channel. The following example spits outs a Timeout<InterceptedService<InterceptedService<Channel, {[email protected]:140:44}>, ...>>>
.
let channel = ServiceBuilder::new()
.timeout(Duration::from_secs(30))
.layer(tonic::service::interceptor(my_cool_interceptor(
ctx.clone(),
)))
.layer(tonic::service::interceptor(|req| Ok(req)))
.service(channel);
Some (generated?) documentation about the bounds to include when passing a client to a fn would be awesome.
from tonic.
Related Issues (20)
- Duplex transport is incompatible with `serve_with_incoming_shutdown`
- Introduce a build option to not add `#[async_trait]` on generated service.
- BrokenPipe Error Using TLS With Root Certificate
- Received Status.source = None when server sends Status.source = Some
- Use a `Sink` argument instead of returning a `Stream` for response streaming
- Limit of 4M on outgoing messages HOT 3
- Malformed response for non `Ok` statuses when using `web_tonic` on the server.
- Dynamically named services
- Bump hyper / http / http-body to 1.x version HOT 2
- Integration of flatbuffers
- gRPC-web CORS issue HOT 1
- Add a way to test/inspect server and router build/configuration
- GOAWAY message leads to other Clients/Channels being unresponsive
- `tonic-reflection` is still using deprecated file descriptor set reflection_v1alpha1 HOT 1
- tonic-build: Codegen generates a name collision after auto-capitalization is applied, resulting in cargo build errors HOT 1
- `tonic-health`: add `NOT_SERVING` override for all registered services
- Idiomatic import paths
- IPv6 endpoint passed through the URL will crash
- Async Interceptor support HOT 2
- How to create channel without tls when tls feature flag is enabled 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.