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

Feature Request: implement layer method on ServiceExt #766

Open
tp971 opened this issue Mar 28, 2024 · 2 comments
Open

Feature Request: implement layer method on ServiceExt #766

tp971 opened this issue Mar 28, 2024 · 2 comments
Labels
A-layer Area: The tower `Layer` trait C-feature-request Category: A feature request, i.e: not implemented / a PR. I-needs-decision Issues in need of decision. P-low Low priority T-middleware Topic: middleware

Comments

@tp971
Copy link

tp971 commented Mar 28, 2024

I am currently working on a project where middleware is applied bottom-up, i.e. the middleware is appended after the services (which is also the case in axum for example, where middlewares are applied after the routes). I noticed that there is no way to add a layer to a service except calling Layer::layer or using the ServiceBuilder, both of which are top-down, i.e. the middlewares are added before the services. In my project, I'm currently using this helper trait:

pub trait ServiceHelper<Request>: Service<Request> {
    fn layer<L: Layer<Self>>(self, layer: L) -> L::Service
    where
        Self: Sized
    {
        layer.layer(self)
    }
}

impl<T: Service<Request> + ?Sized, Request> ServiceHelper<Request> for T {}

This allows you to apply middleware like this:

some_service.layer(layer_c).layer(layer_b).layer(layer_a)

I see no reason to not include this in tower::ServiceExt, so I'm filing this issue as a feature request.

@GlenDC
Copy link
Contributor

GlenDC commented Mar 28, 2024

I’m not against it, but I cannot think of a situation where I wanted it bottom up. I can see the appeal for it. As a community member I’m not against it, but neither in particular need or desire for it.

@tobz
Copy link
Member

tobz commented Jul 21, 2024

Yeah, it'd be good to hear some rationale on this.

The main thing I can think of is that it lets you avoid needing to reference ServiceBuilder at the callsite of where you're layering... but you're still either importing the helper trait or importing ServiceBuilder. The resulting layering of the middleware is identical either way.

@tobz tobz added A-layer Area: The tower `Layer` trait C-feature-request Category: A feature request, i.e: not implemented / a PR. P-low Low priority I-needs-decision Issues in need of decision. T-middleware Topic: middleware labels Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-layer Area: The tower `Layer` trait C-feature-request Category: A feature request, i.e: not implemented / a PR. I-needs-decision Issues in need of decision. P-low Low priority T-middleware Topic: middleware
Projects
None yet
Development

No branches or pull requests

3 participants