tonic icon indicating copy to clipboard operation
tonic copied to clipboard

What's the idiomatic way of passing data from a layer to the actual service implementation?

Open sonthonaxrk opened this issue 4 years ago • 2 comments

I'm looking at this example where you use a tower layer to do some async processing before a request (in my case I want to check if a use is authenticated).

However, one problem I'm facing with the tower layer is that _I don't know how to pass the result to the Service. In the example below, you can do some 'extra work', but you just pass the request further up the stack of layers. For instance, I want to be able to pass a user ID up the stack.

https://github.com/hyperium/tonic/blob/master/examples/src/tower/server.rs#L105

In the above example, you pass a hyper::Request, which doesn't really have anyway of attaching metadata (beyond something silly like adding it to the headers).

sonthonaxrk avatar Nov 18 '21 16:11 sonthonaxrk

It should be possible to write an example for this I think

LucioFranco avatar Dec 08 '21 00:12 LucioFranco

You can set your data in a http::Request extension and then extract it in your RPC via tonic::Request::extension.

Tonic automatically copies http extensions into grpc extensions.

davidpdrsn avatar Dec 08 '21 08:12 davidpdrsn