feat: mark boxed http body as sync (#291)
This commit is contained in:
parent
1b3db8959c
commit
9ca20c69b2
3 changed files with 30 additions and 42 deletions
|
|
@ -4,7 +4,7 @@ use std::{convert::Infallible, fmt::Display, sync::Arc, time::Duration};
|
||||||
use bytes::{Buf, Bytes};
|
use bytes::{Buf, Bytes};
|
||||||
use http::Response;
|
use http::Response;
|
||||||
use http_body::Body;
|
use http_body::Body;
|
||||||
use http_body_util::{BodyExt, Empty, Full, combinators::UnsyncBoxBody};
|
use http_body_util::{BodyExt, Empty, Full, combinators::BoxBody};
|
||||||
use sse_stream::{KeepAlive, Sse, SseBody};
|
use sse_stream::{KeepAlive, Sse, SseBody};
|
||||||
|
|
||||||
use super::http_header::EVENT_STREAM_MIME_TYPE;
|
use super::http_header::EVENT_STREAM_MIME_TYPE;
|
||||||
|
|
@ -18,12 +18,12 @@ pub fn session_id() -> SessionId {
|
||||||
|
|
||||||
pub const DEFAULT_AUTO_PING_INTERVAL: Duration = Duration::from_secs(15);
|
pub const DEFAULT_AUTO_PING_INTERVAL: Duration = Duration::from_secs(15);
|
||||||
|
|
||||||
pub(crate) type BoxResponse = Response<UnsyncBoxBody<Bytes, Infallible>>;
|
pub(crate) type BoxResponse = Response<BoxBody<Bytes, Infallible>>;
|
||||||
|
|
||||||
pub(crate) fn accepted_response() -> Response<UnsyncBoxBody<Bytes, Infallible>> {
|
pub(crate) fn accepted_response() -> Response<BoxBody<Bytes, Infallible>> {
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(http::StatusCode::ACCEPTED)
|
.status(http::StatusCode::ACCEPTED)
|
||||||
.body(Empty::new().boxed_unsync())
|
.body(Empty::new().boxed())
|
||||||
.expect("valid response")
|
.expect("valid response")
|
||||||
}
|
}
|
||||||
pin_project_lite::pin_project! {
|
pin_project_lite::pin_project! {
|
||||||
|
|
@ -63,9 +63,9 @@ pub struct ServerSseMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn sse_stream_response(
|
pub(crate) fn sse_stream_response(
|
||||||
stream: impl futures::Stream<Item = ServerSseMessage> + Send + 'static,
|
stream: impl futures::Stream<Item = ServerSseMessage> + Send + Sync + 'static,
|
||||||
keep_alive: Option<Duration>,
|
keep_alive: Option<Duration>,
|
||||||
) -> Response<UnsyncBoxBody<Bytes, Infallible>> {
|
) -> Response<BoxBody<Bytes, Infallible>> {
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
let stream = SseBody::new(stream.map(|message| {
|
let stream = SseBody::new(stream.map(|message| {
|
||||||
let data = serde_json::to_string(&message.message).expect("valid message");
|
let data = serde_json::to_string(&message.message).expect("valid message");
|
||||||
|
|
@ -76,8 +76,8 @@ pub(crate) fn sse_stream_response(
|
||||||
let stream = match keep_alive {
|
let stream = match keep_alive {
|
||||||
Some(duration) => stream
|
Some(duration) => stream
|
||||||
.with_keep_alive::<TokioTimer>(KeepAlive::new().interval(duration))
|
.with_keep_alive::<TokioTimer>(KeepAlive::new().interval(duration))
|
||||||
.boxed_unsync(),
|
.boxed(),
|
||||||
None => stream.boxed_unsync(),
|
None => stream.boxed(),
|
||||||
};
|
};
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(http::StatusCode::OK)
|
.status(http::StatusCode::OK)
|
||||||
|
|
@ -89,7 +89,7 @@ pub(crate) fn sse_stream_response(
|
||||||
|
|
||||||
pub(crate) const fn internal_error_response<E: Display>(
|
pub(crate) const fn internal_error_response<E: Display>(
|
||||||
context: &str,
|
context: &str,
|
||||||
) -> impl FnOnce(E) -> Response<UnsyncBoxBody<Bytes, Infallible>> {
|
) -> impl FnOnce(E) -> Response<BoxBody<Bytes, Infallible>> {
|
||||||
move |error| {
|
move |error| {
|
||||||
tracing::error!("Internal server error when {context}: {error}");
|
tracing::error!("Internal server error when {context}: {error}");
|
||||||
Response::builder()
|
Response::builder()
|
||||||
|
|
@ -98,24 +98,22 @@ pub(crate) const fn internal_error_response<E: Display>(
|
||||||
Full::new(Bytes::from(format!(
|
Full::new(Bytes::from(format!(
|
||||||
"Encounter an error when {context}: {error}"
|
"Encounter an error when {context}: {error}"
|
||||||
)))
|
)))
|
||||||
.boxed_unsync(),
|
.boxed(),
|
||||||
)
|
)
|
||||||
.expect("valid response")
|
.expect("valid response")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn unexpected_message_response(
|
pub(crate) fn unexpected_message_response(expect: &str) -> Response<BoxBody<Bytes, Infallible>> {
|
||||||
expect: &str,
|
|
||||||
) -> Response<UnsyncBoxBody<Bytes, Infallible>> {
|
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(http::StatusCode::UNPROCESSABLE_ENTITY)
|
.status(http::StatusCode::UNPROCESSABLE_ENTITY)
|
||||||
.body(Full::new(Bytes::from(format!("Unexpected message, expect {expect}"))).boxed_unsync())
|
.body(Full::new(Bytes::from(format!("Unexpected message, expect {expect}"))).boxed())
|
||||||
.expect("valid response")
|
.expect("valid response")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn expect_json<B>(
|
pub(crate) async fn expect_json<B>(
|
||||||
body: B,
|
body: B,
|
||||||
) -> Result<ClientJsonRpcMessage, Response<UnsyncBoxBody<Bytes, Infallible>>>
|
) -> Result<ClientJsonRpcMessage, Response<BoxBody<Bytes, Infallible>>>
|
||||||
where
|
where
|
||||||
B: Body + Send + 'static,
|
B: Body + Send + 'static,
|
||||||
B::Error: Display,
|
B::Error: Display,
|
||||||
|
|
@ -129,7 +127,7 @@ where
|
||||||
.status(http::StatusCode::UNSUPPORTED_MEDIA_TYPE)
|
.status(http::StatusCode::UNSUPPORTED_MEDIA_TYPE)
|
||||||
.body(
|
.body(
|
||||||
Full::new(Bytes::from(format!("fail to deserialize request body {e}")))
|
Full::new(Bytes::from(format!("fail to deserialize request body {e}")))
|
||||||
.boxed_unsync(),
|
.boxed(),
|
||||||
)
|
)
|
||||||
.expect("valid response");
|
.expect("valid response");
|
||||||
Err(response)
|
Err(response)
|
||||||
|
|
@ -139,10 +137,7 @@ where
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let response = Response::builder()
|
let response = Response::builder()
|
||||||
.status(http::StatusCode::INTERNAL_SERVER_ERROR)
|
.status(http::StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
.body(
|
.body(Full::new(Bytes::from(format!("Failed to read request body: {e}"))).boxed())
|
||||||
Full::new(Bytes::from(format!("Failed to read request body: {e}")))
|
|
||||||
.boxed_unsync(),
|
|
||||||
)
|
|
||||||
.expect("valid response");
|
.expect("valid response");
|
||||||
Err(response)
|
Err(response)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ pub trait SessionManager: Send + Sync + 'static {
|
||||||
id: &SessionId,
|
id: &SessionId,
|
||||||
message: ClientJsonRpcMessage,
|
message: ClientJsonRpcMessage,
|
||||||
) -> impl Future<
|
) -> impl Future<
|
||||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + 'static, Self::Error>,
|
Output = Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>,
|
||||||
> + Send;
|
> + Send;
|
||||||
fn accept_message(
|
fn accept_message(
|
||||||
&self,
|
&self,
|
||||||
|
|
@ -42,13 +42,13 @@ pub trait SessionManager: Send + Sync + 'static {
|
||||||
&self,
|
&self,
|
||||||
id: &SessionId,
|
id: &SessionId,
|
||||||
) -> impl Future<
|
) -> impl Future<
|
||||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + 'static, Self::Error>,
|
Output = Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>,
|
||||||
> + Send;
|
> + Send;
|
||||||
fn resume(
|
fn resume(
|
||||||
&self,
|
&self,
|
||||||
id: &SessionId,
|
id: &SessionId,
|
||||||
last_event_id: String,
|
last_event_id: String,
|
||||||
) -> impl Future<
|
) -> impl Future<
|
||||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + 'static, Self::Error>,
|
Output = Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>,
|
||||||
> + Send;
|
> + Send;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use bytes::Bytes;
|
||||||
use futures::{StreamExt, future::BoxFuture};
|
use futures::{StreamExt, future::BoxFuture};
|
||||||
use http::{Method, Request, Response, header::ALLOW};
|
use http::{Method, Request, Response, header::ALLOW};
|
||||||
use http_body::Body;
|
use http_body::Body;
|
||||||
use http_body_util::{BodyExt, Full, combinators::UnsyncBoxBody};
|
use http_body_util::{BodyExt, Full, combinators::BoxBody};
|
||||||
use tokio_stream::wrappers::ReceiverStream;
|
use tokio_stream::wrappers::ReceiverStream;
|
||||||
|
|
||||||
use super::session::SessionManager;
|
use super::session::SessionManager;
|
||||||
|
|
@ -105,7 +105,7 @@ where
|
||||||
fn get_service(&self) -> Result<S, std::io::Error> {
|
fn get_service(&self) -> Result<S, std::io::Error> {
|
||||||
(self.service_factory)()
|
(self.service_factory)()
|
||||||
}
|
}
|
||||||
pub async fn handle<B>(&self, request: Request<B>) -> Response<UnsyncBoxBody<Bytes, Infallible>>
|
pub async fn handle<B>(&self, request: Request<B>) -> Response<BoxBody<Bytes, Infallible>>
|
||||||
where
|
where
|
||||||
B: Body + Send + 'static,
|
B: Body + Send + 'static,
|
||||||
B::Error: Display,
|
B::Error: Display,
|
||||||
|
|
@ -120,7 +120,7 @@ where
|
||||||
let response = Response::builder()
|
let response = Response::builder()
|
||||||
.status(http::StatusCode::METHOD_NOT_ALLOWED)
|
.status(http::StatusCode::METHOD_NOT_ALLOWED)
|
||||||
.header(ALLOW, "GET, POST, DELETE")
|
.header(ALLOW, "GET, POST, DELETE")
|
||||||
.body(Full::new(Bytes::from("Method Not Allowed")).boxed_unsync())
|
.body(Full::new(Bytes::from("Method Not Allowed")).boxed())
|
||||||
.expect("valid response");
|
.expect("valid response");
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
@ -148,7 +148,7 @@ where
|
||||||
Full::new(Bytes::from(
|
Full::new(Bytes::from(
|
||||||
"Not Acceptable: Client must accept text/event-stream",
|
"Not Acceptable: Client must accept text/event-stream",
|
||||||
))
|
))
|
||||||
.boxed_unsync(),
|
.boxed(),
|
||||||
)
|
)
|
||||||
.expect("valid response"));
|
.expect("valid response"));
|
||||||
}
|
}
|
||||||
|
|
@ -162,7 +162,7 @@ where
|
||||||
// unauthorized
|
// unauthorized
|
||||||
return Ok(Response::builder()
|
return Ok(Response::builder()
|
||||||
.status(http::StatusCode::UNAUTHORIZED)
|
.status(http::StatusCode::UNAUTHORIZED)
|
||||||
.body(Full::new(Bytes::from("Unauthorized: Session ID is required")).boxed_unsync())
|
.body(Full::new(Bytes::from("Unauthorized: Session ID is required")).boxed())
|
||||||
.expect("valid response"));
|
.expect("valid response"));
|
||||||
};
|
};
|
||||||
// check if session exists
|
// check if session exists
|
||||||
|
|
@ -175,7 +175,7 @@ where
|
||||||
// unauthorized
|
// unauthorized
|
||||||
return Ok(Response::builder()
|
return Ok(Response::builder()
|
||||||
.status(http::StatusCode::UNAUTHORIZED)
|
.status(http::StatusCode::UNAUTHORIZED)
|
||||||
.body(Full::new(Bytes::from("Unauthorized: Session not found")).boxed_unsync())
|
.body(Full::new(Bytes::from("Unauthorized: Session not found")).boxed())
|
||||||
.expect("valid response"));
|
.expect("valid response"));
|
||||||
}
|
}
|
||||||
// check if last event id is provided
|
// check if last event id is provided
|
||||||
|
|
@ -219,7 +219,7 @@ where
|
||||||
{
|
{
|
||||||
return Ok(Response::builder()
|
return Ok(Response::builder()
|
||||||
.status(http::StatusCode::NOT_ACCEPTABLE)
|
.status(http::StatusCode::NOT_ACCEPTABLE)
|
||||||
.body(Full::new(Bytes::from("Not Acceptable: Client must accept both application/json and text/event-stream")).boxed_unsync())
|
.body(Full::new(Bytes::from("Not Acceptable: Client must accept both application/json and text/event-stream")).boxed())
|
||||||
.expect("valid response"));
|
.expect("valid response"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -236,7 +236,7 @@ where
|
||||||
Full::new(Bytes::from(
|
Full::new(Bytes::from(
|
||||||
"Unsupported Media Type: Content-Type must be application/json",
|
"Unsupported Media Type: Content-Type must be application/json",
|
||||||
))
|
))
|
||||||
.boxed_unsync(),
|
.boxed(),
|
||||||
)
|
)
|
||||||
.expect("valid response"));
|
.expect("valid response"));
|
||||||
}
|
}
|
||||||
|
|
@ -265,10 +265,7 @@ where
|
||||||
// unauthorized
|
// unauthorized
|
||||||
return Ok(Response::builder()
|
return Ok(Response::builder()
|
||||||
.status(http::StatusCode::UNAUTHORIZED)
|
.status(http::StatusCode::UNAUTHORIZED)
|
||||||
.body(
|
.body(Full::new(Bytes::from("Unauthorized: Session not found")).boxed())
|
||||||
Full::new(Bytes::from("Unauthorized: Session not found"))
|
|
||||||
.boxed_unsync(),
|
|
||||||
)
|
|
||||||
.expect("valid response"));
|
.expect("valid response"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -307,8 +304,7 @@ where
|
||||||
_ => Ok(Response::builder()
|
_ => Ok(Response::builder()
|
||||||
.status(http::StatusCode::NOT_IMPLEMENTED)
|
.status(http::StatusCode::NOT_IMPLEMENTED)
|
||||||
.body(
|
.body(
|
||||||
Full::new(Bytes::from("Batch requests are not supported yet"))
|
Full::new(Bytes::from("Batch requests are not supported yet")).boxed(),
|
||||||
.boxed_unsync(),
|
|
||||||
)
|
)
|
||||||
.expect("valid response")),
|
.expect("valid response")),
|
||||||
}
|
}
|
||||||
|
|
@ -415,10 +411,7 @@ where
|
||||||
ClientJsonRpcMessage::Error(_json_rpc_error) => Ok(accepted_response()),
|
ClientJsonRpcMessage::Error(_json_rpc_error) => Ok(accepted_response()),
|
||||||
_ => Ok(Response::builder()
|
_ => Ok(Response::builder()
|
||||||
.status(http::StatusCode::NOT_IMPLEMENTED)
|
.status(http::StatusCode::NOT_IMPLEMENTED)
|
||||||
.body(
|
.body(Full::new(Bytes::from("Batch requests are not supported yet")).boxed())
|
||||||
Full::new(Bytes::from("Batch requests are not supported yet"))
|
|
||||||
.boxed_unsync(),
|
|
||||||
)
|
|
||||||
.expect("valid response")),
|
.expect("valid response")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -439,7 +432,7 @@ where
|
||||||
// unauthorized
|
// unauthorized
|
||||||
return Ok(Response::builder()
|
return Ok(Response::builder()
|
||||||
.status(http::StatusCode::UNAUTHORIZED)
|
.status(http::StatusCode::UNAUTHORIZED)
|
||||||
.body(Full::new(Bytes::from("Unauthorized: Session ID is required")).boxed_unsync())
|
.body(Full::new(Bytes::from("Unauthorized: Session ID is required")).boxed())
|
||||||
.expect("valid response"));
|
.expect("valid response"));
|
||||||
};
|
};
|
||||||
// close session
|
// close session
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue