Skip to content

Commit

Permalink
feat(volo-http): use std::convert::Infallible instead of DynError
Browse files Browse the repository at this point in the history
Signed-off-by: Yu Li <[email protected]>
  • Loading branch information
yukiiiteru authored and PureWhiteWu committed Dec 6, 2023
1 parent 0b44589 commit 75e36c0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
4 changes: 3 additions & 1 deletion volo-http/src/extract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::convert::Infallible;

use futures_util::Future;
use hyper::http::{Method, Uri};
use volo::net::Address;

use crate::{response::Infallible, HttpContext, Params, State};
use crate::{HttpContext, Params, State};

pub trait FromContext<S>: Sized {
fn from_context(
Expand Down
8 changes: 4 additions & 4 deletions volo-http/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{future::Future, marker::PhantomData};
use std::{convert::Infallible, future::Future, marker::PhantomData};

use hyper::body::Incoming;
use motore::Service;
Expand All @@ -8,7 +8,7 @@ use crate::{
macros::{all_the_tuples, all_the_tuples_no_last_special_case},
request::FromRequest,
response::{IntoResponse, Response},
DynError, DynService, HttpContext,
DynService, HttpContext,
};

pub trait Handler<T, S>: Sized {
Expand Down Expand Up @@ -126,7 +126,7 @@ where
cx: &mut HttpContext,
req: Incoming,
state: S,
) -> Result<Response, DynError> {
) -> Result<Response, Infallible> {
self.0.into_route(state).call(cx, req).await
}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ where
S: Sync,
{
type Response = Response;
type Error = DynError;
type Error = Infallible;

async fn call<'s, 'cx>(
&'s self,
Expand Down
7 changes: 4 additions & 3 deletions volo-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod server;

mod macros;

use std::convert::Infallible;

pub use bytes::Bytes;
pub use hyper::{
body::Incoming,
Expand All @@ -19,12 +21,11 @@ pub use volo::net::Address;
pub use crate::{
param::Params,
request::{Json, Request},
response::{Infallible, Response},
response::Response,
server::Server,
};

pub type DynService = motore::BoxCloneService<HttpContext, Incoming, Response, DynError>;
pub type DynError = Box<dyn std::error::Error + Send + Sync>;
pub type DynService = motore::BoxCloneService<HttpContext, Incoming, Response, Infallible>;

#[derive(Debug, Default, Clone, Copy)]
pub struct State<S>(pub S);
Expand Down
12 changes: 7 additions & 5 deletions volo-http/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
convert::Infallible,
ops::{Deref, DerefMut},
pin::Pin,
task::{Context, Poll},
Expand All @@ -12,15 +13,16 @@ use hyper::{
};
use pin_project::pin_project;

use crate::DynError;

pub struct Response(pub(crate) hyper::http::Response<RespBody>);
pub struct Infallible;
pub struct Response(hyper::http::Response<RespBody>);

impl Response {
pub fn builder() -> Builder {
Builder::new()
}

pub(crate) fn inner(self) -> hyper::http::Response<RespBody> {
self.0
}
}

impl Deref for Response {
Expand Down Expand Up @@ -52,7 +54,7 @@ pub struct RespBody {
impl Body for RespBody {
type Data = Bytes;

type Error = DynError;
type Error = Infallible;

fn poll_frame(
self: Pin<&mut Self>,
Expand Down
24 changes: 12 additions & 12 deletions volo-http/src/route.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, convert::Infallible};

use hyper::{
body::Incoming,
Expand All @@ -9,7 +9,7 @@ use motore::{layer::Layer, service::Service};
use crate::{
handler::{DynHandler, Handler},
response::IntoResponse,
DynError, DynService, HttpContext, Response,
DynService, HttpContext, Response,
};

// The `matchit::Router` cannot be converted to `Iterator`, so using
Expand Down Expand Up @@ -72,7 +72,7 @@ where
pub fn layer<L>(self, l: L) -> Self
where
L: Layer<DynService> + Clone + Send + Sync + 'static,
L::Service: Service<HttpContext, Incoming, Response = Response, Error = DynError>
L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
+ Clone
+ Send
+ Sync
Expand Down Expand Up @@ -120,7 +120,7 @@ where
impl Service<HttpContext, Incoming> for Router<()> {
type Response = Response;

type Error = DynError;
type Error = Infallible;

async fn call<'s, 'cx>(
&'s self,
Expand Down Expand Up @@ -179,7 +179,7 @@ where
pub fn layer<L>(self, l: L) -> Self
where
L: Layer<DynService> + Clone + Send + Sync + 'static,
L::Service: Service<HttpContext, Incoming, Response = Response, Error = DynError>
L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
+ Clone
+ Send
+ Sync
Expand Down Expand Up @@ -246,7 +246,7 @@ where
cx: &'cx mut HttpContext,
req: Incoming,
state: S,
) -> Result<Response, DynError>
) -> Result<Response, Infallible>
where
S: 'cx,
{
Expand Down Expand Up @@ -355,7 +355,7 @@ where

pub fn from_service<Srv>(srv: Srv) -> MethodEndpoint<S>
where
Srv: Service<HttpContext, Incoming, Response = Response, Error = DynError>
Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
+ Clone
+ Send
+ Sync
Expand Down Expand Up @@ -411,7 +411,7 @@ where

pub fn from_service<Srv>(srv: Srv) -> Fallback<S>
where
Srv: Service<HttpContext, Incoming, Response = Response, Error = DynError>
Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
+ Clone
+ Send
+ Sync
Expand All @@ -433,7 +433,7 @@ where
pub(crate) fn layer<L>(self, l: L) -> Self
where
L: Layer<DynService> + Clone + Send + Sync + 'static,
L::Service: Service<HttpContext, Incoming, Response = Response, Error = DynError>
L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
+ Clone
+ Send
+ Sync
Expand All @@ -454,7 +454,7 @@ where
cx: &'cx mut HttpContext,
req: Incoming,
state: S,
) -> Result<Response, DynError>
) -> Result<Response, Infallible>
where
S: 'cx,
{
Expand All @@ -476,7 +476,7 @@ where

pub fn from_service<Srv, S>(srv: Srv) -> MethodEndpoint<S>
where
Srv: Service<HttpContext, Incoming, Response = Response, Error = DynError>
Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
+ Clone
+ Send
+ Sync
Expand All @@ -491,7 +491,7 @@ struct RouteForStatusCode(StatusCode);

impl Service<HttpContext, Incoming> for RouteForStatusCode {
type Response = Response;
type Error = DynError;
type Error = Infallible;

async fn call<'s, 'cx>(
&'s self,
Expand Down
22 changes: 16 additions & 6 deletions volo-http/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
convert::Infallible,
sync::{atomic::Ordering, Arc},
time::Duration,
};
Expand All @@ -10,7 +11,11 @@ use tokio::sync::Notify;
use tracing::{info, trace};
use volo::net::{conn::Conn, incoming::Incoming, Address, MakeIncoming};

use crate::{param::Params, response::Response, DynError, HttpContext};
use crate::{
param::Params,
response::{IntoResponse, RespBody, Response},
HttpContext,
};

pub struct Server<App> {
app: Arc<App>,
Expand All @@ -26,8 +31,10 @@ impl<A> Clone for Server<A> {

impl<App> Server<App>
where
App: motore::Service<HttpContext, BodyIncoming, Response = Response> + Send + Sync + 'static,
App::Error: Into<DynError>,
App: motore::Service<HttpContext, BodyIncoming, Response = Response, Error = Infallible>
+ Send
+ Sync
+ 'static,
{
pub fn new(app: App) -> Self {
Self { app: Arc::new(app) }
Expand Down Expand Up @@ -157,12 +164,11 @@ async fn handle_conn<S>(
conn_cnt: Arc<std::sync::atomic::AtomicUsize>,
peer: Address,
) where
S: motore::Service<HttpContext, BodyIncoming, Response = Response>
S: motore::Service<HttpContext, BodyIncoming, Response = Response, Error = Infallible>
+ Clone
+ Send
+ Sync
+ 'static,
S::Error: Into<DynError>,
{
let notified = exit_notify.notified();
tokio::pin!(notified);
Expand All @@ -186,7 +192,11 @@ async fn handle_conn<S>(
inner: Vec::with_capacity(0),
},
};
service.call(&mut cx, req).await.map(|resp| resp.0)
let resp = match service.call(&mut cx, req).await {
Ok(resp) => resp,
Err(inf) => inf.into_response(),
};
Ok::<hyper::http::Response<RespBody>, Infallible>(resp.inner())
}
}),
);
Expand Down

0 comments on commit 75e36c0

Please sign in to comment.