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

fix(transport): Make Server::layer() support more than one layer #932

Merged
merged 2 commits into from
Mar 1, 2022

Conversation

Arcterus
Copy link
Contributor

The current behavior is to replace the previous layer with the newly specified layer. The new behavior is to chain the layers together. The current behavior might actually be intentional, but IMO it's not very intuitive given how services are added by chaining method calls.

I can adjust the docs and/or tests as necessary, so let me know if you want me to do so.

Motivation

We were using Server::layer() and assumed it supported more than one layer (without using Stack from tower) given the example in the docs (which has an interceptor for authentication that, with the current behavior, gets replaced with effectively an identity interceptor afaict).

Solution

This change basically just uses Stack internally rather than requiring users to use it themselves manually.

Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've actually been thinking that we should consider changing Server::layer so it can be called after services have been added, and applies the middleware to the services already added. This is how axum does it and I personally find it easier to understand. Adding the middleware first, then the services, seems a bit backwards to me.

In general we're not really making much use of the fact that tonic's uses axum internally for its routing. We could add more flexible APIs such as merging routers. That is probably outside the scope of this PR but thought I would mention it.

I think this changes makes sense on its own though.

@davidpdrsn davidpdrsn added this to the 0.7 milestone Mar 1, 2022
tonic/src/transport/server/mod.rs Outdated Show resolved Hide resolved
@Arcterus
Copy link
Contributor Author

Arcterus commented Mar 1, 2022

I haven't used axum much, so ¯\_(ツ)_/¯.

When you say the middleware applies to the services you specified previously, does that mean you can layer the middleware so some only apply to a subset of services? Like, you could add more services after adding a middleware and those new services would not be affected by the middleware? If so, I'd definitely prefer that approach. We currently have an ad hoc mechanism to do something like this, which I'd prefer to remove if possible.

@LucioFranco
Copy link
Member

It should be possible to achieve this right now by just passing in your own service builder correct? I honestly want to just nix the entire server code and just use axum at this point 😆

@davidpdrsn
Copy link
Member

It should be possible to achieve this right now by just passing in your own service builder correct?

Yeah this would just be a convenience.

I honestly want to just nix the entire server code and just use axum at this point 😆

👌

@LucioFranco LucioFranco changed the title Make Server::layer() support more than one layer fix(transport): Make Server::layer() support more than one layer Mar 1, 2022
Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM just one question

service_builder: ServiceBuilder<L>,
}

impl Default for Server<Identity> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we shouldn't have to implement this manually, is there something missing in tower?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently Default is only implemented for ServiceBuilder<Identity>, so the derive fails.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay huh I guess derive doesn't take into account default bounds

@LucioFranco LucioFranco requested a review from davidpdrsn March 1, 2022 20:33
@LucioFranco LucioFranco merged commit e30bb7e into hyperium:master Mar 1, 2022
@allan2 allan2 mentioned this pull request Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants