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 http::Response;
|
||||
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 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(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()
|
||||
.status(http::StatusCode::ACCEPTED)
|
||||
.body(Empty::new().boxed_unsync())
|
||||
.body(Empty::new().boxed())
|
||||
.expect("valid response")
|
||||
}
|
||||
pin_project_lite::pin_project! {
|
||||
|
|
@ -63,9 +63,9 @@ pub struct ServerSseMessage {
|
|||
}
|
||||
|
||||
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>,
|
||||
) -> Response<UnsyncBoxBody<Bytes, Infallible>> {
|
||||
) -> Response<BoxBody<Bytes, Infallible>> {
|
||||
use futures::StreamExt;
|
||||
let stream = SseBody::new(stream.map(|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 {
|
||||
Some(duration) => stream
|
||||
.with_keep_alive::<TokioTimer>(KeepAlive::new().interval(duration))
|
||||
.boxed_unsync(),
|
||||
None => stream.boxed_unsync(),
|
||||
.boxed(),
|
||||
None => stream.boxed(),
|
||||
};
|
||||
Response::builder()
|
||||
.status(http::StatusCode::OK)
|
||||
|
|
@ -89,7 +89,7 @@ pub(crate) fn sse_stream_response(
|
|||
|
||||
pub(crate) const fn internal_error_response<E: Display>(
|
||||
context: &str,
|
||||
) -> impl FnOnce(E) -> Response<UnsyncBoxBody<Bytes, Infallible>> {
|
||||
) -> impl FnOnce(E) -> Response<BoxBody<Bytes, Infallible>> {
|
||||
move |error| {
|
||||
tracing::error!("Internal server error when {context}: {error}");
|
||||
Response::builder()
|
||||
|
|
@ -98,24 +98,22 @@ pub(crate) const fn internal_error_response<E: Display>(
|
|||
Full::new(Bytes::from(format!(
|
||||
"Encounter an error when {context}: {error}"
|
||||
)))
|
||||
.boxed_unsync(),
|
||||
.boxed(),
|
||||
)
|
||||
.expect("valid response")
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn unexpected_message_response(
|
||||
expect: &str,
|
||||
) -> Response<UnsyncBoxBody<Bytes, Infallible>> {
|
||||
pub(crate) fn unexpected_message_response(expect: &str) -> Response<BoxBody<Bytes, Infallible>> {
|
||||
Response::builder()
|
||||
.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")
|
||||
}
|
||||
|
||||
pub(crate) async fn expect_json<B>(
|
||||
body: B,
|
||||
) -> Result<ClientJsonRpcMessage, Response<UnsyncBoxBody<Bytes, Infallible>>>
|
||||
) -> Result<ClientJsonRpcMessage, Response<BoxBody<Bytes, Infallible>>>
|
||||
where
|
||||
B: Body + Send + 'static,
|
||||
B::Error: Display,
|
||||
|
|
@ -129,7 +127,7 @@ where
|
|||
.status(http::StatusCode::UNSUPPORTED_MEDIA_TYPE)
|
||||
.body(
|
||||
Full::new(Bytes::from(format!("fail to deserialize request body {e}")))
|
||||
.boxed_unsync(),
|
||||
.boxed(),
|
||||
)
|
||||
.expect("valid response");
|
||||
Err(response)
|
||||
|
|
@ -139,10 +137,7 @@ where
|
|||
Err(e) => {
|
||||
let response = Response::builder()
|
||||
.status(http::StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(
|
||||
Full::new(Bytes::from(format!("Failed to read request body: {e}")))
|
||||
.boxed_unsync(),
|
||||
)
|
||||
.body(Full::new(Bytes::from(format!("Failed to read request body: {e}"))).boxed())
|
||||
.expect("valid response");
|
||||
Err(response)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ pub trait SessionManager: Send + Sync + 'static {
|
|||
id: &SessionId,
|
||||
message: ClientJsonRpcMessage,
|
||||
) -> impl Future<
|
||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + 'static, Self::Error>,
|
||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>,
|
||||
> + Send;
|
||||
fn accept_message(
|
||||
&self,
|
||||
|
|
@ -42,13 +42,13 @@ pub trait SessionManager: Send + Sync + 'static {
|
|||
&self,
|
||||
id: &SessionId,
|
||||
) -> impl Future<
|
||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + 'static, Self::Error>,
|
||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>,
|
||||
> + Send;
|
||||
fn resume(
|
||||
&self,
|
||||
id: &SessionId,
|
||||
last_event_id: String,
|
||||
) -> impl Future<
|
||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + 'static, Self::Error>,
|
||||
Output = Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>,
|
||||
> + Send;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use bytes::Bytes;
|
|||
use futures::{StreamExt, future::BoxFuture};
|
||||
use http::{Method, Request, Response, header::ALLOW};
|
||||
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 super::session::SessionManager;
|
||||
|
|
@ -105,7 +105,7 @@ where
|
|||
fn get_service(&self) -> Result<S, std::io::Error> {
|
||||
(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
|
||||
B: Body + Send + 'static,
|
||||
B::Error: Display,
|
||||
|
|
@ -120,7 +120,7 @@ where
|
|||
let response = Response::builder()
|
||||
.status(http::StatusCode::METHOD_NOT_ALLOWED)
|
||||
.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");
|
||||
return response;
|
||||
}
|
||||
|
|
@ -148,7 +148,7 @@ where
|
|||
Full::new(Bytes::from(
|
||||
"Not Acceptable: Client must accept text/event-stream",
|
||||
))
|
||||
.boxed_unsync(),
|
||||
.boxed(),
|
||||
)
|
||||
.expect("valid response"));
|
||||
}
|
||||
|
|
@ -162,7 +162,7 @@ where
|
|||
// unauthorized
|
||||
return Ok(Response::builder()
|
||||
.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"));
|
||||
};
|
||||
// check if session exists
|
||||
|
|
@ -175,7 +175,7 @@ where
|
|||
// unauthorized
|
||||
return Ok(Response::builder()
|
||||
.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"));
|
||||
}
|
||||
// check if last event id is provided
|
||||
|
|
@ -219,7 +219,7 @@ where
|
|||
{
|
||||
return Ok(Response::builder()
|
||||
.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"));
|
||||
}
|
||||
|
||||
|
|
@ -236,7 +236,7 @@ where
|
|||
Full::new(Bytes::from(
|
||||
"Unsupported Media Type: Content-Type must be application/json",
|
||||
))
|
||||
.boxed_unsync(),
|
||||
.boxed(),
|
||||
)
|
||||
.expect("valid response"));
|
||||
}
|
||||
|
|
@ -265,10 +265,7 @@ where
|
|||
// unauthorized
|
||||
return Ok(Response::builder()
|
||||
.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"));
|
||||
}
|
||||
|
||||
|
|
@ -307,8 +304,7 @@ where
|
|||
_ => Ok(Response::builder()
|
||||
.status(http::StatusCode::NOT_IMPLEMENTED)
|
||||
.body(
|
||||
Full::new(Bytes::from("Batch requests are not supported yet"))
|
||||
.boxed_unsync(),
|
||||
Full::new(Bytes::from("Batch requests are not supported yet")).boxed(),
|
||||
)
|
||||
.expect("valid response")),
|
||||
}
|
||||
|
|
@ -415,10 +411,7 @@ where
|
|||
ClientJsonRpcMessage::Error(_json_rpc_error) => Ok(accepted_response()),
|
||||
_ => Ok(Response::builder()
|
||||
.status(http::StatusCode::NOT_IMPLEMENTED)
|
||||
.body(
|
||||
Full::new(Bytes::from("Batch requests are not supported yet"))
|
||||
.boxed_unsync(),
|
||||
)
|
||||
.body(Full::new(Bytes::from("Batch requests are not supported yet")).boxed())
|
||||
.expect("valid response")),
|
||||
}
|
||||
}
|
||||
|
|
@ -439,7 +432,7 @@ where
|
|||
// unauthorized
|
||||
return Ok(Response::builder()
|
||||
.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"));
|
||||
};
|
||||
// close session
|
||||
|
|
|
|||
Loading…
Reference in a new issue