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: Add NewService::into_make_service #618

Merged
merged 1 commit into from
Aug 5, 2020
Merged
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
30 changes: 30 additions & 0 deletions linkerd/stack/src/new_service.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
use crate::FutureService;
use futures::future;
use linkerd2_error::Never;
use std::task::{Context, Poll};
use tower::util::{Oneshot, ServiceExt};

/// Immediately and infalliby creates (usually) a Service.
pub trait NewService<T> {
type Service;

fn new_service(&self, target: T) -> Self::Service;

fn into_make_service(self) -> IntoMakeService<Self>
where
Self: Sized,
{
IntoMakeService { new_service: self }
}
}

/// A Layer that modifies inner `MakeService`s to be exposd as a `NewService`.
Expand All @@ -18,6 +28,12 @@ pub struct FromMakeService<S> {
make_service: S,
}

/// Modifies inner `MakeService`s to be exposd as a `NewService`.
#[derive(Clone, Copy, Debug)]
pub struct IntoMakeService<N> {
new_service: N,
}

// === impl NewService ===

impl<F, T, S> NewService<T> for F
Expand Down Expand Up @@ -53,3 +69,17 @@ where
FutureService::new(self.make_service.clone().oneshot(target))
}
}

impl<T, N: NewService<T>> tower::Service<T> for IntoMakeService<N> {
type Response = N::Service;
type Error = Never;
type Future = future::Ready<Result<N::Service, Never>>;

fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Never>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, target: T) -> Self::Future {
future::ok(self.new_service.new_service(target))
}
}