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

stack: Make the router fallible #888

Merged
merged 1 commit into from
Feb 1, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions linkerd/app/inbound/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use linkerd_app_core::{
tls,
transport::{self, listen},
transport_header::TransportHeader,
Addr, Conditional, CANONICAL_DST_HEADER, DST_OVERRIDE_HEADER,
Addr, Conditional, Error, CANONICAL_DST_HEADER, DST_OVERRIDE_HEADER,
};
use std::{convert::TryInto, net::SocketAddr, str::FromStr, sync::Arc};
use tracing::debug;
Expand Down Expand Up @@ -301,7 +301,7 @@ impl From<HttpAccept> for RequestTarget {
impl<A> svc::stack::RecognizeRoute<http::Request<A>> for RequestTarget {
type Key = Target;

fn recognize(&self, req: &http::Request<A>) -> Self::Key {
fn recognize(&self, req: &http::Request<A>) -> Result<Self::Key, Error> {
let dst = req
.headers()
.get(CANONICAL_DST_HEADER)
Expand All @@ -325,7 +325,7 @@ impl<A> svc::stack::RecognizeRoute<http::Request<A>> for RequestTarget {
.or_else(|| http_request_host_addr(req).ok())
.unwrap_or_else(|| self.accept.tcp.target_addr.into());

Target {
Ok(Target {
dst,
target_addr: self.accept.tcp.target_addr,
tls: self.accept.tcp.tls.clone(),
Expand All @@ -335,7 +335,7 @@ impl<A> svc::stack::RecognizeRoute<http::Request<A>> for RequestTarget {
.version()
.try_into()
.expect("HTTP version must be valid"),
}
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions linkerd/app/outbound/src/ingress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ impl TargetPerRequest {
impl<B> svc::stack::RecognizeRoute<http::Request<B>> for TargetPerRequest {
type Key = Target;

fn recognize(&self, req: &http::Request<B>) -> Self::Key {
Target {
fn recognize(&self, req: &http::Request<B>) -> Result<Self::Key, Error> {
Ok(Target {
accept: self.0,
dst: http_request_l5d_override_dst_addr(req).unwrap_or_else(|_| self.0.orig_dst.into()),
}
})
}
}
28 changes: 20 additions & 8 deletions linkerd/stack/src/router.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::{layer, NewService};
use futures::{future, TryFutureExt};
use linkerd_error::Error;
use std::task::{Context, Poll};
use tower::util::{Oneshot, ServiceExt};

/// Determines the router key for each `T`-typed target.
pub trait RecognizeRoute<T> {
type Key: Clone;

fn recognize(&self, t: &T) -> Self::Key;
fn recognize(&self, t: &T) -> Result<Self::Key, Error>;
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -61,29 +63,39 @@ where
K: RecognizeRoute<Req>,
N: NewService<K::Key, Service = S>,
S: tower::Service<Req>,
S::Error: Into<Error>,
{
type Response = S::Response;
type Error = S::Error;
type Future = Oneshot<S, Req>;
type Error = Error;
type Future = future::Either<
future::ErrInto<Oneshot<S, Req>, Error>,
future::Ready<Result<S::Response, Error>>,
>;

fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
#[inline]
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: Req) -> Self::Future {
let key = self.recognize.recognize(&req);
self.inner.new_service(key).oneshot(req)
match self.recognize.recognize(&req) {
Ok(key) => {
let svc = self.inner.new_service(key);
future::Either::Left(svc.oneshot(req).err_into::<Error>())
}
Err(e) => future::Either::Right(future::err(e)),
}
}
}

impl<T, K, F> RecognizeRoute<T> for F
where
K: Clone,
F: Fn(&T) -> K,
F: Fn(&T) -> Result<K, Error>,
{
type Key = K;

fn recognize(&self, t: &T) -> Self::Key {
fn recognize(&self, t: &T) -> Result<Self::Key, Error> {
(self)(t)
}
}