1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-26 20:57:43 +02:00

service trait takes request type parameter (#232)

This commit is contained in:
Rob Ede
2020-12-27 04:28:00 +00:00
committed by GitHub
parent 518bf3f6a6
commit 3ab8c3eb69
28 changed files with 1142 additions and 1136 deletions

View File

@ -1,4 +1,4 @@
//! Framed dispatcher service and related utilities
//! Framed dispatcher service and related utilities.
#![allow(type_alias_bounds)]
@ -11,6 +11,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
use actix_service::{IntoService, Service};
use futures_core::stream::Stream;
use log::debug;
use pin_project_lite::pin_project;
use crate::mpsc;
@ -62,12 +63,12 @@ pub enum Message<T> {
Close,
}
pin_project_lite::pin_project! {
pin_project! {
/// Dispatcher is a future that reads frames from Framed object
/// and passes them to the service.
pub struct Dispatcher<S, T, U, I>
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead,
@ -86,7 +87,11 @@ pin_project_lite::pin_project! {
}
}
enum State<S: Service, U: Encoder<I> + Decoder, I> {
enum State<S, U, I>
where
S: Service<<U as Decoder>::Item>,
U: Encoder<I> + Decoder,
{
Processing,
Error(DispatcherError<S::Error, U, I>),
FramedError(DispatcherError<S::Error, U, I>),
@ -94,7 +99,11 @@ enum State<S: Service, U: Encoder<I> + Decoder, I> {
Stopping,
}
impl<S: Service, U: Encoder<I> + Decoder, I> State<S, U, I> {
impl<S, U, I> State<S, U, I>
where
S: Service<<U as Decoder>::Item>,
U: Encoder<I> + Decoder,
{
fn take_error(&mut self) -> DispatcherError<S::Error, U, I> {
match mem::replace(self, State::Processing) {
State::Error(err) => err,
@ -112,7 +121,7 @@ impl<S: Service, U: Encoder<I> + Decoder, I> State<S, U, I> {
impl<S, T, U, I> Dispatcher<S, T, U, I>
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -121,7 +130,10 @@ where
<U as Decoder>::Error: fmt::Debug,
<U as Encoder<I>>::Error: fmt::Debug,
{
pub fn new<F: IntoService<S>>(framed: Framed<T, U>, service: F) -> Self {
pub fn new<F>(framed: Framed<T, U>, service: F) -> Self
where
F: IntoService<S, <U as Decoder>::Item>,
{
let (tx, rx) = mpsc::channel();
Dispatcher {
framed,
@ -133,11 +145,14 @@ where
}
/// Construct new `Dispatcher` instance with customer `mpsc::Receiver`
pub fn with_rx<F: IntoService<S>>(
pub fn with_rx<F>(
framed: Framed<T, U>,
service: F,
rx: mpsc::Receiver<Result<Message<I>, S::Error>>,
) -> Self {
) -> Self
where
F: IntoService<S, <U as Decoder>::Item>,
{
let tx = rx.sender();
Dispatcher {
framed,
@ -176,7 +191,7 @@ where
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -220,7 +235,7 @@ where
/// write to framed object
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> bool
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
@ -271,7 +286,7 @@ where
impl<S, T, U, I> Future for Dispatcher<S, T, U, I>
where
S: Service<Request = <U as Decoder>::Item, Response = I>,
S: Service<<U as Decoder>::Item, Response = I>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite,

View File

@ -2,6 +2,7 @@
//!
//! If the response does not complete within the specified timeout, the response
//! will be aborted.
use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;
@ -10,6 +11,7 @@ use core::{fmt, time};
use actix_rt::time::{delay_for, Delay};
use actix_service::{IntoService, Service, Transform};
use pin_project_lite::pin_project;
/// Applies a timeout to requests.
#[derive(Debug)]
@ -77,21 +79,21 @@ impl<E> Clone for Timeout<E> {
}
}
impl<S, E> Transform<S> for Timeout<E>
impl<S, E, Req> Transform<S, Req> for Timeout<E>
where
S: Service,
S: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = TimeoutError<S::Error>;
type Transform = TimeoutService<S>;
type InitError = E;
type Transform = TimeoutService<S, Req>;
type Future = TimeoutFuture<Self::Transform, Self::InitError>;
fn new_transform(&self, service: S) -> Self::Future {
let service = TimeoutService {
service,
timeout: self.timeout,
_phantom: PhantomData,
};
TimeoutFuture {
@ -118,40 +120,41 @@ impl<T, E> Future for TimeoutFuture<T, E> {
/// Applies a timeout to requests.
#[derive(Debug, Clone)]
pub struct TimeoutService<S> {
pub struct TimeoutService<S, Req> {
service: S,
timeout: time::Duration,
_phantom: PhantomData<Req>,
}
impl<S> TimeoutService<S>
impl<S, Req> TimeoutService<S, Req>
where
S: Service,
S: Service<Req>,
{
pub fn new<U>(timeout: time::Duration, service: U) -> Self
where
U: IntoService<S>,
U: IntoService<S, Req>,
{
TimeoutService {
timeout,
service: service.into_service(),
_phantom: PhantomData,
}
}
}
impl<S> Service for TimeoutService<S>
impl<S, Req> Service<Req> for TimeoutService<S, Req>
where
S: Service,
S: Service<Req>,
{
type Request = S::Request;
type Response = S::Response;
type Error = TimeoutError<S::Error>;
type Future = TimeoutServiceResponse<S>;
type Future = TimeoutServiceResponse<S, Req>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx).map_err(TimeoutError::Service)
}
fn call(&mut self, request: S::Request) -> Self::Future {
fn call(&mut self, request: Req) -> Self::Future {
TimeoutServiceResponse {
fut: self.service.call(request),
sleep: delay_for(self.timeout),
@ -159,21 +162,24 @@ where
}
}
pin_project_lite::pin_project! {
pin_project! {
/// `TimeoutService` response future
#[derive(Debug)]
pub struct TimeoutServiceResponse<T: Service> {
pub struct TimeoutServiceResponse<S, Req>
where
S: Service<Req>
{
#[pin]
fut: T::Future,
fut: S::Future,
sleep: Delay,
}
}
impl<T> Future for TimeoutServiceResponse<T>
impl<S, Req> Future for TimeoutServiceResponse<S, Req>
where
T: Service,
S: Service<Req>,
{
type Output = Result<T::Response, TimeoutError<T::Error>>;
type Output = Result<S::Response, TimeoutError<S::Error>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@ -204,8 +210,7 @@ mod tests {
struct SleepService(Duration);
impl Service for SleepService {
type Request = ();
impl Service<()> for SleepService {
type Response = ();
type Error = ();
type Future = LocalBoxFuture<'static, Result<(), ()>>;