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

Re-use Request instance? #694

Open ERnsTL opened 3 years ago

ERnsTL commented 3 years ago

Feature Request

Crates

tonic

Motivation

tonic seems slow in use-cases where many similar requests need to be sent.

Proposal

Re-use of the tonic::Request instance, just setting the payload new -- or even just overwriting some fields of the payload.

Very simple example:

        let request = tonic::Request::new(WeatherRequest{
                location: "Vienna/Austria".into()
        });

Current situation: "trait Copy not implemented for struct Request" -- "value moved in previous iteration".

Alternatives

Not sure.

Jason5Lee commented 3 years ago

I think it's related to tokio-rs/prost/issues/429, currently Rust protobuf generator doesn't allow you to choose the pointer, in which case you're not allowed to use Arc for reusing value. I hope it can be implemented soon.

LucioFranco commented 2 years ago

http::Request is not clonable due to the Extensions type it uses. You can though construct your proto to use bytes and then use the bytes feature in prost to have the bytes field be Bytes. This then would allow you to cheaply clone the bytes and avoid heavy clones.

LucioFranco commented 2 years ago

Related issue https://github.com/hyperium/http/issues/395