Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow FnMut in Interceptor #372

Closed
dfreese opened this issue Jun 16, 2020 · 5 comments · Fixed by #651
Closed

Allow FnMut in Interceptor #372

dfreese opened this issue Jun 16, 2020 · 5 comments · Fixed by #651
Labels
A-tonic C-question Category: Further information is requested

Comments

@dfreese
Copy link
Contributor

dfreese commented Jun 16, 2020

I wrote a json web token authorization interceptor based on the gcp example (super helpful, btw!). This let me use a service account key to authenticate with google services.

There was one thing that cam up in the process. Since Interceptor is built from Fn, rather than FnMut, I end up being forced to never reauthorizing the key (without tearing down the client) or reauthorizing the key every time.

Looking at the code for tonic::client::Grpc::streaming, it seems like it would be fairly straight-forward to add something like a IterceptorMut. However, from the docs, it sounds like this design was slightly intentional and that the most appropriate way of doing this would be a tower middleware.

Reading through the tonic and hyper docs/source, as well as #69 and #70, there doesn't seem to be
a clean way of hooking those in. Am I missing a better way of doing this? If not, is there a way you envisioned this happening? (It also might make more sense to look at this within hyper)

Happy to work through implementation if there's a general design that you'd want, or take a stab at the design if there's support for changing APIs around to support this feature.

@LucioFranco
Copy link
Member

Yeah, the idea was intentional since an interceptor can run on multiple tasks at once and on different threads. The solution is to go with the tower middleware but that isn't 100% simple. On the server side there is the hyper_warp example which shows how to work with services a bit.

@LucioFranco LucioFranco added A-tonic C-question Category: Further information is requested labels Jun 16, 2020
@dfreese
Copy link
Contributor Author

dfreese commented Jun 16, 2020

Thanks. I'll take a look at that example a little more closely and see how to best translate it for the client side.

@LucioFranco
Copy link
Member

Let me know if you have any questions in discord and we can chat a bit more.

@dfreese
Copy link
Contributor Author

dfreese commented Jul 26, 2020

@LucioFranco thanks for the offer. I'm still interested in trying to get something together and will reach out if I run into problems once I have a chance to get to this. Unfortunately, I ran into the classic problem of being able to get enough working that I haven't had the time to prioritize it further.

The addition of the server-side example in #375 will also probably help as a guide.

@LucioFranco LucioFranco added this to the 0.4 milestone Nov 27, 2020
@dfreese
Copy link
Contributor Author

dfreese commented Mar 3, 2021

Looking at this a little more closely, there doesn't seem to be a clear way to access the underlying tower layers for the transport::Channel. The server side seems to have the into_service escape hatch that could be used to combine with server middleware.

It does look like the Channel from the connect functions could be returned fairly easily, but I'm not sure if there's a specific API suggestion I would make at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tonic C-question Category: Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants