From df0fff2f2c1fd284bba83d4622ced64e7f4e37db Mon Sep 17 00:00:00 2001 From: Jeremyhi Date: Wed, 26 Jun 2024 14:14:14 +0800 Subject: [PATCH] feat(servers): make http timeout and body limit optional (#4217) * feat(servers): make http timeout and body limit optional Signed-off-by: Ruihang Xia * add comment Signed-off-by: Ruihang Xia * chore: make config-docs --------- Signed-off-by: Ruihang Xia Co-authored-by: Ruihang Xia --- config/config.md | 8 ++++---- config/frontend.example.toml | 5 +++-- config/standalone.example.toml | 5 +++-- src/servers/src/http.rs | 26 ++++++++++++++++++-------- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/config/config.md b/config/config.md index 2955e38024..7ff0364ed5 100644 --- a/config/config.md +++ b/config/config.md @@ -19,8 +19,8 @@ | `runtime.bg_rt_size` | Integer | `4` | The number of threads to execute the runtime for global background operations. | | `http` | -- | -- | The HTTP server options. | | `http.addr` | String | `127.0.0.1:4000` | The address to bind the HTTP server. | -| `http.timeout` | String | `30s` | HTTP request timeout. | -| `http.body_limit` | String | `64MB` | HTTP request body limit.
Support the following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`. | +| `http.timeout` | String | `30s` | HTTP request timeout. Set to 0 to disable timeout. | +| `http.body_limit` | String | `64MB` | HTTP request body limit.
The following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`.
Set to 0 to disable limit. | | `grpc` | -- | -- | The gRPC server options. | | `grpc.addr` | String | `127.0.0.1:4001` | The address to bind the gRPC server. | | `grpc.runtime_size` | Integer | `8` | The number of server worker threads. | @@ -166,8 +166,8 @@ | `heartbeat.retry_interval` | String | `3s` | Interval for retrying to send heartbeat messages to the metasrv. | | `http` | -- | -- | The HTTP server options. | | `http.addr` | String | `127.0.0.1:4000` | The address to bind the HTTP server. | -| `http.timeout` | String | `30s` | HTTP request timeout. | -| `http.body_limit` | String | `64MB` | HTTP request body limit.
Support the following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`. | +| `http.timeout` | String | `30s` | HTTP request timeout. Set to 0 to disable timeout. | +| `http.body_limit` | String | `64MB` | HTTP request body limit.
The following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`.
Set to 0 to disable limit. | | `grpc` | -- | -- | The gRPC server options. | | `grpc.addr` | String | `127.0.0.1:4001` | The address to bind the gRPC server. | | `grpc.hostname` | String | `127.0.0.1` | The hostname advertised to the metasrv,
and used for connections from outside the host | diff --git a/config/frontend.example.toml b/config/frontend.example.toml index c9c1c28cf1..6c2bddbc52 100644 --- a/config/frontend.example.toml +++ b/config/frontend.example.toml @@ -26,10 +26,11 @@ retry_interval = "3s" [http] ## The address to bind the HTTP server. addr = "127.0.0.1:4000" -## HTTP request timeout. +## HTTP request timeout. Set to 0 to disable timeout. timeout = "30s" ## HTTP request body limit. -## Support the following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`. +## The following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`. +## Set to 0 to disable limit. body_limit = "64MB" ## The gRPC server options. diff --git a/config/standalone.example.toml b/config/standalone.example.toml index a4d384f2be..0a2544a772 100644 --- a/config/standalone.example.toml +++ b/config/standalone.example.toml @@ -21,10 +21,11 @@ bg_rt_size = 4 [http] ## The address to bind the HTTP server. addr = "127.0.0.1:4000" -## HTTP request timeout. +## HTTP request timeout. Set to 0 to disable timeout. timeout = "30s" ## HTTP request body limit. -## Support the following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`. +## The following units are supported: `B`, `KB`, `KiB`, `MB`, `MiB`, `GB`, `GiB`, `TB`, `TiB`, `PB`, `PiB`. +## Set to 0 to disable limit. body_limit = "64MB" ## The gRPC server options. diff --git a/src/servers/src/http.rs b/src/servers/src/http.rs index e8465a6649..26e0349de2 100644 --- a/src/servers/src/http.rs +++ b/src/servers/src/http.rs @@ -670,20 +670,30 @@ impl HttpServer { } pub fn build(&self, router: Router) -> Router { + let timeout_layer = if self.options.timeout != Duration::default() { + Some(ServiceBuilder::new().layer(TimeoutLayer::new(self.options.timeout))) + } else { + info!("HTTP server timeout is disabled"); + None + }; + let body_limit_layer = if self.options.body_limit != ReadableSize(0) { + Some( + ServiceBuilder::new() + .layer(DefaultBodyLimit::max(self.options.body_limit.0 as usize)), + ) + } else { + info!("HTTP server body limit is disabled"); + None + }; + router // middlewares .layer( ServiceBuilder::new() .layer(HandleErrorLayer::new(handle_error)) .layer(TraceLayer::new_for_http()) - .layer(TimeoutLayer::new(self.options.timeout)) - .layer(DefaultBodyLimit::max( - self.options - .body_limit - .0 - .try_into() - .unwrap_or_else(|_| DEFAULT_BODY_LIMIT.as_bytes() as usize), - )) + .option_layer(timeout_layer) + .option_layer(body_limit_layer) // auth layer .layer(middleware::from_fn_with_state( AuthState::new(self.user_provider.clone()),