Skip to content

Commit

Permalink
Allow configuration of outbound substream in OneShotHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
sireliah committed Mar 26, 2020
1 parent 28ea62d commit ab5cee3
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions swarm/src/protocols_handler/one_shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ where
max_dial_negotiated: u32,
/// Value to return from `connection_keep_alive`.
keep_alive: KeepAlive,
/// After the given duration has elapsed, an inactive connection will shutdown.
inactive_timeout: Duration,
/// The configuration container for the handler
config: OneShotHandlerConfig,
}

impl<TInProto, TOutProto, TOutEvent>
Expand All @@ -67,7 +67,7 @@ where
#[inline]
pub fn new(
listen_protocol: SubstreamProtocol<TInProto>,
inactive_timeout: Duration
config: OneShotHandlerConfig,
) -> Self {
OneShotHandler {
listen_protocol,
Expand All @@ -77,7 +77,7 @@ where
dial_negotiated: 0,
max_dial_negotiated: 8,
keep_alive: KeepAlive::Yes,
inactive_timeout,
config
}
}

Expand Down Expand Up @@ -121,7 +121,10 @@ where
{
#[inline]
fn default() -> Self {
OneShotHandler::new(SubstreamProtocol::new(Default::default()), Duration::from_secs(10))
OneShotHandler::new(
SubstreamProtocol::new(Default::default()),
OneShotHandlerConfig::default()
)
}
}

Expand Down Expand Up @@ -157,7 +160,7 @@ where
) {
// If we're shutting down the connection for inactivity, reset the timeout.
if !self.keep_alive.is_yes() {
self.keep_alive = KeepAlive::Until(Instant::now() + self.inactive_timeout);
self.keep_alive = KeepAlive::Until(Instant::now() + self.config.inactive_timeout);
}

self.events_out.push(out.into());
Expand All @@ -172,7 +175,7 @@ where
self.dial_negotiated -= 1;

if self.dial_negotiated == 0 && self.dial_queue.is_empty() {
self.keep_alive = KeepAlive::Until(Instant::now() + self.inactive_timeout);
self.keep_alive = KeepAlive::Until(Instant::now() + self.config.inactive_timeout);
}

self.events_out.push(out.into());
Expand Down Expand Up @@ -224,7 +227,8 @@ where
self.dial_negotiated += 1;
return Poll::Ready(
ProtocolsHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(self.dial_queue.remove(0)),
protocol: SubstreamProtocol::new(self.dial_queue.remove(0))
.with_timeout(self.config.substream_timeout),
info: (),
},
);
Expand All @@ -236,3 +240,25 @@ where
Poll::Pending
}
}

/// Configuration parameters for the `OneShotHandler`
pub struct OneShotHandlerConfig {
/// After the given duration has elapsed, an inactive connection will shutdown.
inactive_timeout: Duration,
/// Timeout duration for each newly opened outbound substream.
substream_timeout: Duration,
}

impl OneShotHandlerConfig {
fn new(inactive_timeout: Duration, substream_timeout: Duration) -> Self {
OneShotHandlerConfig { inactive_timeout, substream_timeout }
}
}

impl Default for OneShotHandlerConfig {
fn default() -> Self {
let inactive_timeout = Duration::from_secs(10);
let substream_timeout = Duration::from_secs(10);
OneShotHandlerConfig::new(inactive_timeout, substream_timeout)
}
}

0 comments on commit ab5cee3

Please sign in to comment.