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
impl StreamingResponse
Sourcepub fn new(status: u16) -> (Self, BodyWriter)
pub fn new(status: u16) -> (Self, BodyWriter)
status の chunked ストリーミング応答と、データ送出用の BodyWriter を
既定容量(DEFAULT_CHANNEL_CAPACITY)で組み立てる。
Content-Type を付けたい場合は Self::channel を使う。
Sourcepub fn channel(
status: u16,
content_type: Option<&'static str>,
capacity: usize,
) -> (Self, BodyWriter)
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();
});