Skip to content

TransportService: Improve connection stability by downgrading connections on substream inactivity #253

@lexnv

Description

@lexnv

Context

The transport service handler intent is to downgrade connections (leading to possibly closing them), if substreams where not open in a given timeframe:

/// Close the connection if no substreams are open within this time frame.
keep_alive_timeout: Duration,
/// Pending keep-alive timeouts.
pending_keep_alive_timeouts: FuturesUnordered<BoxFuture<'static, (PeerId, ConnectionId)>>,

When a connection is established, the timeout is properly tracked:

self.pending_keep_alive_timeouts.push(Box::pin(async move {
tokio::time::sleep(keep_alive_timeout).await;
(peer, connection_id)
}));

When the timeout expires, the connection is downgraded:

while let Poll::Ready(Some((peer, connection_id))) =
self.pending_keep_alive_timeouts.poll_next_unpin(cx)
{
if let Some(context) = self.connections.get_mut(&peer) {
tracing::trace!(
target: LOG_TARGET,
?peer,
?connection_id,
"keep-alive timeout over, downgrade connection",
);
context.downgrade(&connection_id);
}
}

Issue

Opening of substreams are not taken into account for downgrading connections:

Some(event) => return Poll::Ready(Some(event.into())),

Here we should match for Some(InnerTransportEvent::SubstreamOpened and move forward the timeout future.

With the current implementation, we downgrade connections at 5 seconds intervals.

Implications

We need to double-check if the protocol name matches the substream open protocol.
Further, InnerTransportEvent::SubstreamOpened needs to be extended with a connectionID because the TransportService holds up to 2 connections (primary and secondary). We should properly advance the timeout of the connection ID.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions