|
38 | 38 | ) |
39 | 39 | from libp2p.network.auto_connector import AutoConnector |
40 | 40 | from libp2p.network.config import ConnectionConfig, RetryConfig |
| 41 | +from libp2p.transport.quic.config import QUICTransportConfig |
41 | 42 | from libp2p.network.connection_gate import ConnectionGate |
42 | 43 | from libp2p.network.connection_pruner import ConnectionPruner |
43 | 44 | from libp2p.network.tag_store import TagInfo, TagStore |
|
57 | 58 | OpenConnectionError, |
58 | 59 | SecurityUpgradeFailure, |
59 | 60 | ) |
60 | | -from libp2p.transport.quic.config import QUICTransportConfig |
61 | | -from libp2p.transport.quic.connection import QUICConnection |
62 | | -from libp2p.transport.quic.transport import QUICTransport |
63 | 61 | from libp2p.transport.upgrader import ( |
64 | 62 | TransportUpgrader, |
65 | 63 | ) |
@@ -200,13 +198,10 @@ async def run(self) -> None: |
200 | 198 |
|
201 | 199 | # Set background nursery BEFORE setting the event |
202 | 200 | # This ensures transports have the nursery when they check |
203 | | - if isinstance(self.transport, QUICTransport): |
204 | | - self.transport.set_background_nursery(nursery) |
205 | | - self.transport.set_swarm(self) |
206 | | - elif hasattr(self.transport, "set_background_nursery"): |
207 | | - # WebSocket transport also needs background nursery |
208 | | - # for connection management |
| 201 | + if hasattr(self.transport, "set_background_nursery"): |
209 | 202 | self.transport.set_background_nursery(nursery) # type: ignore[attr-defined] |
| 203 | + if hasattr(self.transport, "set_swarm"): |
| 204 | + self.transport.set_swarm(self) # type: ignore[attr-defined] |
210 | 205 |
|
211 | 206 | # Set event after background nursery is configured |
212 | 207 | # This ensures transports have the nursery when they check the event |
@@ -647,11 +642,11 @@ async def _dial_addr_single_attempt(self, addr: Multiaddr, peer_id: ID) -> INetC |
647 | 642 | pass |
648 | 643 | raise SwarmException(f"Unexpected error dialing peer {peer_id}") from e |
649 | 644 |
|
650 | | - if isinstance(self.transport, QUICTransport) and isinstance( |
651 | | - raw_conn, IMuxedConn |
| 645 | + if getattr(self.transport, 'provides_muxing', False) and getattr( |
| 646 | + raw_conn, 'is_muxed', False |
652 | 647 | ): |
653 | 648 | logger.info( |
654 | | - "Skipping upgrade for QUIC, QUIC connections are already multiplexed" |
| 649 | + "Skipping upgrade — transport already provides security + muxing" |
655 | 650 | ) |
656 | 651 | try: |
657 | 652 | swarm_conn = await self.add_conn(raw_conn, direction="outbound") |
@@ -886,7 +881,7 @@ async def new_stream(self, peer_id: ID) -> INetStream: |
886 | 881 | f"Failed to get a valid connection for peer {peer_id}" |
887 | 882 | ) |
888 | 883 |
|
889 | | - if isinstance(self.transport, QUICTransport) and connection is not None: |
| 884 | + if getattr(self.transport, 'provides_muxing', False) and connection is not None: |
890 | 885 | conn = cast("SwarmConn", connection) |
891 | 886 | try: |
892 | 887 | stream = await conn.new_stream() |
@@ -1115,14 +1110,16 @@ async def conn_handler( |
1115 | 1110 | pass |
1116 | 1111 | return |
1117 | 1112 |
|
1118 | | - # No need to upgrade QUIC Connection |
1119 | | - if isinstance(self.transport, QUICTransport): |
| 1113 | + # No need to upgrade connections from transports with built-in muxing |
| 1114 | + if getattr(self.transport, 'provides_muxing', False): |
1120 | 1115 | try: |
1121 | | - quic_conn = cast(QUICConnection, read_write_closer) |
1122 | | - await self.add_conn(quic_conn, direction="inbound") |
1123 | | - peer_id = quic_conn.peer_id |
| 1116 | + # The connection is already muxed; add it directly. |
| 1117 | + muxed_conn = cast(IMuxedConn, read_write_closer) |
| 1118 | + await self.add_conn(muxed_conn, direction="inbound") |
| 1119 | + peer_id = muxed_conn.peer_id |
1124 | 1120 | logger.debug( |
1125 | | - f"successfully opened quic connection to peer {peer_id}" |
| 1121 | + "successfully opened native-muxed connection " |
| 1122 | + f"to peer {peer_id}" |
1126 | 1123 | ) |
1127 | 1124 | # NOTE: This is a intentional barrier to prevent from the |
1128 | 1125 | # handler exiting and closing the connection. |
@@ -1511,10 +1508,13 @@ async def add_conn( |
1511 | 1508 | logger.debug("Swarm::add_conn | starting muxed connection") |
1512 | 1509 | self.manager.run_task(muxed_conn.start) |
1513 | 1510 | await muxed_conn.event_started.wait() |
1514 | | - # For QUIC connections, also verify connection is established |
1515 | | - if isinstance(muxed_conn, QUICConnection): |
1516 | | - if not muxed_conn.is_established: |
1517 | | - await muxed_conn._connected_event.wait() |
| 1511 | + # For connections that need an explicit handshake-completion step, |
| 1512 | + # wait until the connection reports itself as established. |
| 1513 | + if hasattr(muxed_conn, 'is_established') and hasattr( |
| 1514 | + muxed_conn, '_connected_event' |
| 1515 | + ): |
| 1516 | + if not muxed_conn.is_established: # type: ignore[attr-defined] |
| 1517 | + await muxed_conn._connected_event.wait() # type: ignore[attr-defined] |
1518 | 1518 | logger.debug("Swarm::add_conn | starting swarm connection") |
1519 | 1519 | self.manager.run_task(swarm_conn.start) |
1520 | 1520 | await swarm_conn.event_started.wait() |
|
0 commit comments