Skip to content

Commit

Permalink
refactor: add timeout conf for server
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 6, 2024
1 parent 50f81a5 commit e7b8ebf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fn location_filter(c: &mut Criterion) {
Upstream::new(
upstream_name,
&UpstreamConf {
addrs: vec!["127.0.0.1:8001".to_string()],
..Default::default()
},
)
Expand Down Expand Up @@ -167,6 +168,7 @@ fn location_rewrite_path(c: &mut Criterion) {
Upstream::new(
upstream_name,
&UpstreamConf {
addrs: vec!["127.0.0.1:8001".to_string()],
..Default::default()
},
)
Expand Down
7 changes: 7 additions & 0 deletions conf/pingap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ threads = 1
# Allow work stealing between threads of the same service. Default `true`.
work_stealing = true

# Grace period in seconds before starting the final step of the graceful shutdown after signaling shutdown.
# Default `5m`.
grace_period = "3m"

# Timeout in seconds of the final step for the graceful shutdown. Default `5s`.
graceful_shutdown_timeout = "10s"


# Upsteam config list, it will defined as [upstreams.name]
[upstreams.charts]
Expand Down
14 changes: 14 additions & 0 deletions src/config/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ pub struct PingapConf {
pub threads: Option<usize>,
pub work_stealing: Option<bool>,
pub created_at: Option<String>,
#[serde(default)]
#[serde(with = "humantime_serde")]
pub grace_period: Option<Duration>,
#[serde(default)]
#[serde(with = "humantime_serde")]
pub graceful_shutdown_timeout: Option<Duration>,
}

impl PingapConf {
Expand Down Expand Up @@ -248,6 +254,12 @@ struct TomlConfig {
threads: Option<usize>,
work_stealing: Option<bool>,
created_at: Option<String>,
#[serde(default)]
#[serde(with = "humantime_serde")]
pub grace_period: Option<Duration>,
#[serde(default)]
#[serde(with = "humantime_serde")]
pub graceful_shutdown_timeout: Option<Duration>,
}

fn format_toml(value: &Value) -> String {
Expand Down Expand Up @@ -326,6 +338,8 @@ pub fn load_config(path: &str, admin: bool) -> Result<PingapConf> {
threads,
work_stealing: data.work_stealing,
created_at: data.created_at,
grace_period: data.grace_period,
graceful_shutdown_timeout: data.graceful_shutdown_timeout,
..Default::default()
};
for (name, value) in data.upstreams {
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ fn new_server_conf(args: &Args, conf: &PingapConf) -> server::configuration::Ser
error_log: args.log.clone(),
..Default::default()
};
if let Some(value) = conf.grace_period {
server_conf.grace_period_seconds = Some(value.as_secs());
}
if let Some(value) = conf.graceful_shutdown_timeout {
server_conf.graceful_shutdown_timeout_seconds = Some(value.as_secs());
}
if let Some(pid_file) = &conf.pid_file {
server_conf.pid_file = pid_file.to_string();
}
Expand Down

0 comments on commit e7b8ebf

Please sign in to comment.