-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
router: Use NewService instead of MakeService (#724)
There's no reason for `NewRouter` to operate over `MakeService`. This change makes it generic over a `NewService` instead, and moves the implementation into the stack subcrate.
- Loading branch information
Showing
10 changed files
with
85 additions
and
199 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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::NewService; | ||
use std::task::{Context, Poll}; | ||
use tower::util::{Oneshot, ServiceExt}; | ||
|
||
pub trait RecognizeRoute<T> { | ||
type Key: Clone; | ||
|
||
fn recognize(&self, t: &T) -> Self::Key; | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct NewRouter<T, N> { | ||
new_recgonize: T, | ||
inner: N, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Router<T, N> { | ||
recognize: T, | ||
inner: N, | ||
} | ||
|
||
impl<K, N> NewRouter<K, N> { | ||
pub fn new(new_recgonize: K, inner: N) -> Self { | ||
Self { | ||
new_recgonize, | ||
inner, | ||
} | ||
} | ||
} | ||
|
||
impl<T, K, N> NewService<T> for NewRouter<K, N> | ||
where | ||
K: NewService<T>, | ||
N: Clone, | ||
{ | ||
type Service = Router<K::Service, N>; | ||
|
||
fn new_service(&mut self, t: T) -> Self::Service { | ||
Router { | ||
recognize: self.new_recgonize.new_service(t), | ||
inner: self.inner.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<K, N, S, Req> tower::Service<Req> for Router<K, N> | ||
where | ||
K: RecognizeRoute<Req>, | ||
N: NewService<K::Key, Service = S>, | ||
S: tower::Service<Req>, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = Oneshot<S, Req>; | ||
|
||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn call(&mut self, req: Req) -> Self::Future { | ||
let key = self.recognize.recognize(&req); | ||
self.inner.new_service(key).oneshot(req) | ||
} | ||
} | ||
|
||
impl<T, K, F> RecognizeRoute<T> for F | ||
where | ||
K: Clone, | ||
F: Fn(&T) -> K, | ||
{ | ||
type Key = K; | ||
|
||
fn recognize(&self, t: &T) -> Self::Key { | ||
(self)(t) | ||
} | ||
} |