mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-10-12 06:52:34 +00:00
fix: honor HTTP timeout headers with no default deadline
Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
This commit is contained in:
+36
-11
@@ -1032,17 +1032,12 @@ impl HttpServer {
|
||||
/// Attaches middlewares and debug routes to the router.
|
||||
/// Callers should call this method after [HttpServer::make_app()].
|
||||
pub fn build(&self, router: Router) -> Result<Router> {
|
||||
let timeout_layer = if self.options.timeout != Duration::default() {
|
||||
Some(
|
||||
ServiceBuilder::new().layer(
|
||||
DynamicTimeoutLayer::new(self.options.timeout)
|
||||
.with_status_code_fn(Self::request_timeout_status_code),
|
||||
),
|
||||
)
|
||||
} else {
|
||||
info!("HTTP server timeout is disabled");
|
||||
None
|
||||
};
|
||||
let timeout_layer = Some(
|
||||
ServiceBuilder::new().layer(
|
||||
DynamicTimeoutLayer::new(self.options.timeout)
|
||||
.with_status_code_fn(Self::request_timeout_status_code),
|
||||
),
|
||||
);
|
||||
let body_limit_layer = if self.options.body_limit != ReadableSize(0) {
|
||||
Some(
|
||||
ServiceBuilder::new()
|
||||
@@ -2116,6 +2111,36 @@ mod test {
|
||||
assert_ne!(response.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_http_header_timeout_with_disabled_default() {
|
||||
let (tx, _rx) = mpsc::channel(100);
|
||||
let options = HttpOptions {
|
||||
timeout: Duration::ZERO,
|
||||
..Default::default()
|
||||
};
|
||||
let app = make_test_app_custom(tx, options);
|
||||
let client = TestClient::new(app).await;
|
||||
assert!(
|
||||
tokio::time::timeout(
|
||||
Duration::from_millis(10),
|
||||
client.get("/test/timeout").send()
|
||||
)
|
||||
.await
|
||||
.is_err(),
|
||||
"no header with a disabled global timeout should remain unlimited"
|
||||
);
|
||||
let response = tokio::time::timeout(
|
||||
Duration::from_secs(1),
|
||||
client
|
||||
.get("/test/timeout")
|
||||
.header(GREPTIME_DB_HEADER_TIMEOUT, "10ms")
|
||||
.send(),
|
||||
)
|
||||
.await
|
||||
.expect("request with header timeout should complete");
|
||||
assert_eq!(response.status(), StatusCode::REQUEST_TIMEOUT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_http_server_request_timeout() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
|
||||
Reference in New Issue
Block a user