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

Add AlbHealthCheckLayer #2540

Merged
merged 44 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
8374c38
Add Amazon IP header
jjant Apr 4, 2023
b746d6c
Make types that need to be public public
jjant Apr 4, 2023
5153fef
Use `CheckHealthLayer` in example
jjant Apr 4, 2023
ceeb99b
Simplify `CheckHealthService`
jjant Apr 4, 2023
7fb5c48
Add `CheckHealthLayer::with_default_handler`
jjant Apr 4, 2023
bdbed1b
Defer `service.clone()` to when needed and use `oneshot()`
jjant Apr 12, 2023
d830a0f
Always return `Poll::Ready` in the middleware
jjant Apr 12, 2023
ffacb4c
Make health check uri configurable
jjant Apr 12, 2023
ef888e5
Implement `CheckHealthPlugin`
jjant Apr 12, 2023
764f389
Add `Debug` impls
jjant Apr 12, 2023
7fbd3da
Use extension trait method
jjant Apr 13, 2023
687c897
Add `PluginPipeline::http_layer` method
jjant Apr 13, 2023
3983a0d
Use `http_layer` method on pokemon service example
jjant Apr 13, 2023
b27db9b
Remove `CheckHealthPlugin` and `CheckHealthExt`
jjant Apr 13, 2023
8e2945a
Test that the pokemon service responds to health check requests
jjant Apr 13, 2023
e513685
Remove default ping handler
jjant Apr 13, 2023
8030949
Make check health handler infallible
jjant Apr 13, 2023
7e55d0c
Undo breaking change in `HttpLayer`
jjant Apr 13, 2023
8f6ec3d
Simplify `MappedHandlerFuture`
jjant Apr 13, 2023
e4d22cf
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 13, 2023
d496eb7
Fix docs
jjant Apr 13, 2023
6772f5a
Use `async` block instead of explicit `std::future::ready` call
jjant Apr 14, 2023
6735a32
Newtype future and add module docs
jjant Apr 17, 2023
4e442ca
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 17, 2023
ae48b9d
Fix clippy complaints
jjant Apr 17, 2023
9fd2088
Inline `MappedHandlerFuture` into `CheckHealthFuture`
jjant Apr 17, 2023
bc385f2
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 17, 2023
5bed399
Add module level example for `check_health`
jjant Apr 18, 2023
32007cd
Fix minor grammar mistake in comment
jjant Apr 20, 2023
887fbdc
Don't run example
jjant Apr 20, 2023
7fad221
Allow using non-static `&str`s for health check URIs
jjant Apr 20, 2023
3d15ad8
Use healthcheck-based naming instead of "ping"-based
jjant Apr 20, 2023
9bd96ce
Use "health check" instead of "check health" everywhere
jjant Apr 20, 2023
05af5e7
Prefix public items with `Alb`
jjant Apr 20, 2023
67493f2
Add changelog entry
jjant Apr 20, 2023
b822c53
Mention `PluginPipeline::http_layer` in changelog
jjant Apr 20, 2023
a2719d4
Document `AlbHealthCheckLayer::new`
jjant Apr 20, 2023
8c04213
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 20, 2023
f22e012
Make layer take in a `Service`
jjant Apr 20, 2023
2f4d3bd
Use `Cow<'a, str>` for uri
jjant Apr 21, 2023
a961ecb
Rename `new_fn` -> `from_handler`
jjant Apr 21, 2023
d56d5cd
Use `'static` for `Cow` lifetime
jjant Apr 21, 2023
3e98163
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 21, 2023
8a4eede
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 21, 2023
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
3 changes: 3 additions & 0 deletions examples/pokemon-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ async-stream = "0.3"
rand = "0.8.5"
serial_test = "1.0.0"

# We use hyper client on tests
hyper = {version = "0.14.25", features = ["server", "client"] }

# This dependency is only required for testing the `pokemon-service-tls` program.
hyper-rustls = { version = "0.23.2", features = ["http2"] }

