Module tower
Modular middleware framework for async request/response services.
tower defines the Service and Layer traits
that form the middleware model for axum, hyper, and the broader tokio ecosystem.
A Service is an async function from request to response;
a Layer wraps a service to add behavior like timeouts, rate limiting, retries, etc.
Built-in middleware:
timeout- fails requests exceeding a durationlimit- rate limiting and concurrency limitingretry- retries failed requests per a policybuffer- adds a cloneable mpsc queue in front of a serviceload_shed- rejects requests immediately when the inner service is not ready
In practice, most axum users encounter tower through
axum::Router::layer and ServiceBuilder
when adding middleware to routes.
Examples
Building a service with stacked middleware using ServiceBuilder:
use ;
use TimeoutLayer;
use RateLimitLayer;
use Duration;
use Infallible;
// A simple async service function
async
async
Creating a service from an async function with service_fn:
use ;
use Infallible;
async
async
Modules
- balance Middleware that allows balancing load among multiple services.
- buffer Middleware that provides a buffered mpsc channel to a service.
- builder Builder types to compose layers and services
- discover Service discovery
- filter Conditionally dispatch requests to the inner service based on the result of a predicate.
- hedge Pre-emptively retry requests which have been outstanding for longer than a given latency percentile.
-
layer
A collection of
Layerbased tower services - limit Tower middleware for limiting requests.
- load Service load measurement
- load_shed Middleware for shedding load when inner services aren't ready.
- make Trait aliases for Services that produce specific types of Responses.
- ready_cache A cache of services
- reconnect Reconnect services when they fail.
- retry Middleware for retrying "failed" requests.
- spawn_ready When an underlying service is not ready, drive it to readiness on a background task.
-
steer
This module provides functionality to aid managing routing requests between
Services. - timeout Middleware that applies a timeout to requests.
- util Various utility types and functions that are generally used with Tower.
Type Aliases
- BoxError Alias for a type-erased error type.