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

Simplify http::boxed stack initialization #805

Merged
merged 2 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,7 @@ dependencies = [
"http",
"http-body",
"linkerd2-error",
"linkerd2-stack",
"pin-project 0.4.22",
"tower",
]
Expand Down
23 changes: 0 additions & 23 deletions linkerd/app/core/src/svc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,6 @@ impl<L> Layers<L> {
self.push(stack::MapResponseLayer::new(map_response))
}

pub fn box_http_request<B>(self) -> Layers<Pair<L, http::boxed::request::Layer<B>>>
where
B: hyper::body::HttpBody + 'static,
{
self.push(http::boxed::request::Layer::default())
}

pub fn box_http_response(self) -> Layers<Pair<L, http::boxed::response::Layer>> {
self.push(http::boxed::response::Layer::new())
}

pub fn push_oneshot(self) -> Layers<Pair<L, stack::OneshotLayer>> {
self.push(stack::OneshotLayer::new())
}
Expand Down Expand Up @@ -248,18 +237,6 @@ impl<S> Stack<S> {
}))
}

// pub fn box_http_request<B>(self) -> Stack<http::boxed::BoxRequest<S, B>>
// where
// B: hyper::body::HttpBody<Data = http::boxed::Data, Error = Error> + 'static,
// S: tower::Service<http::Request<http::boxed::BoxBody>>,
// {
// self.push(http::boxed::request::Layer::new())
// }

pub fn box_http_response(self) -> Stack<http::boxed::BoxResponse<S>> {
self.push(http::boxed::response::Layer::new())
}

