Module tower::filter

source ·
Expand description

Conditionally dispatch requests to the inner service based on the result of a predicate.

A predicate takes some request type and returns a Result<Request, Error>. If the predicate returns Ok, the inner service is called with the request returned by the predicate — which may be the original request or a modified one. If the predicate returns Err, the request is rejected and the inner service is not called.

Predicates may either be synchronous (simple functions from a Request to a Result) or asynchronous (functions returning Futures). Separate traits, Predicate and AsyncPredicate, represent these two types of predicate. Note that when it is not necessary to await some other asynchronous operation in the predicate, the synchronous predicate should be preferred, as it introduces less overhead.

The predicate traits are implemented for closures and function pointers. However, users may also implement them for other types, such as when the predicate requires some state carried between requests. For example, Predicate could be implemented for a type that rejects a fixed set of requests by checking if they are contained by a a HashSet or other collection.