hyperium / tonic

A native gRPC client & server implementation with async/await support.
https://docs.rs/tonic
MIT License
10.02k stars 1.02k forks source link

Router should support request body types other than hyper::Body #905

Open sfackler opened 2 years ago

sfackler commented 2 years ago

The generated server Service implementations already support arbitrary request body types, but Router is currently hardcoded to work with hyper::Body directly. It'd be great if that were relaxed to work with any suitable body type.

LucioFranco commented 2 years ago

Agreed we can make this change. I believe we should do this in conjunction with https://github.com/hyperium/tonic/pull/830

LucioFranco commented 2 years ago

I am going to remove this from 0.7 because it will be quite challenging to get this change in right now and I would actually like to refactor the transport module in the next release (aka remove it and re do it).

davidpdrsn commented 2 years ago

Is it actually possible? Hyper requires services accept Request<hyper::Body>. You can change the body in a middleware but the top level body must be hyper::Body.

sfackler commented 2 years ago

You can install layers between hyper and the tonic service that change the body type.

davidpdrsn commented 2 years ago

Yes that's what I meant.

sfackler commented 2 years ago

So then it's totally possible to create a Hyper service that changes the request body type and then feeds that modified body type into the Tonic Router, except that Router won't let you do that.

tobz commented 2 years ago

This would be extremely helpful for a particular issue I'm working on. As @sfackler points out, the major sticking point seems to be where tonic meets axum. Without this, the only current workaround is doing very goofy stuff like getting a channel-backed hyper::Body, even if you only needed to do something like count the number of bytes per data chunk emitted by the body, etc.

I'd be interested in trying to work on/submit a PR for this. Is this something you'd be receptive to, given the thoughts/plans around breaking tonic apart into more composable chunks?