pub fn box_new_service<T>(self) -> Stack<stack::BoxNewService<T, S::Service>>
where
S: NewService<T> + Clone + Send + Sync + 'static,
Expand Down
12 changes: 6 additions & 6 deletions linkerd/app/inbound/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl Config {
let target = endpoint
.push_map_target(HttpEndpoint::from)
.push(observe)
.push_on_response(svc::layers().box_http_response())
.push_on_response(http::boxed::BoxResponse::layer())
.check_new_service::<Target, http::Request<_>>();

// Attempts to discover a service profile for each logical target (as
Expand All @@ -245,7 +245,7 @@ impl Config {
let profile = target
.clone()
.check_new_service::<Target, http::Request<http::boxed::BoxBody>>()
.push_on_response(svc::layers().box_http_request())
.push_on_response(http::boxed::BoxRequest::layer())
// The target stack doesn't use the profile resolution, so drop it.
.push_map_target(endpoint::Target::from)
.push(profiles::http::route_request::layer(
Expand All @@ -267,7 +267,7 @@ impl Config {
profiles_client,
AllowProfile(allow_discovery),
))
.push_on_response(svc::layers().box_http_response())
.push_on_response(http::boxed::BoxResponse::layer())
.instrument(|_: &Target| debug_span!("profile"))
// Skip the profile stack if it takes too long to become ready.
.push_when_unready(target.clone(), self.profile_idle_timeout)
Expand All @@ -288,7 +288,7 @@ impl Config {
.push_on_response(
svc::layers()
.push(http::Retain::layer())
.box_http_response(),
.push(http::boxed::BoxResponse::layer()),
)
// Boxing is necessary purely to limit the link-time overhead of
// having enormous types.
Expand Down Expand Up @@ -398,8 +398,8 @@ impl Config {
SpanConverter::server(span_sink, trace_labels())
})))
.push(metrics.stack.layer(stack_labels("http", "server")))
.box_http_request()
.box_http_response(),
.push(http::boxed::BoxRequest::layer())
.push(http::boxed::BoxResponse::layer()),
)
.push(http::NewNormalizeUri::layer())
.push_map_target(|(_, accept): (_, TcpAccept)| accept)
Expand Down
2 changes: 1 addition & 1 deletion linkerd/app/outbound/src/http/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
"host",
CANONICAL_DST_HEADER,
]))
.push_on_response(svc::layers().box_http_response())
.push_on_response(http::boxed::BoxResponse::layer())
.check_new::<Endpoint>()
.instrument(|e: &Endpoint| debug_span!("endpoint", peer.addr = %e.addr))
.into_inner()
Expand Down
6 changes: 3 additions & 3 deletions linkerd/app/outbound/src/http/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
.stack
.layer(stack_labels("http", "balance.endpoint")),
)
.box_http_request(),
.push(http::boxed::BoxRequest::layer()),
)
.check_new_service::<Endpoint, http::Request<_>>()
.push(resolve::layer(resolve, watchdog))
Expand Down Expand Up @@ -120,14 +120,14 @@ where
svc::layers()
// Strips headers that may be set by this proxy.
.push(http::strip_header::request::layer(DST_OVERRIDE_HEADER))
.push(svc::layers().box_http_response()),
.push(http::boxed::BoxResponse::layer()),
)
.instrument(|l: &Logical| debug_span!("logical", dst = %l.addr()))
.check_new_service::<Logical, http::Request<_>>()
.push_switch(
Logical::should_resolve,
svc::stack(endpoint)
.push_on_response(svc::layers().box_http_request())
.push_on_response(http::boxed::BoxRequest::layer())
.push_map_target(Endpoint::from_logical(
ReasonForNoPeerName::NotProvidedByServiceDiscovery,
))
Expand Down
4 changes: 2 additions & 2 deletions linkerd/app/outbound/src/ingress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ where
.check_new_service::<http::Accept, http::Request<_>>()
.push_on_response(
svc::layers()
.box_http_request()
.push(http::boxed::BoxRequest::layer())
// Limits the number of in-flight requests.
.push(svc::ConcurrencyLimit::layer(max_in_flight_requests))
// Eagerly fail requests when the proxy is out of capacity for a
Expand All @@ -119,7 +119,7 @@ where
.push(metrics.stack.layer(stack_labels("http", "server")))
.push_failfast(dispatch_timeout)
.push_spawn_buffer(buffer_capacity)
.box_http_response(),
.push(http::boxed::BoxResponse::layer()),
)
.check_new_service::<http::Accept, http::Request<_>>()
.push(http::NewNormalizeUri::layer())
Expand Down
4 changes: 2 additions & 2 deletions linkerd/app/outbound/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ where
svc::stack(http_router)
.push_on_response(
svc::layers()
.box_http_request()
.push(http::boxed::BoxRequest::layer())
// Limits the number of in-flight requests.
.push(svc::ConcurrencyLimit::layer(max_in_flight_requests))
// Eagerly fail requests when the proxy is out of capacity for a
Expand All @@ -203,7 +203,7 @@ where
.push(metrics.stack.layer(stack_labels("http", "server")))
.push_failfast(dispatch_timeout)
.push_spawn_buffer(buffer_capacity)
.box_http_response(),
.push(http::boxed::BoxResponse::layer()),
)
.push(http::NewNormalizeUri::layer())
.instrument(|l: &http::Logical| debug_span!("http", v = %l.protocol))
Expand Down
7 changes: 3 additions & 4 deletions linkerd/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ pub mod tap;
pub use self::metrics::Metrics;
use futures::{future, FutureExt, TryFutureExt};
pub use linkerd2_app_core::{self as core, metrics, trace};
use linkerd2_app_core::{control::ControlAddr, dns, drain, serve, svc, Error};
use linkerd2_app_core::{control::ControlAddr, dns, drain, proxy::http, serve, svc, Error};
use linkerd2_app_gateway as gateway;
use linkerd2_app_inbound as inbound;
use linkerd2_app_outbound as outbound;
use std::net::SocketAddr;
use std::pin::Pin;
use std::{net::SocketAddr, pin::Pin};
use tokio::time::Duration;
use tracing::{debug, error, info, info_span};
use tracing_futures::Instrument;
Expand Down Expand Up @@ -218,7 +217,7 @@ impl Config {
inbound_addr,
local_identity,
svc::stack(http_gateway)
.push_on_response(svc::layers().box_http_request())
.push_on_response(http::boxed::BoxRequest::layer())
.into_inner(),
dst.profiles,
tap_layer,
Expand Down
3 changes: 2 additions & 1 deletion linkerd/http-box/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ futures = "0.3"
http = "0.2"
http-body = "0.4"
linkerd2-error = { path = "../error" }
linkerd2-stack = { path = "../stack" }
tower = {version = "0.4", default-features = false }
pin-project = "0.4"
pin-project = "0.4"
4 changes: 2 additions & 2 deletions linkerd/http-box/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![deny(warnings, rust_2018_idioms)]

mod body;
pub mod request;
pub mod response;
mod request;
mod response;

pub use self::{
body::{BoxBody, Data},
Expand Down
41 changes: 9 additions & 32 deletions linkerd/http-box/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,18 @@

use crate::BoxBody;
use linkerd2_error::Error;
use std::task::{Context, Poll};
use linkerd2_stack::layer;
use std::{
marker::PhantomData,
task::{Context, Poll},
};

#[derive(Debug)]
pub struct Layer<B>(std::marker::PhantomData<fn(B)>);
pub struct BoxRequest<S, B>(S, PhantomData<fn(B)>);

#[derive(Debug)]
pub struct BoxRequest<S, B>(S, std::marker::PhantomData<fn(B)>);

impl<B> Default for Layer<B>
where
B: http_body::Body + 'static,
{
fn default() -> Self {
Layer(std::marker::PhantomData)
}
}

impl<B> Clone for Layer<B> {
fn clone(&self) -> Self {
Layer(self.0)
}
}

impl<S, B> tower::layer::Layer<S> for Layer<B>
where
B: http_body::Body + 'static,
B::Data: Send + 'static,
B::Error: Into<Error>,
S: tower::Service<http::Request<BoxBody>>,
BoxRequest<S, B>: tower::Service<http::Request<B>>,
{
type Service = BoxRequest<S, B>;

fn layer(&self, inner: S) -> Self::Service {
BoxRequest(inner, self.0)
impl<S, B> BoxRequest<S, B> {
pub fn layer() -> impl layer::Layer<S, Service = Self> + Clone + Copy {
layer::mk(|inner| BoxRequest(inner, PhantomData))
}
}

Expand Down
18 changes: 4 additions & 14 deletions linkerd/http-box/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@
use crate::BoxBody;
use futures::{future, TryFutureExt};
use linkerd2_error::Error;
use linkerd2_stack::layer;
use std::task::{Context, Poll};

#[derive(Copy, Clone, Debug, Default)]
pub struct Layer(());

#[derive(Clone, Debug)]
pub struct BoxResponse<S>(S);

impl Layer {
pub fn new() -> Self {
Layer(())
}
}

impl<S> tower::layer::Layer<S> for Layer {
type Service = BoxResponse<S>;

fn layer(&self, inner: S) -> Self::Service {
BoxResponse(inner)
impl<S> BoxResponse<S> {
pub fn layer() -> impl layer::Layer<S, Service = Self> + Clone + Copy {
layer::mk(Self)
}
}

Expand Down