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 5 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
5 changes: 3 additions & 2 deletions examples/pokemon-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mod plugin;
use std::{net::SocketAddr, sync::Arc};

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

Expand Down Expand Up @@ -63,6 +63,7 @@ pub async fn main() {
let app = app
// Setup shared state and middlewares.
.layer(&AddExtensionLayer::new(Arc::new(State::default())))
.layer(&CheckHealthLayer::with_default_handler())
// Add request IDs
.layer(&ServerRequestIdProviderLayer::new());

Expand Down
96 changes: 96 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,96 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

use std::future::Ready;

use futures_util::Future;
use http::StatusCode;
use hyper::{Body, Request, Response};
use tower::Layer;
use tower::Service;

use crate::body;
use crate::body::BoxBody;

use super::Either;

#[derive(Clone)]
pub struct CheckHealthLayer<H> {
ping_handler: H,
}

impl<H> CheckHealthLayer<H> {
pub fn new(ping_handler: H) -> Self {
CheckHealthLayer { ping_handler }
}
}

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

impl CheckHealthLayer<()> {
pub fn with_default_handler<E>() -> CheckHealthLayer<DefaultHandler<E>> {
CheckHealthLayer::new(default_ping_handler)
}
}

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(),
}
}
}

#[derive(Clone)]
pub struct CheckHealthService<H, S> {
inner: S,
layer: CheckHealthLayer<H>,
}

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 = Result<Response<BoxBody>, S::Error>>,
jjant marked this conversation as resolved.
Show resolved Hide resolved
H: Fn(Request<Body>) -> HandlerFuture,
jjant marked this conversation as resolved.
Show resolved Hide resolved
{
type Response = S::Response;

type Error = S::Error;

type Future = Either<HandlerFuture, S::Future>;
jjant marked this conversation as resolved.
Show resolved Hide resolved

fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
jjant marked this conversation as resolved.
Show resolved Hide resolved
}

fn call(&mut self, req: Request<Body>) -> Self::Future {
let clone = self.inner.clone();
jjant marked this conversation as resolved.
Show resolved Hide resolved
let mut service = std::mem::replace(&mut self.inner, clone);

if req.uri() == "/ping" {
Either::Left {
value: (self.layer.ping_handler)(req),
}
} else {
Either::Right {
value: service.call(req),
}
}
}
}

/// A handler that returns `200 OK` with an empty body.
fn default_ping_handler<E>(_req: Request<Body>) -> Ready<Result<Response<BoxBody>, E>> {
let response = Response::builder()
.status(StatusCode::OK)
.body(body::boxed(Body::empty()))
.expect("Couldn't construct response");

std::future::ready(Ok::<_, E>(response))
}
2 changes: 2 additions & 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 @@
//! ```
//!

mod check_health;
mod closure;
mod either;
mod filter;
Expand All @@ -128,6 +129,7 @@ mod stack;

use crate::operation::Operation;

pub use check_health::CheckHealthLayer;
pub use closure::{plugin_from_operation_name_fn, OperationNameFn};
pub use either::Either;
pub use filter::{filter_by_operation_name, FilterByOperationName};
Expand Down