Skip to content

Commit

Permalink
feat: close inactive tunnels and their channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 committed Aug 20, 2024
1 parent cd77eeb commit fb00e87
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ impl PProxy {
println!("Local node is listening on {address}");
}

SwarmEvent::ConnectionClosed { peer_id, .. } => {
self.inbound_tunnels.retain(|(p, _), _| p != &peer_id);
self.tunnel_txs.retain(|(p, _), _| p != &peer_id);
}

SwarmEvent::Behaviour(PProxyNetworkBehaviourEvent::RequestResponse(
request_response::Event::Message { peer, message },
)) => match message {
Expand All @@ -271,11 +276,17 @@ impl PProxy {
let tunnel_id = request.tunnel_id.clone();
let resp = match self.handle_tunnel_request(peer, request).await {
Ok(resp) => resp,
Err(e) => Some(proto::Tunnel {
tunnel_id,
command: proto::TunnelCommand::Break.into(),
data: Some(e.to_string().as_bytes().to_vec()),
}),
Err(e) => {
if let Ok(tunnel_id) = tunnel_id.parse() {
self.inbound_tunnels.remove(&(peer, tunnel_id));
self.tunnel_txs.remove(&(peer, tunnel_id));
}
Some(proto::Tunnel {
tunnel_id,
command: proto::TunnelCommand::Break.into(),
data: Some(e.to_string().as_bytes().to_vec()),
})
}
};

self.swarm
Expand Down Expand Up @@ -324,8 +335,7 @@ impl PProxy {
// Terminat connecting tunnel
if let Some(tx) = self.outbound_ready_notifiers.remove(&request_id) {
tx.send(Err(Error::Tunnel(TunnelError::ConnectionAborted)))
.map_err(|_| Error::EssentialTaskClosed)?;
return Ok(());
.map_err(|_| Error::EssentialTaskClosed)?
}

// Terminat connected tunnel
Expand Down Expand Up @@ -354,7 +364,7 @@ impl PProxy {
.map_err(|_| Error::EssentialTaskClosed)?
}

// TODO: Should tell tunnel sent failed
// TODO: Should also tell tunnel sent failed. But cannot get tunnel_id here.
}

_ => {}
Expand Down

0 comments on commit fb00e87

Please sign in to comment.