-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter Store and forward messages if feature is not enabled
- Store and forward messages are discarded when they are not supported by the node - To implement this, tower-filter was used. However, it is not released yet for futures 0.3 so I've included it directly in this PR
- Loading branch information
Showing
15 changed files
with
477 additions
and
12 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 0.3.0-alpha.2 (September 30, 2019) | ||
|
||
- Move to `futures-*-preview 0.3.0-alpha.19` | ||
- Move to `pin-project 0.4` | ||
|
||
# 0.3.0-alpha.1 | ||
|
||
- Move to `std::future` | ||
|
||
# 0.1.0 (unreleased) | ||
|
||
- Initial release |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
[package] | ||
name = "tower-filter" | ||
# When releasing to crates.io: | ||
# - Remove path dependencies | ||
# - Update html_root_url. | ||
# - Update doc url | ||
# - Cargo.toml | ||
# - README.md | ||
# - Update CHANGELOG.md. | ||
# - Create "v0.1.x" git tag. | ||
version = "0.3.0-alpha.2" | ||
authors = ["Tower Maintainers <team@tower-rs.com>"] | ||
license = "MIT" | ||
readme = "README.md" | ||
repository = "https://github.com/tower-rs/tower" | ||
homepage = "https://github.com/tower-rs/tower" | ||
documentation = "https://docs.rs/tower-filter/0.3.0-alpha.2" | ||
description = """ | ||
Conditionally allow requests to be dispatched to a service based on the result | ||
of a predicate. | ||
""" | ||
categories = ["asynchronous", "network-programming"] | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
tower= { version = "=0.3.0-alpha.2"} | ||
pin-project = "0.4" | ||
futures-core-preview = "=0.3.0-alpha.19" | ||
|
||
[dev-dependencies] | ||
tower-test = { version = "=0.3.0-alpha.2" } | ||
tokio-test = "=0.2.0-alpha.6" | ||
tokio = "=0.2.0-alpha.6" | ||
futures-util-preview = "=0.3.0-alpha.19" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright (c) 2019 Tower Contributors | ||
|
||
Permission is hereby granted, free of charge, to any | ||
person obtaining a copy of this software and associated | ||
documentation files (the "Software"), to deal in the | ||
Software without restriction, including without | ||
limitation the rights to use, copy, modify, merge, | ||
publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software | ||
is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice | ||
shall be included in all copies or substantial portions | ||
of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Tower Filter | ||
|
||
Conditionally allow requests to be dispatched to a service based on the result | ||
of a predicate. | ||
|
||
## License | ||
|
||
This project is licensed under the [MIT license](LICENSE). | ||
|
||
### Contribution | ||
|
||
Unless you explicitly state otherwise, any contribution intentionally submitted | ||
for inclusion in Tower by you, shall be licensed as MIT, without any additional | ||
terms or conditions. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! Error types | ||
use std::{error, fmt}; | ||
|
||
/// Error produced by `Filter` | ||
#[derive(Debug)] | ||
pub struct Error { | ||
source: Option<Source>, | ||
} | ||
|
||
pub(crate) type Source = Box<dyn error::Error + Send + Sync>; | ||
|
||
impl Error { | ||
/// Create a new `Error` representing a rejected request. | ||
pub fn rejected() -> Error { | ||
Error { source: None } | ||
} | ||
|
||
/// Create a new `Error` representing an inner service error. | ||
pub fn inner<E>(source: E) -> Error | ||
where E: Into<Source> { | ||
Error { | ||
source: Some(source.into()), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
if self.source.is_some() { | ||
write!(fmt, "inner service errored") | ||
} else { | ||
write!(fmt, "rejected") | ||
} | ||
} | ||
} | ||
|
||
impl error::Error for Error { | ||
fn source(&self) -> Option<&(dyn error::Error + 'static)> { | ||
if let Some(ref err) = self.source { | ||
Some(&**err) | ||
} else { | ||
None | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//! Future types | ||
use crate::error::{self, Error}; | ||
use futures_core::ready; | ||
use pin_project::{pin_project, project}; | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
use tower::Service; | ||
|
||
/// Filtered response future | ||
#[pin_project] | ||
#[derive(Debug)] | ||
pub struct ResponseFuture<T, S, Request> | ||
where S: Service<Request> | ||
{ | ||
#[pin] | ||
/// Response future state | ||
state: State<Request, S::Future>, | ||
|
||
#[pin] | ||
/// Predicate future | ||
check: T, | ||
|
||
/// Inner service | ||
service: S, | ||
} | ||
|
||
#[pin_project] | ||
#[derive(Debug)] | ||
enum State<Request, U> { | ||
Check(Option<Request>), | ||
WaitResponse(#[pin] U), | ||
} | ||
|
||
impl<F, T, S, Request> ResponseFuture<F, S, Request> | ||
where | ||
F: Future<Output = Result<T, Error>>, | ||
S: Service<Request>, | ||
S::Error: Into<error::Source>, | ||
{ | ||
pub(crate) fn new(request: Request, check: F, service: S) -> Self { | ||
ResponseFuture { | ||
state: State::Check(Some(request)), | ||
check, | ||
service, | ||
} | ||
} | ||
} | ||
|
||
impl<F, T, S, Request> Future for ResponseFuture<F, S, Request> | ||
where | ||
F: Future<Output = Result<T, Error>>, | ||
S: Service<Request>, | ||
S::Error: Into<error::Source>, | ||
{ | ||
type Output = Result<S::Response, Error>; | ||
|
||
#[project] | ||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let mut this = self.project(); | ||
|
||
loop { | ||
#[project] | ||
match this.state.as_mut().project() { | ||
State::Check(request) => { | ||
let request = request | ||
.take() | ||
.expect("we either give it back or leave State::Check once we take"); | ||
|
||
// Poll predicate | ||
match this.check.as_mut().poll(cx)? { | ||
Poll::Ready(_) => { | ||
let response = this.service.call(request); | ||
this.state.set(State::WaitResponse(response)); | ||
}, | ||
Poll::Pending => { | ||
this.state.set(State::Check(Some(request))); | ||
return Poll::Pending; | ||
}, | ||
} | ||
}, | ||
State::WaitResponse(response) => { | ||
return Poll::Ready(ready!(response.poll(cx)).map_err(Error::inner)); | ||
}, | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.