Skip to main content

StreamingResponse

Struct StreamingResponse 

Source
pub struct StreamingResponse {
    pub status: u16,
    /* private fields */
}
Expand description

crate::server::Handler::handle_streaming が返すストリーミング応答。

ステータス・Content-Type のみを保持し、実データは StreamingResponse::channel で得られる BodyWriter 経由で producer タスクが逐次送る。fandhe_backend_http::response::Response とは異なり body を持たない(chunked framing はコアの書き出しループが直接組み立てる ため、ここでは中間表現としての Response を経由しない)。

Fields§

§status: u16

応答ステータスコード。書き出しループが fandhe_backend_http::response::Response::serialize_chunked_head へ そのまま渡す。

Implementations§

Source§

impl StreamingResponse

Source

pub fn new(status: u16) -> (Self, BodyWriter)

status の chunked ストリーミング応答と、データ送出用の BodyWriter を 既定容量(DEFAULT_CHANNEL_CAPACITY)で組み立てる。

Content-Type を付けたい場合は Self::channel を使う。

Source

pub fn channel( status: u16, content_type: Option<&'static str>, capacity: usize, ) -> (Self, BodyWriter)

status / content_type / 明示的な capacity(bounded mpsc の容量)で ストリーミング応答を組み立てる。

capacity が 0 の場合は 1 に切り上げる(tokio::sync::mpsc::channel は容量 0 を受け付けないため。バックプレッシャの効きを最も強くしたい 呼び出し元が 0 を渡しても panic させないフェイルセーフ)。

戻り値のタプル (StreamingResponse, BodyWriter) は、前者を Handler::handle_streaming の戻り値として返し、後者を tokio::spawn した producer タスクへ move して使う想定 (crate::server::Handler::handle_streaming の doc test を参照)。

use fandhe_backend_core::streaming::StreamingResponse;

let (response, writer) = StreamingResponse::channel(200, Some("text/plain"), 4);
assert_eq!(response.status, 200);

tokio::spawn(async move {
    writer.send(b"hello ".to_vec()).await.ok();
    writer.send(b"world".to_vec()).await.ok();
    writer.finish().await.ok();
});

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.