Skip to main content

BedrockAgentCoreApp

Struct BedrockAgentCoreApp 

Source
pub struct BedrockAgentCoreApp { /* private fields */ }
Expand description

An HTTP server that implements the AgentCore Runtime contract.

See the module documentation for the request handling rules.

Implementations§

Source§

impl BedrockAgentCoreApp

Source

pub fn builder() -> Builder

Starts the configuration of an app.

Source

pub async fn run(self) -> Result<(), Error>

Binds the configured address and serves until the process receives SIGTERM or Ctrl-C.

§Threads

The server reads requests and answers GET /ping on a thread of its own. The invocation handler, the stream of its reply, the routes given to Builder::routes and the warmup handlers run on the tokio runtime that calls this method, as tasks of that runtime. A handler that blocks its thread therefore does not stop the health check, as in the Python SDK. It still delays the other work of that runtime thread, so blocking work belongs in tokio::task::spawn_blocking. A layer given to Builder::layer runs on the server thread and must not block.

The same holds for the serve methods. A router from router that you serve yourself runs all of its code in the task of the connection.

§Shutdown

On the signal the server stops accepting connections. It logs one warning for each invocation in flight, with its request ID and session ID, and one line with the tracked background tasks. Requests in flight, streams and tracked background tasks (crate::tasks) then get the grace period to finish (Builder::shutdown_grace, 8 seconds by default). The grace period starts at the signal, not at startup.

When everything finishes in time, this method returns Ok. Otherwise, at the end of the grace period the server cancels the requests that are still in flight, on all routes. A handler that is still running is dropped and its client gets a 503 reply. A response body that is still being sent fails, and its stream is dropped. The server then waits up to half a second for its connections to close and returns ErrorKind::ShutdownTimeout.

A caller behind AgentCore Runtime does not see a reply that the agent cuts. In tests against the service it received such a reply as a complete one.

Cancellation takes effect when the handler or the stream is next polled. Two kinds of work can therefore outlast the return of this method, until they end or the process exits:

  • A handler, a stream or a destructor that blocks its thread without yielding.
  • A stream whose client has stopped reading. The server does not poll the stream while it cannot write, so the stream is dropped only when the connection closes.

The server also cannot stop work that it does not own: tasks that your code spawned, including tracked background tasks, and blocking work that has started.

§Errors

Returns ErrorKind::Bind when the address cannot be bound. Returns ErrorKind::Serve when the signal handlers cannot be installed or the server stops with an I/O error. Returns ErrorKind::ShutdownTimeout as described above.

Source

pub async fn serve(self, listener: TcpListener) -> Result<(), Error>

Serves on a listener that the caller has bound, until the process receives SIGTERM or Ctrl-C. Shutdown works as described for run.

Use it to listen on an ephemeral port: bind port 0 and read the address from the listener before calling this method.

§Errors

Returns ErrorKind::Serve when the signal handlers cannot be installed or the server stops with an I/O error, and ErrorKind::ShutdownTimeout as described for run.

Source

pub async fn serve_with_shutdown( self, listener: TcpListener, shutdown: impl Future<Output = ()> + Send + 'static, ) -> Result<(), Error>

Serves on a listener that the caller has bound, until shutdown completes. Shutdown then works as described for run. This method installs no signal handlers.

§Errors

Returns ErrorKind::Serve when the server stops with an I/O error, and ErrorKind::ShutdownTimeout as described for run.

Source

pub fn router(&self) -> Router

The routes of the app as an axum router: POST /invocations, GET /ping, the warmup hook and the routes given to Builder::routes. Use it to mount the app in a server that you run yourself, for example with Router::merge, or to wrap it in middleware with Router::layer. A layer that you add wraps the app’s routes, so it sees each request before the app does.

The layers given to Builder::layer are part of this router. They wrap what the router holds: its routes and a fallback given through Builder::routes. They do not wrap a route or a fallback that you add to the router afterwards, or the fallback of a router that you merge it into.

§Fallback

The JSON 404 that the app sends for unknown paths when it serves itself is not part of this router. A fallback that you gave through Builder::routes is part of it.

axum cannot merge two routers that both have a fallback: Router::merge panics. So combine the routers in one of these ways:

  • Give the app no fallback, and set one on the router that you merge it into.
  • Give the app the fallback through Builder::routes, and merge its router into a router without one. The app’s fallback then answers every unknown path of the combined router.
§Serving

Shutdown and the listen address are yours to handle when you serve this router yourself: the settings Builder::bind and Builder::shutdown_grace apply only to run and the serve methods.

Source

pub fn tasks(&self) -> &AsyncTasks

The registry of background work of this app. Every invocation’s RequestContext::tasks returns the same registry.

Trait Implementations§

Source§

impl Debug for BedrockAgentCoreApp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for BedrockAgentCoreApp

§

impl !UnwindSafe for BedrockAgentCoreApp

§

impl Forget for BedrockAgentCoreApp

§

impl Freeze for BedrockAgentCoreApp
where *const ArcInner<dyn Fn(Bytes, RequestContext) -> Pin<Box<dyn Future<Output = Result<Response, HandlerError>> + Send>> + Send + Sync> is !null: Freeze, PhantomData<ArcInner<dyn Fn(Bytes, RequestContext) -> Pin<Box<dyn Future<Output = Result<Response, HandlerError>> + Send>> + Send + Sync>>: Freeze, *const ArcInner<dyn Fn() -> Option<PingStatus> + Send + Sync> is !null: Freeze, PhantomData<ArcInner<dyn Fn() -> Option<PingStatus> + Send + Sync>>: Freeze, *const ArcInner<Inner> is !null: Freeze, PhantomData<ArcInner<Inner>>: Freeze, *const ArcInner<Mutex<Last>> is !null: Freeze, PhantomData<ArcInner<Mutex<Last>>>: Freeze, *const ArcInner<Atomic<bool>> is !null: Freeze, PhantomData<ArcInner<Atomic<bool>>>: Freeze, *const ArcInner<Mutex<Vec<(String, String)>>> is !null: Freeze, PhantomData<ArcInner<Mutex<Vec<(String, String)>>>>: Freeze, *const ArcInner<Mutex<Flights>> is !null: Freeze, PhantomData<ArcInner<Mutex<Flights>>>: Freeze, *const ArcInner<OnceLock<Handle>> is !null: Freeze, PhantomData<ArcInner<OnceLock<Handle>>>: Freeze, *const ArcInner<RouterInner<()>> is !null: Freeze, PhantomData<ArcInner<RouterInner<()>>>: Freeze,

§

impl Move for BedrockAgentCoreApp

§

impl Send for BedrockAgentCoreApp
where Arc<dyn Fn(Bytes, RequestContext) -> Pin<Box<dyn Future<Output = Result<Response, HandlerError>> + Send>> + Send + Sync>: Send, Arc<dyn Fn() -> Option<PingStatus> + Send + Sync>: Send, Arc<Inner>: Send, Arc<Mutex<Last>>: Send, Arc<Atomic<bool>>: Send, Arc<Mutex<Vec<(String, String)>>>: Send, Arc<Mutex<Flights>>: Send, Arc<OnceLock<Handle>>: Send, Arc<RouterInner<()>>: Send,

§

impl Sync for BedrockAgentCoreApp
where Arc<dyn Fn(Bytes, RequestContext) -> Pin<Box<dyn Future<Output = Result<Response, HandlerError>> + Send>> + Send + Sync>: Sync, Arc<dyn Fn() -> Option<PingStatus> + Send + Sync>: Sync, Arc<Inner>: Sync, Arc<Mutex<Last>>: Sync, Arc<Atomic<bool>>: Sync, Arc<Mutex<Vec<(String, String)>>>: Sync, Arc<Mutex<Flights>>: Sync, Arc<OnceLock<Handle>>: Sync, Arc<RouterInner<()>>: Sync,

§

impl Unpin for BedrockAgentCoreApp
where Arc<dyn Fn(Bytes, RequestContext) -> Pin<Box<dyn Future<Output = Result<Response, HandlerError>> + Send>> + Send + Sync>: Unpin, Arc<dyn Fn() -> Option<PingStatus> + Send + Sync>: Unpin, Arc<Inner>: Unpin, Arc<Mutex<Last>>: Unpin, Arc<Atomic<bool>>: Unpin, Arc<Mutex<Vec<(String, String)>>>: Unpin, Arc<Mutex<Flights>>: Unpin, Arc<OnceLock<Handle>>: Unpin, Arc<RouterInner<()>>: Unpin,

§

impl UnsafeUnpin for BedrockAgentCoreApp
where *const ArcInner<dyn Fn(Bytes, RequestContext) -> Pin<Box<dyn Future<Output = Result<Response, HandlerError>> + Send>> + Send + Sync> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn Fn(Bytes, RequestContext) -> Pin<Box<dyn Future<Output = Result<Response, HandlerError>> + Send>> + Send + Sync>>: UnsafeUnpin, *const ArcInner<dyn Fn() -> Option<PingStatus> + Send + Sync> is !null: UnsafeUnpin, PhantomData<ArcInner<dyn Fn() -> Option<PingStatus> + Send + Sync>>: UnsafeUnpin, *const ArcInner<Inner> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner>>: UnsafeUnpin, *const ArcInner<Mutex<Last>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<Last>>>: UnsafeUnpin, *const ArcInner<Atomic<bool>> is !null: UnsafeUnpin, PhantomData<ArcInner<Atomic<bool>>>: UnsafeUnpin, *const ArcInner<Mutex<Vec<(String, String)>>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<Vec<(String, String)>>>>: UnsafeUnpin, *const ArcInner<Mutex<Flights>> is !null: UnsafeUnpin, PhantomData<ArcInner<Mutex<Flights>>>: UnsafeUnpin, *const ArcInner<OnceLock<Handle>> is !null: UnsafeUnpin, PhantomData<ArcInner<OnceLock<Handle>>>: UnsafeUnpin, *const ArcInner<RouterInner<()>> is !null: UnsafeUnpin, PhantomData<ArcInner<RouterInner<()>>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more