Skip to content

Commit

Permalink
g3proxy: change default idle check interval and max count value
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq committed Feb 14, 2025
1 parent a393f2f commit a76bda6
Show file tree
Hide file tree
Showing 32 changed files with 180 additions and 144 deletions.
12 changes: 11 additions & 1 deletion g3proxy/src/audit/detour/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use g3_io_ext::IdleWheel;
use g3_types::net::UpstreamAddr;

use crate::config::audit::AuditStreamDetourConfig;
use crate::config::server::ServerConfig;
use crate::inspect::StreamInspectTaskNotes;

mod connect;
Expand Down Expand Up @@ -64,6 +65,7 @@ pub(crate) struct StreamDetourContext<'a, SC> {
protocol: Protocol,
payload: Vec<u8>,
request_timeout: Duration,
max_idle_count: usize,
}

impl<SC> StreamDetourContext<'_, SC> {
Expand Down Expand Up @@ -99,7 +101,14 @@ impl StreamDetourClient {
task_notes: &'a StreamInspectTaskNotes,
upstream: &'a UpstreamAddr,
protocol: Protocol,
) -> StreamDetourContext<'a, SC> {
) -> StreamDetourContext<'a, SC>
where
SC: ServerConfig,
{
let max_idle_count = task_notes
.user()
.and_then(|u| u.task_max_idle_count())
.unwrap_or(server_config.task_max_idle_count());
StreamDetourContext {
server_config,
server_quit_policy,
Expand All @@ -109,6 +118,7 @@ impl StreamDetourClient {
protocol,
payload: Vec::new(),
request_timeout: self.config.request_timeout,
max_idle_count,
}
}

Expand Down
9 changes: 3 additions & 6 deletions g3proxy/src/audit/detour/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,13 @@ where
if clt_to_d.is_idle() && d_to_clt.is_idle() && ups_to_d.is_idle() && d_to_ups.is_idle() {
idle_count += n;

let quit = if let Some(user) = self.task_notes.user() {
if let Some(user) = self.task_notes.user() {
if user.is_blocked() {
return Err(ServerTaskError::CanceledAsUserBlocked);
}
idle_count >= user.task_max_idle_count()
} else {
idle_count >= self.server_config.task_max_idle_count()
};
}

if quit {
if idle_count >= self.max_idle_count {
return Err(ServerTaskError::Idle(idle_interval.period(), idle_count));
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion g3proxy/src/auth/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) struct User {

impl User {
#[inline]
pub(crate) fn task_max_idle_count(&self) -> usize {
pub(crate) fn task_max_idle_count(&self) -> Option<usize> {
self.config.task_idle_max_count
}

Expand Down
3 changes: 2 additions & 1 deletion g3proxy/src/config/auth/user/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ impl UserConfig {
Ok(())
}
"task_idle_max_count" => {
self.task_idle_max_count = g3_json::value::as_usize(v)
let count = g3_json::value::as_usize(v)
.context(format!("invalid usize value for key {k}"))?;
self.task_idle_max_count = Some(count);
Ok(())
}
"socks_use_udp_associate" => {
Expand Down
4 changes: 2 additions & 2 deletions g3proxy/src/config/auth/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) struct UserConfig {
pub(crate) http_user_agent_filter: Option<AclUserAgentRule>,
pub(crate) resolve_strategy: Option<ResolveStrategy>,
pub(crate) resolve_redirection: Option<ResolveRedirectionBuilder>,
pub(crate) task_idle_max_count: usize,
pub(crate) task_idle_max_count: Option<usize>,
pub(crate) socks_use_udp_associate: bool,
pub(crate) egress_path_selection: Option<EgressPathSelection>,
pub(crate) explicit_sites: BTreeMap<NodeName, Arc<UserSiteConfig>>,
Expand Down Expand Up @@ -114,7 +114,7 @@ impl Default for UserConfig {
http_user_agent_filter: None,
resolve_strategy: None,
resolve_redirection: None,
task_idle_max_count: 1,
task_idle_max_count: None,
socks_use_udp_associate: false,
egress_path_selection: None,
explicit_sites: BTreeMap::new(),
Expand Down
3 changes: 2 additions & 1 deletion g3proxy/src/config/auth/user/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ impl UserConfig {
Ok(())
}
"task_idle_max_count" => {
self.task_idle_max_count = g3_yaml::value::as_usize(v)
let count = g3_yaml::value::as_usize(v)
.context(format!("invalid usize value for key {k}"))?;
self.task_idle_max_count = Some(count);
Ok(())
}
"socks_use_udp_associate" => {
Expand Down
4 changes: 2 additions & 2 deletions g3proxy/src/config/server/http_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use g3_yaml::YamlDocPosition;

use super::{
AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_DEFAULT_DURATION,
IDLE_CHECK_MAXIMUM_DURATION,
IDLE_CHECK_DEFAULT_MAX_COUNT, IDLE_CHECK_MAXIMUM_DURATION,
};

const SERVER_CONFIG_TYPE: &str = "HttpProxy";
Expand Down Expand Up @@ -129,7 +129,7 @@ impl HttpProxyServerConfig {
tcp_sock_speed_limit: TcpSockSpeedLimitConfig::default(),
timeout: HttpProxyServerTimeoutConfig::default(),
task_idle_check_duration: IDLE_CHECK_DEFAULT_DURATION,
task_idle_max_count: 1,
task_idle_max_count: IDLE_CHECK_DEFAULT_MAX_COUNT,
flush_task_log_on_created: false,
flush_task_log_on_connected: false,
task_log_flush_interval: None,
Expand Down
4 changes: 2 additions & 2 deletions g3proxy/src/config/server/http_rproxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use g3_yaml::YamlDocPosition;

use super::{
AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_DEFAULT_DURATION,
IDLE_CHECK_MAXIMUM_DURATION,
IDLE_CHECK_DEFAULT_MAX_COUNT, IDLE_CHECK_MAXIMUM_DURATION,
};

mod host;
Expand Down Expand Up @@ -116,7 +116,7 @@ impl HttpRProxyServerConfig {
tcp_sock_speed_limit: TcpSockSpeedLimitConfig::default(),
timeout: HttpRProxyServerTimeoutConfig::default(),
task_idle_check_duration: IDLE_CHECK_DEFAULT_DURATION,
task_idle_max_count: 1,
task_idle_max_count: IDLE_CHECK_DEFAULT_MAX_COUNT,
flush_task_log_on_created: false,
flush_task_log_on_connected: false,
task_log_flush_interval: None,
Expand Down
3 changes: 2 additions & 1 deletion g3proxy/src/config/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ const CONFIG_KEY_SERVER_TYPE: &str = "type";
const CONFIG_KEY_SERVER_NAME: &str = "name";

const IDLE_CHECK_MAXIMUM_DURATION: Duration = Duration::from_secs(1800);
const IDLE_CHECK_DEFAULT_DURATION: Duration = Duration::from_secs(300);
const IDLE_CHECK_DEFAULT_DURATION: Duration = Duration::from_secs(60);
const IDLE_CHECK_DEFAULT_MAX_COUNT: usize = 5;

pub(crate) enum ServerConfigDiffAction {
NoAction,
Expand Down
9 changes: 6 additions & 3 deletions g3proxy/src/config/server/sni_proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use g3_types::net::{TcpListenConfig, TcpMiscSockOpts, TcpSockSpeedLimitConfig};
use g3_types::route::HostMatch;
use g3_yaml::YamlDocPosition;

use super::{AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_MAXIMUM_DURATION};
use super::{
AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_DEFAULT_DURATION,
IDLE_CHECK_DEFAULT_MAX_COUNT, IDLE_CHECK_MAXIMUM_DURATION,
};

mod host;
pub(crate) use host::SniHostConfig;
Expand Down Expand Up @@ -76,8 +79,8 @@ impl SniProxyServerConfig {
listen_in_worker: false,
ingress_net_filter: None,
tcp_sock_speed_limit: TcpSockSpeedLimitConfig::default(),
task_idle_check_duration: Duration::from_secs(300),
task_idle_max_count: 1,
task_idle_check_duration: IDLE_CHECK_DEFAULT_DURATION,
task_idle_max_count: IDLE_CHECK_DEFAULT_MAX_COUNT,
flush_task_log_on_created: false,
flush_task_log_on_connected: false,
task_log_flush_interval: None,
Expand Down
4 changes: 2 additions & 2 deletions g3proxy/src/config/server/socks_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use g3_yaml::YamlDocPosition;

use super::{
AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_DEFAULT_DURATION,
IDLE_CHECK_MAXIMUM_DURATION,
IDLE_CHECK_DEFAULT_MAX_COUNT, IDLE_CHECK_MAXIMUM_DURATION,
};

const SERVER_CONFIG_TYPE: &str = "SocksProxy";
Expand Down Expand Up @@ -116,7 +116,7 @@ impl SocksProxyServerConfig {
udp_sock_speed_limit: UdpSockSpeedLimitConfig::default(),
timeout: SocksProxyServerTimeoutConfig::default(),
task_idle_check_duration: IDLE_CHECK_DEFAULT_DURATION,
task_idle_max_count: 1,
task_idle_max_count: IDLE_CHECK_DEFAULT_MAX_COUNT,
flush_task_log_on_created: false,
flush_task_log_on_connected: false,
task_log_flush_interval: None,
Expand Down
9 changes: 6 additions & 3 deletions g3proxy/src/config/server/tcp_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use g3_types::net::{
};
use g3_yaml::YamlDocPosition;

use super::{AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_MAXIMUM_DURATION};
use super::{
AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_DEFAULT_DURATION,
IDLE_CHECK_DEFAULT_MAX_COUNT, IDLE_CHECK_MAXIMUM_DURATION,
};

const SERVER_CONFIG_TYPE: &str = "TcpStream";

Expand Down Expand Up @@ -76,8 +79,8 @@ impl TcpStreamServerConfig {
upstream_pick_policy: SelectivePickPolicy::Random,
upstream_tls_name: None,
tcp_sock_speed_limit: TcpSockSpeedLimitConfig::default(),
task_idle_check_duration: Duration::from_secs(300),
task_idle_max_count: 1,
task_idle_check_duration: IDLE_CHECK_DEFAULT_DURATION,
task_idle_max_count: IDLE_CHECK_DEFAULT_MAX_COUNT,
flush_task_log_on_created: false,
flush_task_log_on_connected: false,
task_log_flush_interval: None,
Expand Down
9 changes: 6 additions & 3 deletions g3proxy/src/config/server/tcp_tproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use g3_types::metrics::{NodeName, StaticMetricsTags};
use g3_types::net::{TcpListenConfig, TcpMiscSockOpts, TcpSockSpeedLimitConfig};
use g3_yaml::YamlDocPosition;

use super::{AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_MAXIMUM_DURATION};
use super::{
AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_DEFAULT_DURATION,
IDLE_CHECK_DEFAULT_MAX_COUNT, IDLE_CHECK_MAXIMUM_DURATION,
};

const SERVER_CONFIG_TYPE: &str = "TcpTProxy";

Expand Down Expand Up @@ -64,8 +67,8 @@ impl TcpTProxyServerConfig {
listen_in_worker: false,
ingress_net_filter: None,
tcp_sock_speed_limit: TcpSockSpeedLimitConfig::default(),
task_idle_check_duration: Duration::from_secs(300),
task_idle_max_count: 1,
task_idle_check_duration: IDLE_CHECK_DEFAULT_DURATION,
task_idle_max_count: IDLE_CHECK_DEFAULT_MAX_COUNT,
flush_task_log_on_created: false,
flush_task_log_on_connected: false,
task_log_flush_interval: None,
Expand Down
9 changes: 6 additions & 3 deletions g3proxy/src/config/server/tls_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use g3_types::net::{
};
use g3_yaml::YamlDocPosition;

use super::{AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_MAXIMUM_DURATION};
use super::{
AnyServerConfig, ServerConfig, ServerConfigDiffAction, IDLE_CHECK_DEFAULT_DURATION,
IDLE_CHECK_DEFAULT_MAX_COUNT, IDLE_CHECK_MAXIMUM_DURATION,
};

const SERVER_CONFIG_TYPE: &str = "TlsStream";

Expand Down Expand Up @@ -81,8 +84,8 @@ impl TlsStreamServerConfig {
upstream_pick_policy: SelectivePickPolicy::Random,
upstream_tls_name: None,
tcp_sock_speed_limit: TcpSockSpeedLimitConfig::default(),
task_idle_check_duration: Duration::from_secs(300),
task_idle_max_count: 1,
task_idle_check_duration: IDLE_CHECK_DEFAULT_DURATION,
task_idle_max_count: IDLE_CHECK_DEFAULT_MAX_COUNT,
flush_task_log_on_created: false,
flush_task_log_on_connected: false,
task_log_flush_interval: None,
Expand Down
3 changes: 1 addition & 2 deletions g3proxy/src/inspect/http/v1/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ where

let mut idle_interval = self.ctx.idle_wheel.get();
let mut idle_count = 0;
let max_idle_count = self.ctx.task_max_idle_count();

loop {
tokio::select! {
Expand All @@ -433,7 +432,7 @@ where
n = idle_interval.tick() => {
if ups_to_clt.is_idle() {
idle_count += n;
if idle_count >= max_idle_count {
if idle_count >= self.ctx.max_idle_count {
return if ups_to_clt.no_cached_data() {
Err(ServerTaskError::UpstreamAppTimeout("idle while reading response body"))
} else {
Expand Down
6 changes: 2 additions & 4 deletions g3proxy/src/inspect/http/v1/forward/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ impl<'a, SC: ServerConfig> H1ForwardTask<'a, SC> {

let mut idle_interval = self.ctx.idle_wheel.get();
let mut idle_count = 0;
let max_idle_count = self.ctx.task_max_idle_count();

loop {
tokio::select! {
Expand Down Expand Up @@ -549,7 +548,7 @@ impl<'a, SC: ServerConfig> H1ForwardTask<'a, SC> {
n = idle_interval.tick() => {
if clt_to_ups.is_idle() {
idle_count += n;
if idle_count >= max_idle_count {
if idle_count >= self.ctx.max_idle_count {
return if clt_to_ups.no_cached_data() {
Err(ServerTaskError::ClientAppTimeout("idle while reading request body"))
} else {
Expand Down Expand Up @@ -825,7 +824,6 @@ impl<'a, SC: ServerConfig> H1ForwardTask<'a, SC> {

let mut idle_interval = self.ctx.idle_wheel.get();
let mut idle_count = 0;
let max_idle_count = self.ctx.task_max_idle_count();

loop {
tokio::select! {
Expand All @@ -848,7 +846,7 @@ impl<'a, SC: ServerConfig> H1ForwardTask<'a, SC> {
n = idle_interval.tick() => {
if ups_to_clt.is_idle() {
idle_count += n;
if idle_count >= max_idle_count {
if idle_count >= self.ctx.max_idle_count {
return if ups_to_clt.no_cached_data() {
Err(ServerTaskError::UpstreamAppTimeout("idle while reading response body"))
} else {
Expand Down
3 changes: 1 addition & 2 deletions g3proxy/src/inspect/http/v1/upgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ where

let mut idle_interval = self.ctx.idle_wheel.get();
let mut idle_count = 0;
let max_idle_count = self.ctx.task_max_idle_count();

loop {
tokio::select! {
Expand All @@ -533,7 +532,7 @@ where
n = idle_interval.tick() => {
if ups_to_clt.is_idle() {
idle_count += n;
if idle_count >= max_idle_count {
if idle_count >= self.ctx.max_idle_count {
return if ups_to_clt.no_cached_data() {
Err(ServerTaskError::UpstreamAppTimeout("idle while reading response body"))
} else {
Expand Down
3 changes: 1 addition & 2 deletions g3proxy/src/inspect/http/v2/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ impl<'a, SC: ServerConfig> ExchangeHead<'a, SC> {

let mut idle_interval = self.ctx.idle_wheel.get();
let mut idle_count = 0;
let max_idle_count = self.ctx.task_max_idle_count();

loop {
tokio::select! {
Expand All @@ -429,7 +428,7 @@ impl<'a, SC: ServerConfig> ExchangeHead<'a, SC> {
if rsp_body_transfer.is_idle() {
idle_count += n;

if idle_count > max_idle_count {
if idle_count > self.ctx.max_idle_count {
return Err(H2StreamTransferError::Idle(idle_interval.period(), idle_count));
}
} else {
Expand Down
6 changes: 2 additions & 4 deletions g3proxy/src/inspect/http/v2/forward/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ where

let mut idle_interval = self.ctx.idle_wheel.get();
let mut idle_count = 0;
let max_idle_count = self.ctx.task_max_idle_count();

let mut ups_rsp: Option<Response<RecvStream>> = None;

Expand Down Expand Up @@ -497,7 +496,7 @@ where
if req_body_transfer.is_idle() {
idle_count += n;

if idle_count > max_idle_count {
if idle_count > self.ctx.max_idle_count {
return Err(H2StreamTransferError::Idle(idle_interval.period(), idle_count));
}
} else {
Expand Down Expand Up @@ -652,7 +651,6 @@ where

let mut idle_interval = self.ctx.idle_wheel.get();
let mut idle_count = 0;
let max_idle_count = self.ctx.task_max_idle_count();

loop {
tokio::select! {
Expand All @@ -671,7 +669,7 @@ where
if rsp_body_transfer.is_idle() {
idle_count += n;

if idle_count > max_idle_count {
if idle_count > self.ctx.max_idle_count {
return Err(H2StreamTransferError::Idle(idle_interval.period(), idle_count));
}
} else {
Expand Down
Loading

0 comments on commit a76bda6

Please sign in to comment.