Skip to content

Commit 8f7fe1c

Browse files
vproxy: add icap tcp connect timeout
1 parent 649cee6 commit 8f7fe1c

7 files changed

Lines changed: 36 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/vey-icap-client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ vey-http.workspace = true
3131
vey-h2.workspace = true
3232
vey-smtp-proto.workspace = true
3333
vey-yaml = { workspace = true, optional = true, features = ["rustls", "http"] }
34+
log.workspace = true
3435

3536
[features]
3637
default = []

lib/vey-icap-client/src/service/config/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct IcapServiceConfig {
3434
pub(crate) tls_name: ServerName<'static>,
3535
pub(crate) connection_pool: ConnectionPoolConfig,
3636
pub(crate) tcp_keepalive: TcpKeepAliveConfig,
37+
pub(crate) tcp_connect_timeout: Duration,
3738
#[cfg(unix)]
3839
pub(crate) use_unix_socket: Option<PathBuf>,
3940
pub(crate) icap_206_enable: bool,
@@ -75,6 +76,7 @@ impl IcapServiceConfig {
7576
tls_name,
7677
connection_pool: ConnectionPoolConfig::default(),
7778
tcp_keepalive: TcpKeepAliveConfig::default_enabled(),
79+
tcp_connect_timeout: Duration::from_secs(1),
7880
#[cfg(unix)]
7981
use_unix_socket: None,
8082
icap_206_enable: false,
@@ -90,6 +92,10 @@ impl IcapServiceConfig {
9092
self.tcp_keepalive = config;
9193
}
9294

95+
pub fn set_tcp_connect_timeout(&mut self, time: Duration) {
96+
self.tcp_connect_timeout = time;
97+
}
98+
9399
pub fn set_tls_client(&mut self, config: RustlsClientConfigBuilder) {
94100
self.tls_client = Some(config);
95101
}

lib/vey-icap-client/src/service/config/yaml.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ impl IcapServiceConfig {
4646
config.set_tcp_keepalive(keepalive);
4747
Ok(())
4848
}
49+
"tcp_connect_timeout" => {
50+
let connect_timeout = vey_yaml::humanize::as_duration(v)
51+
.context(format!("invalid humanize duration value for key {k}"))?;
52+
config.set_tcp_connect_timeout(connect_timeout);
53+
Ok(())
54+
}
4955
#[cfg(unix)]
5056
"use_unix_socket" => {
5157
let path = vey_yaml::value::as_absolute_path(v)

lib/vey-icap-client/src/service/connection.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,17 @@ impl IcapConnector {
117117
&Default::default(),
118118
true,
119119
)?;
120-
let stream = socket.connect(peer).await?;
120+
121+
let stream =
122+
match tokio::time::timeout(self.config.tcp_connect_timeout, socket.connect(peer)).await
123+
{
124+
Ok(Ok(s)) => Ok(s),
125+
Ok(Err(e)) => Err(e),
126+
Err(_) => Err(io::Error::new(
127+
io::ErrorKind::TimedOut,
128+
"tcp connection with ICAP server timed out",
129+
)),
130+
}?;
121131

122132
if let Some(client) = &self.tls_client {
123133
let tls_connector = TlsConnector::from(client.driver.clone());

lib/vey-icap-client/src/service/pool.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ impl IcapServicePool {
4848
connector: Arc<IcapConnector>,
4949
) -> Self {
5050
let options = Arc::new(IcapServiceOptions::new_expired(config.method));
51-
let check_interval = tokio::time::interval(config.connection_pool.check_interval());
51+
let mut check_interval = tokio::time::interval(config.connection_pool.check_interval());
52+
check_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
53+
5254
let (pool_cmd_sender, pool_cmd_receiver) = mpsc::channel(POOL_CMD_CHANNEL_SIZE);
5355
let (conn_req_sender, conn_req_receiver) =
5456
kanal::bounded_async(config.connection_pool.max_idle_count());

sphinx/vey-values/configuration/values/audit.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ If the value is a map, the following keys are supported:
8686

8787
**default**: enabled with default value
8888

89+
* tcp_connect_timeout
90+
91+
**optional**, **type**: :ref:`humanize duration <conf_value_humanize_duration>`
92+
93+
TCP connection timeout configuration for establishing connection to the ICAP server.
94+
95+
**default**: 1s
96+
8997
* icap_connection_pool
9098

9199
**optional**, **type**: :ref:`connection pool <conf_value_connection_pool_config>`

0 commit comments

Comments
 (0)