-
Notifications
You must be signed in to change notification settings - Fork 30
transport/manager: Add ability to accept/reject incoming connections before negotiation #186
Description
Refactor the transport manager and every transport to allow the transport manager to accept or reject incoming connections.
Currently, each transport is responsible for polling the underlying socket and all incoming connections are immediately accepted and negotiated:
litep2p/src/transport/tcp/mod.rs
Lines 421 to 429 in 4c7cda6
| fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | |
| while let Poll::Ready(event) = self.listener.poll_next_unpin(cx) { | |
| match event { | |
| None | Some(Err(_)) => return Poll::Ready(None), | |
| Some(Ok((connection, address))) => { | |
| self.on_inbound_connection(connection, address); | |
| } | |
| } | |
| } |
litep2p/src/transport/tcp/mod.rs
Lines 113 to 126 in 4c7cda6
| self.pending_connections.push(Box::pin(async move { | |
| TcpConnection::accept_connection( | |
| connection, | |
| connection_id, | |
| keypair, | |
| address, | |
| yamux_config, | |
| max_read_ahead_factor, | |
| max_write_buffer_size, | |
| connection_open_timeout, | |
| substream_open_timeout, | |
| ) | |
| .await | |
| .map_err(|error| (connection_id, error)) |
It would be beneficial in terms of resources to move the accept/reject responsibility to the transport manager for inbound connections (similar how we handle outbound ones). We can reject the connection immediately rather than negotiating it to simply reject later.
One option would be to extend the TransportEvent::ConnectionOpened to support an Endpoint instead of a connection + address. Then we can emit this event from each transport to the transport manager. Then, the transport manager can check the limits and handle the negotiation. This may also require a refactor to the peer state logic.