Skip to content

Commit

Permalink
make http max body size configurable and modify default value to 64kb.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachelint committed Dec 6, 2022
1 parent d61a79c commit 0f0159e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
3 changes: 3 additions & 0 deletions server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde_derive::Deserialize;
use table_engine::ANALYTIC_ENGINE_TYPE;

use crate::{
http::DEFAULT_MAX_BODY_SIZE,
limiter::LimiterConfig,
route::rule_based::{ClusterView, RuleList},
};
Expand Down Expand Up @@ -176,6 +177,7 @@ pub struct Config {
pub bind_addr: String,
pub mysql_port: u16,
pub http_port: u16,
pub http_max_body_size: u64,
pub grpc_port: u16,
pub grpc_server_cq_count: usize,

Expand Down Expand Up @@ -226,6 +228,7 @@ impl Default for Config {
Self {
bind_addr: String::from("127.0.0.1"),
http_port: 5000,
http_max_body_size: DEFAULT_MAX_BODY_SIZE,
mysql_port: 3307,
grpc_port,
grpc_server_cq_count: 20,
Expand Down
31 changes: 21 additions & 10 deletions server/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ define_result!(Error);

impl reject::Reject for Error {}

const MAX_BODY_SIZE: u64 = 4096;
pub const DEFAULT_MAX_BODY_SIZE: u64 = 64 * 1024;

/// Http service
///
Expand All @@ -100,6 +100,7 @@ pub struct Service<Q> {
instance: InstanceRef<Q>,
profiler: Arc<Profiler>,
tx: Sender<()>,
config: HttpConfig,
}

impl<Q> Service<Q> {
Expand Down Expand Up @@ -137,7 +138,7 @@ impl<Q: QueryExecutor + 'static> Service<Q> {

warp::path!("sql")
.and(warp::post())
.and(warp::body::content_length_limit(MAX_BODY_SIZE))
.and(warp::body::content_length_limit(self.config.max_body_size))
.and(extract_request)
.and(self.with_context())
.and(self.with_instance())
Expand Down Expand Up @@ -339,16 +340,16 @@ impl<Q: QueryExecutor + 'static> Service<Q> {

/// Service builder
pub struct Builder<Q> {
endpoint: Endpoint,
config: HttpConfig,
engine_runtimes: Option<Arc<EngineRuntimes>>,
log_runtime: Option<Arc<RuntimeLevel>>,
instance: Option<InstanceRef<Q>>,
}

impl<Q> Builder<Q> {
pub fn new(endpoint: Endpoint) -> Self {
pub fn new(config: HttpConfig) -> Self {
Self {
endpoint,
config,
engine_runtimes: None,
log_runtime: None,
instance: None,
Expand Down Expand Up @@ -385,25 +386,35 @@ impl<Q: QueryExecutor + 'static> Builder<Q> {
instance,
profiler: Arc::new(Profiler::default()),
tx,
config: self.config.clone(),
};

let ip_addr: IpAddr = self.endpoint.addr.parse().context(ParseIpAddr {
ip: self.endpoint.addr,
let ip_addr: IpAddr = self.config.endpoint.addr.parse().context(ParseIpAddr {
ip: self.config.endpoint.addr,
})?;

// Register filters to warp and rejection handler
let routes = service.routes().recover(handle_rejection);
let (_addr, server) =
warp::serve(routes).bind_with_graceful_shutdown((ip_addr, self.endpoint.port), async {
let (_addr, server) = warp::serve(routes).bind_with_graceful_shutdown(
(ip_addr, self.config.endpoint.port),
async {
rx.await.ok();
});
},
);
// Run the service
engine_runtime.bg_runtime.spawn(server);

Ok(service)
}
}

/// Http service config
#[derive(Debug, Clone)]
pub struct HttpConfig {
pub endpoint: Endpoint,
pub max_body_size: u64,
}

#[derive(Debug, Serialize)]
struct ErrorResponse {
code: u16,
Expand Down
9 changes: 7 additions & 2 deletions server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use table_engine::engine::{EngineRuntimes, TableEngineRef};
use crate::{
config::{Config, Endpoint},
grpc::{self, RpcServices},
http::{self, Service},
http::{self, HttpConfig, Service},
instance::{Instance, InstanceRef},
limiter::Limiter,
mysql,
Expand Down Expand Up @@ -255,11 +255,16 @@ impl<Q: QueryExecutor + 'static> Builder<Q> {
};

// Create http config
let http_config = Endpoint {
let endpoint = Endpoint {
addr: self.config.bind_addr.clone(),
port: self.config.http_port,
};

let http_config = HttpConfig {
endpoint,
max_body_size: self.config.http_max_body_size,
};

// Start http service
let engine_runtimes = self.engine_runtimes.context(MissingEngineRuntimes)?;
let log_runtime = self.log_runtime.context(MissingLogRuntime)?;
Expand Down

0 comments on commit 0f0159e

Please sign in to comment.