Expand Down
21 changes: 18 additions & 3 deletions examples/pokemon-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ mod plugin;
use std::{net::SocketAddr, sync::Arc};

use aws_smithy_http_server::{
extension::OperationExtensionExt, instrumentation::InstrumentExt, plugin::PluginPipeline,
request::request_id::ServerRequestIdProviderLayer, AddExtensionLayer,
body,
extension::OperationExtensionExt,
instrumentation::InstrumentExt,
plugin::{check_health::CheckHealthLayer, PluginPipeline},
request::request_id::ServerRequestIdProviderLayer,
AddExtensionLayer,
};
use clap::Parser;

use hyper::{Body, Response, StatusCode};
use plugin::PrintExt;
use pokemon_service::{
do_nothing_but_log_request_ids, get_storage_with_local_approved, DEFAULT_ADDRESS, DEFAULT_PORT,
Expand Down Expand Up @@ -46,7 +51,17 @@ pub async fn main() {
// `Response::extensions`, or infer routing failure when it's missing.
.insert_operation_extension()
// Adds `tracing` spans and events to the request lifecycle.
.instrument();
.instrument()
// Handle `/ping` health check requests.
.http_layer(CheckHealthLayer::new("/ping", |_req| {
let response = Response::builder()
.status(StatusCode::OK)
.body(body::boxed(Body::empty()))
.expect("Couldn't construct response");

std::future::ready(response)
}));

let app = PokemonService::builder_with_plugins(plugins)
// Build a registry containing implementations to all the operations in the service. These
// are async functions or async closures that take as input the operation's input and
Expand Down
7 changes: 5 additions & 2 deletions examples/pokemon-service/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ pub async fn run_server() -> ChildDrop {
ChildDrop(child)
}

pub fn base_url() -> String {
format!("http://{DEFAULT_ADDRESS}:{DEFAULT_PORT}")
}

pub fn client() -> Client<DynConnector, DynMiddleware<DynConnector>> {
let base_url = format!("http://{DEFAULT_ADDRESS}:{DEFAULT_PORT}");
let raw_client = Builder::new()
.rustls_connector(Default::default())
.middleware_fn(rewrite_base_url(base_url))
.middleware_fn(rewrite_base_url(base_url()))
.build_dyn();
let config = Config::builder().build();
Client::with_config(raw_client, config)
Expand Down
7 changes: 7 additions & 0 deletions examples/pokemon-service/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ async fn simple_integration_test() {

let service_statistics_out = client.get_server_statistics().send().await.unwrap();
assert_eq!(2, service_statistics_out.calls_count.unwrap());

let hyper_client = hyper::Client::new();
let health_check_url = format!("{}/ping", common::base_url());
let health_check_url = hyper::Uri::try_from(health_check_url).unwrap();
let result = hyper_client.get(health_check_url).await.unwrap();

assert_eq!(result.status(), 200);
}
115 changes: 115 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/check_health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

use std::future::Ready;
use std::marker::PhantomData;
use std::task::{Context, Poll};

use futures_util::Future;
use hyper::{Body, Request, Response};
use pin_project_lite::pin_project;
use tower::{util::Oneshot, Layer, Service, ServiceExt};

use crate::body::BoxBody;

use super::Either;

/// A [`tower::Layer`] used to apply [`CheckHealthService`].
#[derive(Clone, Debug)]
pub struct CheckHealthLayer<PingHandler> {
health_check_uri: &'static str,
ping_handler: PingHandler,
}

impl CheckHealthLayer<()> {
pub fn new<HandlerFuture: Future<Output = Response<BoxBody>>, H: Fn(Request<Body>) -> HandlerFuture>(
health_check_uri: &'static str,
ping_handler: H,
) -> CheckHealthLayer<H> {
CheckHealthLayer {
health_check_uri,
ping_handler,
}
}
}

pub type DefaultHandler = fn(Request<Body>) -> Ready<Response<BoxBody>>;

impl<S, H: Clone> Layer<S> for CheckHealthLayer<H> {
type Service = CheckHealthService<H, S>;

fn layer(&self, inner: S) -> Self::Service {
CheckHealthService {
inner,
layer: self.clone(),
}
}
}

/// A middleware [`Service`] responsible for handling health check requests.
#[derive(Clone, Debug)]
pub struct CheckHealthService<H, S> {
inner: S,
layer: CheckHealthLayer<H>,
}

pin_project! {
/// A future that converts `F` into a compatible `S::Future`.
pub struct MappedHandlerFuture<R, S, F> {
#[pin]
inner: F,
pd: PhantomData<fn(R) -> S>,
}
}

impl<R, S, F> MappedHandlerFuture<R, S, F> {
fn new(inner: F) -> MappedHandlerFuture<R, S, F> {
Self { inner, pd: PhantomData }
}
}

impl<R, S: Service<R>, F: Future<Output = S::Response>> Future for MappedHandlerFuture<R, S, F> {
type Output = Result<S::Response, S::Error>;

fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().inner.poll(cx).map(Ok)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a simpler way to do this? I basically just want to map HandlerFuture from a Future<Output = Response<BoxBody>> to a Future<Output = Result<Response<BoxBody>, S::Error>>.

I tried using futures_util::future::Map but I ran into a compiler error when trying to write the type in line 92 as (expected function pointer but got function item):

type Future = Either<
  Map<HandlerFuture, fn(Response<BoxBody) -> Result<Response<BoxBody>, S::Error>>,
  Oneshot<S, Request<Body>>
>;


impl<H, HandlerFuture, S> Service<Request<Body>> for CheckHealthService<H, S>
where
S: Service<Request<Body>, Response = Response<BoxBody>> + Clone,
S::Future: std::marker::Send + 'static,
HandlerFuture: Future<Output = Response<BoxBody>>,
H: Fn(Request<Body>) -> HandlerFuture,
{
type Response = S::Response;

type Error = S::Error;

type Future = Either<MappedHandlerFuture<Request<Body>, S, HandlerFuture>, Oneshot<S, Request<Body>>>;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
// The check that the service is ready is done by `Oneshot` below.
Poll::Ready(Ok(()))
}

fn call(&mut self, req: Request<Body>) -> Self::Future {
if req.uri() == self.layer.health_check_uri {
let handler_future = (self.layer.ping_handler)(req);

Either::Left {
value: MappedHandlerFuture::new(handler_future),
}
} else {
let clone = self.inner.clone();
let service = std::mem::replace(&mut self.inner, clone);

Either::Right {
value: service.oneshot(req),
}
}
}
}
1 change: 1 addition & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
//! ```
//!

pub mod check_health;
mod closure;
mod either;
mod filter;
Expand Down
7 changes: 7 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use crate::operation::Operation;
use crate::plugin::{IdentityPlugin, Plugin, PluginStack};

use super::HttpLayer;

/// A wrapper struct for composing [`Plugin`]s.
/// It is used as input for the `builder_with_plugins` method on the generate service struct
/// (e.g. `PokemonService::builder_with_plugins`).
Expand Down Expand Up @@ -168,6 +170,11 @@ impl<P> PluginPipeline<P> {
pub fn push<NewPlugin>(self, new_plugin: NewPlugin) -> PluginPipeline<PluginStack<NewPlugin, P>> {
PluginPipeline(PluginStack::new(new_plugin, self.0))
}

/// Applies a single [`Layer`] to all operations _before_ they are deserialized.
pub fn http_layer<L>(self, layer: L) -> PluginPipeline<PluginStack<HttpLayer<L>, P>> {
PluginPipeline(PluginStack::new(HttpLayer(layer), self.0))
}
}

impl<P, Op, S, L, InnerPlugin> Plugin<P, Op, S, L> for PluginPipeline<InnerPlugin>
Expand Down