Skip to content

Commit 0f2cf38

Browse files
authored
refactor(app/gateway): extract spawn_routes() function (#4125)
this is a follow-on to #4119. see #4119 (comment) for further background on this: > This is reaching unreadable levels of complexity imo. > > i'd look to trying to golf this down so that we don't have so many > wrapping lines, or otherwise extract this into a separate function so > it is, e.g. `push_filter(spawn_routes)` this commit performs that transformation, outlining our `push_filter(|| {})` into a standalone function rather than an inline closure. Signed-off-by: katelyn martin <[email protected]>
1 parent e657373 commit 0f2cf38

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

linkerd/app/gateway/src/http.rs

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -81,57 +81,34 @@ impl Gateway {
8181
R: Resolve<ConcreteAddr, Endpoint = Metadata, Error = Error>,
8282
R::Resolution: Unpin,
8383
{
84-
let http =
85-
self.outbound
86-
.clone()
87-
.with_stack(inner)
88-
.push_http_cached(resolve)
89-
.into_stack()
90-
// Discard `T` and its associated client-specific metadata.
91-
.push_map_target(Target::discard_parent)
92-
.push(svc::ArcNewService::layer())
93-
// Add headers to prevent loops.
94-
.push(NewHttpGateway::layer(
95-
self.inbound.identity().local_id().clone(),
96-
))
97-
.push_on_service(svc::LoadShed::layer())
98-
.lift_new()
99-
.push(svc::ArcNewService::layer())
100-
// After protocol-downgrade, we need to build an inner stack for
101-
// each request-level HTTP version.
102-
.push(svc::NewOneshotRoute::layer_via(|t: &Target<T>| {
103-
ByRequestVersion(t.clone())
104-
}))
105-
// Only permit gateway traffic to endpoints for which we have
106-
// discovery information.
107-
.push_filter(
108-
|Permitted {
109-
permit: _,
110-
target: parent,
111-
}: Permitted<T>|
112-
-> Result<_, GatewayDomainInvalid> {
113-
let routes = {
114-
let mut profile = svc::Param::<
115-
Option<watch::Receiver<profiles::Profile>>,
116-
>::param(&parent)
117-
.ok_or(GatewayDomainInvalid)?;
118-
let init = mk_routes(&profile.borrow_and_update())
119-
.ok_or(GatewayDomainInvalid)?;
120-
outbound::http::spawn_routes(profile, init, mk_routes)
121-
};
122-
123-
Ok(Target {
124-
routes,
125-
addr: parent.param(),
126-
version: parent.param(),
127-
parent,
128-
})
129-
},
130-
)
131-
.push(svc::ArcNewService::layer())
132-
// Authorize requests to the gateway.
133-
.push(self.inbound.authorize_http())
134-
.arc_new_clone_http();
84+
let http = self
85+
.outbound
86+
.clone()
87+
.with_stack(inner)
88+
.push_http_cached(resolve)
89+
.into_stack()
90+
// Discard `T` and its associated client-specific metadata.
91+
.push_map_target(Target::discard_parent)
92+
.push(svc::ArcNewService::layer())
93+
// Add headers to prevent loops.
94+
.push(NewHttpGateway::layer(
95+
self.inbound.identity().local_id().clone(),
96+
))
97+
.push_on_service(svc::LoadShed::layer())
98+
.lift_new()
99+
.push(svc::ArcNewService::layer())
100+
// After protocol-downgrade, we need to build an inner stack for
101+
// each request-level HTTP version.
102+
.push(svc::NewOneshotRoute::layer_via(|t: &Target<T>| {
103+
ByRequestVersion(t.clone())
104+
}))
105+
// Only permit gateway traffic to endpoints for which we have
106+
// discovery information.
107+
.push_filter(spawn_routes)
108+
.push(svc::ArcNewService::layer())
109+
// Authorize requests to the gateway.
110+
.push(self.inbound.authorize_http())
111+
.arc_new_clone_http();
135112

136113
self.inbound
137114
.clone()
@@ -146,6 +123,33 @@ impl Gateway {
146123
}
147124
}
148125

126+
fn spawn_routes<T>(
127+
Permitted {
128+
permit: _,
129+
target: parent,
130+
}: Permitted<T>,
131+
) -> Result<Target<T>, GatewayDomainInvalid>
132+
where
133+
T: Clone
134+
+ svc::Param<GatewayAddr>
135+
+ svc::Param<http::Variant>
136+
+ svc::Param<Option<watch::Receiver<profiles::Profile>>>,
137+
{
138+
let routes = {
139+
let mut profile = svc::Param::<Option<watch::Receiver<profiles::Profile>>>::param(&parent)
140+
.ok_or(GatewayDomainInvalid)?;
141+
let init = mk_routes(&profile.borrow_and_update()).ok_or(GatewayDomainInvalid)?;
142+
outbound::http::spawn_routes(profile, init, mk_routes)
143+
};
144+
145+
Ok(Target {
146+
routes,
147+
addr: parent.param(),
148+
version: parent.param(),
149+
parent,
150+
})
151+
}
152+
149153
fn mk_routes(profile: &profiles::Profile) -> Option<outbound::http::Routes> {
150154
if let Some(addr) = profile.addr.clone() {
151155
return Some(outbound::http::Routes::Profile(

0 commit comments

Comments
 (0)