Skip to content

Commit bf4bb0f

Browse files
authored
Merge pull request #410 from eval-exec/exec/onion_timeout
Add new struct `ServiceConfig{timeout, onion_timeout}` to `ServiceConfig`
2 parents 411ac84 + a68deef commit bf4bb0f

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

tentacle/src/builder.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,17 @@ where
8383

8484
/// Timeout for handshake and connect
8585
///
86-
/// Default 10 second
86+
/// Default timeout is 10 second
8787
pub fn timeout(mut self, timeout: Duration) -> Self {
88-
self.config.timeout = timeout;
88+
self.config.timeout.timeout = timeout;
89+
self
90+
}
91+
92+
/// Onion Timeout for handshake and connect
93+
///
94+
/// Default onion_timeout is 120 second
95+
pub fn onion_timeout(mut self, onion_timeout: Duration) -> Self {
96+
self.config.timeout.onion_timeout = onion_timeout;
8997
self
9098
}
9199

tentacle/src/service.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ where
170170
handshake_type,
171171
multi_transport: {
172172
#[cfg(target_family = "wasm")]
173-
let transport = MultiTransport::new(config.timeout);
173+
let transport = MultiTransport::new(config.timeout.timeout);
174174
#[allow(clippy::let_and_return)]
175175
#[cfg(not(target_family = "wasm"))]
176176
let transport = MultiTransport::new(config.timeout, config.tcp_config.clone());
@@ -319,7 +319,7 @@ where
319319
handshake_type: self.handshake_type.clone(),
320320
event_sender: self.session_event_sender.clone(),
321321
max_frame_length: self.config.max_frame_length,
322-
timeout: self.config.timeout,
322+
timeout: self.config.timeout.timeout,
323323
listen_addr: listen_address,
324324
future_task_sender: self.future_task_sender.clone(),
325325
listens_upgrade_modes: self.multi_transport.listens_upgrade_modes.clone(),
@@ -373,7 +373,7 @@ where
373373
self.dial_protocols.insert(address.clone(), target);
374374

375375
let handshake_type = self.handshake_type.clone();
376-
let timeout = self.config.timeout;
376+
let timeout = self.config.timeout.timeout;
377377
let max_frame_length = self.config.max_frame_length;
378378

379379
let mut sender = self.session_event_sender.clone();
@@ -553,7 +553,7 @@ where
553553
handshake_type: self.handshake_type.clone(),
554554
event_sender: self.session_event_sender.clone(),
555555
max_frame_length: self.config.max_frame_length,
556-
timeout: self.config.timeout,
556+
timeout: self.config.timeout.timeout,
557557
}
558558
.handshake(socket);
559559

@@ -692,7 +692,7 @@ where
692692
});
693693

