[−][src]Crate tower
async fn(Request) -> Result<Response, Error>
Overview
Tower is a library of modular and reusable components for building robust networking clients and servers.
Tower provides a simple core abstraction, the Service trait, which
represents an asynchronous function taking a request and returning either a
response or an error. This abstraction can be used to model both clients and
servers.
Generic components, like timeouts, rate limiting, and load balancing,
can be modeled as Services that wrap some inner service and apply
additional behavior before or after the inner service is called. This allows
implementing these components in a protocol-agnostic, composable way. Typically,
such services are referred to as middleware.
An additional abstraction, the Layer trait, is used to compose
middleware with Services. If a Service can be thought of as an
asynchronous function from a request type to a response type, a Layer is
a function taking a Service of one type and returning a Service of a
different type. The ServiceBuilder type is used to add middleware to a
service by composing it with multiple multiple Layers.
The Tower Ecosystem
Tower is made up of the following crates:
tower(this crate)tower-servicetower-layertower-test
Since the Service and Layer traits are important integration points
for all libraries using Tower, they are kept as stable as possible, and
breaking changes are made rarely. Therefore, they are defined in separate
crates, tower-service and tower-layer. This crate contains
re-exports of those core traits, implementations of commonly-used
middleware, and utilities for working with Services and Layers.
Finally, the tower-test crate provides tools for testing programs using
Tower.
Usage
The various middleware implementations provided by this crate are feature flagged, so that users can only compile the parts of Tower they need. By default, all the optional middleware are disabled.
To get started using all of Tower's optional middleware, add this to your
Cargo.toml:
tower = { version = "0.4", features = ["full"] }
Alternatively, you can only enable some features. For example, to enable
only the retry and timeout middleware, write:
tower = { version = "0.4", features = ["retry", "timeout"] }
See here for a complete list of all middleware provided by Tower.
Modules
| balance | balanceMiddleware that allows balancing load among multiple services. |
| buffer | bufferMiddleware that provides a buffered mpsc channel to a service. |
| builder | Builder types to compose layers and services |
| discover | discoverService discovery |
| filter | filterConditionally dispatch requests to the inner service based on the result of a predicate. |
| hedge | hedgePre-emptively retry requests which have been outstanding for longer than a given latency percentile. |
| layer | A collection of |
| limit | limitTower middleware for limiting requests. |
| load | loadService load measurement |
| load_shed | load-shedMiddleware for shedding load when inner services aren't ready. |
| make | makeTrait aliases for Services that produce specific types of Responses. |
| ready_cache | ready-cacheA cache of services |
| reconnect | reconnectReconnect services when they fail. |
| retry | retryMiddleware for retrying "failed" requests. |
| spawn_ready | spawn-readyWhen an underlying service is not ready, drive it to readiness on a background task. |
| steer | steerThis module provides functionality to aid managing routing requests between |
| timeout | timeoutMiddleware that applies a timeout to requests. |
| util | utilVarious utility types and functions that are generally with Tower. |
Structs
| ServiceBuilder | Declaratively construct |
Traits
| Layer | Decorates a |
| MakeService | Creates new |
| Service | An asynchronous function from a |
| ServiceExt | An extension trait for |
Functions
| service_fn | Returns a new |
Type Definitions
| BoxError | Alias for a type-erased error type. |