single tower

This commit is contained in:
nora 2022-09-15 22:03:24 +02:00
parent 3d7956c00c
commit 84882c7eea
6 changed files with 18 additions and 23 deletions

136
tower/src/balance.rs Normal file
View file

@ -0,0 +1,136 @@
use crate::load::Load;
use crate::make::MakeService;
use crate::Service;
use crate::{Change, Discover};
use futures_core::Stream;
use futures_util::future::{self};
use std::hash::Hash;
use std::marker::PhantomData;
use std::{
pin::Pin,
task::{Context, Poll},
};
pub struct Balance<D, Req> {
_req: PhantomData<(D, Req)>,
}
impl<D, Req> Balance<D, Req>
where
D: Discover,
D::Service: Service<Req>,
<D::Service as Service<Req>>::Error: Into<crate::BoxError>,
{
pub fn new(_: D) -> Self {
todo!()
}
}
impl<D, Req> Service<Req> for Balance<D, Req>
where
D: Discover + Unpin,
D::Key: Hash + Clone,
D::Error: Into<crate::BoxError>,
D::Service: Service<Req> + Load,
<D::Service as Load>::Metric: std::fmt::Debug,
<D::Service as Service<Req>>::Error: Into<crate::BoxError>,
{
type Response = <D::Service as Service<Req>>::Response;
type Error = crate::BoxError;
type Future = future::MapErr<
<D::Service as Service<Req>>::Future,
fn(<D::Service as Service<Req>>::Error) -> crate::BoxError,
>;
fn poll_ready(&mut self) -> Poll<Result<(), Self::Error>> {
todo!()
}
}
pub struct PoolDiscoverer<MS, Target, Request>
where
MS: MakeService<Target, Request>,
{
_p: PhantomData<(MS, Target, Request)>,
}
impl<MS, Target, Request> Stream for PoolDiscoverer<MS, Target, Request>
where
MS: MakeService<Target, Request>,
{
type Item = Result<Change<usize, DropNotifyService<MS::Service>>, MS::MakeError>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
todo!()
}
}
pub struct Builder {}
impl Builder {
pub fn build<MS, Target, Request>() -> ()
where
MS: MakeService<Target, Request>,
MS::Service: Load,
<MS::Service as Load>::Metric: std::fmt::Debug,
MS::MakeError: Into<crate::BoxError>,
MS::Error: Into<crate::BoxError>,
{
let d: PoolDiscoverer<MS, Target, Request> = todo!();
let x = Balance::new(Box::pin(d));
todo!()
}
}
pub struct Pool<MS, Target, Request> {
balance: (MS, Target, Request),
}
type PinBalance<S, Request> = Balance<Pin<Box<S>>, Request>;
impl<MS, Target, Req> Service<Req> for Pool<MS, Target, Req>
where
MS: MakeService<Target, Req>,
MS::Service: Load,
<MS::Service as Load>::Metric: std::fmt::Debug,
MS::MakeError: Into<crate::BoxError>,
MS::Error: Into<crate::BoxError>,
Target: Clone,
{
type Response = <PinBalance<PoolDiscoverer<MS, Target, Req>, Req> as Service<Req>>::Response;
type Error = <PinBalance<PoolDiscoverer<MS, Target, Req>, Req> as Service<Req>>::Error;
type Future = <PinBalance<PoolDiscoverer<MS, Target, Req>, Req> as Service<Req>>::Future;
fn poll_ready(&mut self) -> Poll<Result<(), Self::Error>> {
todo!()
}
}
pub struct DropNotifyService<Svc> {
svc: Svc,
}
impl<Svc> Drop for DropNotifyService<Svc> {
fn drop(&mut self) {
todo!()
}
}
impl<Svc: Load> Load for DropNotifyService<Svc> {
type Metric = Svc::Metric;
fn load(&self) -> Self::Metric {
todo!()
}
}
impl<Request, Svc: Service<Request>> Service<Request> for DropNotifyService<Svc> {
type Response = Svc::Response;
type Future = Svc::Future;
type Error = Svc::Error;
fn poll_ready(&mut self) -> Poll<Result<(), Self::Error>> {
todo!()
}
}

59
tower/src/lib.rs Normal file
View file

@ -0,0 +1,59 @@
// #![allow(warnings)]
pub mod balance;
pub mod make;
pub trait Sealed<T> {}
/// Alias for a type-erased error type.
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
mod load {
pub trait Load {
type Metric;
fn load(&self) -> Self::Metric;
}
}
use std::future::Future;
use std::task::Poll;
use futures_core::TryStream;
pub trait Discover {
type Key: Eq;
type Service;
type Error;
}
impl<K, S, E, D: ?Sized> Sealed<Change<(), ()>> for D
where
D: TryStream<Ok = Change<K, S>, Error = E>,
K: Eq,
{
}
impl<K, S, E, D: ?Sized> Discover for D
where
D: TryStream<Ok = Change<K, S>, Error = E>,
K: Eq,
{
type Key = K;
type Service = S;
type Error = E;
}
pub struct Change<K, V>(K, V);
pub trait Service<Request> {
/// Responses given by the service.
type Response;
/// Errors produced by the service.
type Error;
/// The future response value.
type Future: Future<Output = Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self) -> Poll<Result<(), Self::Error>>;
}

70
tower/src/make.rs Normal file
View file

@ -0,0 +1,70 @@
//! Trait aliases for Services that produce specific types of Responses.
use crate::Sealed;
use crate::Service;
use std::task::Poll;
use std::convert::Infallible;
pub struct Shared<S> {
service: S,
}
impl<S, T> Service<T> for Shared<S>
where
S: Clone,
{
type Response = S;
type Error = Infallible;
type Future = SharedFuture<S>;
fn poll_ready(&mut self) -> Poll<Result<(), Self::Error>> {
todo!()
}
}
pub struct SharedFuture<S> {
_s: S,
}
impl<S> std::future::Future for SharedFuture<S>
where
futures_util::future::Ready<Result<S, Infallible>>: std::future::Future,
{
type Output =
<futures_util::future::Ready<Result<S, Infallible>> as std::future::Future>::Output;
fn poll(
self: std::pin::Pin<&mut Self>,
_: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
todo!()
}
}
pub trait MakeService<Target, Request> {
type Response;
type Error;
type Service: Service<Request, Response = Self::Response, Error = Self::Error>;
type MakeError;
type Future;
}
impl<M, S, Target, Request> Sealed<(Target, Request)> for M
where
M: Service<Target, Response = S>,
S: Service<Request>,
{
}
impl<M, S, Target, Request> MakeService<Target, Request> for M
where
M: Service<Target, Response = S>,
S: Service<Request>,
{
type Response = S::Response;
type Error = S::Error;
type Service = S;
type MakeError = M::Error;
type Future = M::Future;
}