-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Motivation It was pointed out that there is currently some overlap between the `try_with` `Service` combinator and `tower::filter` middleware (see #499 (comment) ). `try_with` synchronously maps from a `Request` -> `Result<DifferentRequest, Error>`, while `tower::filter` _asynchronously_ maps from a `&Request` to a `Result<(), Error>`. The key differences are: - `try_with` takes a request by value, and allows the predicate to return a *different* request value - `try_with` also permits changing the _type_ of the request - `try_with` is synchronous, while `tower::filter` is asynchronous - `tower::filter` has a `Predicate` trait, which can be implemented by more than just functions. For example, a struct with a `HashSet` could implement `Predicate` by failing requests that match the values in the hashset. It definitely seems like there's demand for both synchronous and asynchronous request filtering. However, the APIs we have currently differ pretty significantly. It would be nice to make them more consistent with each other. As an aside, `tower::filter` [does not seem all that widely used][1]. Meanwhile, `linkerd2-proxy` defines its own `RequestFilter` middleware, using a [predicate trait][2] that's essentially in between `tower::filter` and `ServiceExt::try_with`: - it's synchronous, like `try_with` - it allows modifying the type of the request, like `try_with` - it uses a trait for predicates, rather than a `Fn`, like `tower::filter` - it uses a similar naming scheme to `tower::filter` ("filtering" rather than "with"/"map"). [1]: https://github.com/search?l=&p=1&q=%22tower%3A%3Afilter%22+extension%3Ars&ref=advsearch&type=Code [2]: https://github.com/linkerd/linkerd2-proxy/blob/24bee8cbc5413b4587a14bea1e2714ce1f1f919a/linkerd/stack/src/request_filter.rs#L8-L12 ## Solution This branch rewrites `tower::filter` to make the following changes: * Predicates are synchronous by default. A separate `AsyncFilter` type and an `AsyncPredicate` trait are available for predicates returning futures. * Predicates may now return a new `Request` type, allowing `Filter` and `AsyncFilter` to subsume `try_map_request`. * Predicates may now return any error type, and errors are now converted into `BoxError`s. Closes #502 Signed-off-by: Eliza Weisman <[email protected]>
- Loading branch information
Showing
13 changed files
with
423 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.