694694
let meta = SessionMeta::new(
695-
self.config.timeout,
695+
self.config.timeout.timeout,
696696
session_context.clone(),
697697
service_event_sender,
698698
self.service_context.control().clone(),

tentacle/src/service/config.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,23 @@ use tokio_rustls::rustls::{ClientConfig, ServerConfig};
2020
/// Default max buffer size
2121
const MAX_BUF_SIZE: usize = 24 * 1024 * 1024;
2222

23-
pub(crate) struct ServiceConfig {
23+
#[derive(Clone, Copy)]
24+
pub(crate) struct ServiceTimeout {
2425
pub timeout: Duration,
26+
pub onion_timeout: Duration,
27+
}
28+
29+
impl Default for ServiceTimeout {
30+
fn default() -> Self {
31+
ServiceTimeout {
32+
timeout: Duration::from_secs(10),
33+
onion_timeout: Duration::from_secs(120),
34+
}
35+
}
36+
}
37+
38+
pub(crate) struct ServiceConfig {
39+
pub timeout: ServiceTimeout,
2540
pub session_config: SessionConfig,
2641
pub max_frame_length: usize,
2742
pub keep_buffer: bool,
@@ -36,7 +51,7 @@ pub(crate) struct ServiceConfig {
3651
impl Default for ServiceConfig {
3752
fn default() -> Self {
3853
ServiceConfig {
39-
timeout: Duration::from_secs(10),
54+
timeout: ServiceTimeout::default(),
4055
session_config: SessionConfig::default(),
4156
max_frame_length: 1024 * 1024 * 8,
4257
keep_buffer: false,

tentacle/src/transports/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ mod os {
9393

9494
use crate::{
9595
runtime::{TcpListener, TcpStream},
96-
service::config::TcpConfig,
96+
service::config::{ServiceTimeout, TcpConfig},
9797
};
9898

9999
use futures::{FutureExt, StreamExt, prelude::Stream};
@@ -136,15 +136,15 @@ mod os {
136136

137137
#[derive(Clone)]
138138
pub(crate) struct MultiTransport {
139-
pub(crate) timeout: Duration,
139+
pub(crate) timeout: ServiceTimeout,
140140
pub(crate) tcp_config: TcpConfig,
141141
pub(crate) listens_upgrade_modes: Arc<crate::lock::Mutex<HashMap<SocketAddr, UpgradeMode>>>,
142142
#[cfg(feature = "tls")]
143143
pub(crate) tls_config: Option<TlsConfig>,
144144
}
145145

146146
impl MultiTransport {
147-
pub fn new(timeout: Duration, tcp_config: TcpConfig) -> Self {
147+
pub fn new(timeout: ServiceTimeout, tcp_config: TcpConfig) -> Self {
148148
MultiTransport {
149149
timeout,
150150
tcp_config,
@@ -217,20 +217,23 @@ mod os {
217217
fn dial(self, address: Multiaddr) -> Result<Self::DialFuture> {
218218
match find_type(&address) {
219219
TransportType::Tcp => {
220-
match TcpTransport::new(self.timeout, self.tcp_config.tcp).dial(address) {
220+
match TcpTransport::new(self.timeout.timeout, self.tcp_config.tcp).dial(address)
221+
{
221222
Ok(res) => Ok(MultiDialFuture::Tcp(res)),
222223
Err(e) => Err(e),
223224
}
224225
}
225226
TransportType::Onion => {
226-
match OnionTransport::new(self.timeout, self.tcp_config.tcp).dial(address) {
227+
match OnionTransport::new(self.timeout.onion_timeout, self.tcp_config.tcp)
228+
.dial(address)
229+
{
227230
Ok(res) => Ok(MultiDialFuture::Tcp(res)),
228231
Err(e) => Err(e),
229232
}
230233
}
231234
#[cfg(feature = "ws")]
232235
TransportType::Ws => {
233-
match WsTransport::new(self.timeout, self.tcp_config.ws).dial(address) {
236+
match WsTransport::new(self.timeout.timeout, self.tcp_config.ws).dial(address) {
234237
Ok(future) => Ok(MultiDialFuture::Ws(future)),
235238
Err(e) => Err(e),
236239
}
@@ -247,7 +250,7 @@ mod os {
247250
let tls_config = self.tls_config.ok_or_else(|| {
248251
TransportErrorKind::TlsError("tls config is not set".to_string())
249252
})?;
250-
TlsTransport::new(self.timeout, tls_config, self.tcp_config.tls)
253+
TlsTransport::new(self.timeout.timeout, tls_config, self.tcp_config.tls)
251254
.dial(address)
252255
.map(MultiDialFuture::Tls)
253256
}

tentacle/src/transports/tcp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl TcpTransport {
6262
listen_mode: TcpListenMode,
6363
) -> Self {
6464
Self {
65-
timeout: multi_transport.timeout,
65+
timeout: multi_transport.timeout.timeout,
6666
tcp_config: match listen_mode {
6767
TcpListenMode::Tcp => multi_transport.tcp_config.tcp,
6868
#[cfg(feature = "ws")]

0 commit comments

Comments
 (0)