fix(stream): add anti-buffer headers for real SSE streaming
Notify Parent Repo / dispatch (push) Canceled after 0s
Notify Parent Repo / dispatch (push) Canceled after 0s
- Add X-Accel-Buffering: no, Cache-Control: no-cache, Connection: keep-alive - Prevents Traefik/proxy from buffering SSE events - Each token is flushed immediately as generated
This commit is contained in:
@@ -5,6 +5,7 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::response::sse::{Event, KeepAlive, Sse};
|
use axum::response::sse::{Event, KeepAlive, Sse};
|
||||||
|
use axum::http::HeaderMap;
|
||||||
use axum::{Json, response::{IntoResponse, Response}};
|
use axum::{Json, response::{IntoResponse, Response}};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
@@ -435,6 +436,10 @@ async fn handle_streaming(
|
|||||||
});
|
});
|
||||||
|
|
||||||
let stream = ReceiverStream::new(rx);
|
let stream = ReceiverStream::new(rx);
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert("X-Accel-Buffering", "no".parse().unwrap());
|
||||||
|
headers.insert("Cache-Control", "no-cache".parse().unwrap());
|
||||||
|
headers.insert("Connection", "keep-alive".parse().unwrap());
|
||||||
let sse = Sse::new(stream).keep_alive(KeepAlive::default());
|
let sse = Sse::new(stream).keep_alive(KeepAlive::default());
|
||||||
Ok(sse.into_response())
|
Ok((headers, sse).into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user