feat: mark boxed http body as sync (#291)
This commit is contained in:
parent
1b3db8959c
commit
9ca20c69b2
1 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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue