Handle protocol negotiation failure in swarm new_stream

This commit is contained in:
NIC619 2019-09-17 16:17:41 +08:00
parent 559f419b4e
commit f253152858
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
3 changed files with 19 additions and 9 deletions

View File

@ -7,6 +7,7 @@ from multiaddr import Multiaddr
from libp2p.peer.id import ID from libp2p.peer.id import ID
from libp2p.peer.peerstore import PeerStoreError from libp2p.peer.peerstore import PeerStoreError
from libp2p.peer.peerstore_interface import IPeerStore from libp2p.peer.peerstore_interface import IPeerStore
from libp2p.protocol_muxer.exceptions import MultiselectClientError
from libp2p.protocol_muxer.multiselect import Multiselect from libp2p.protocol_muxer.multiselect import Multiselect
from libp2p.protocol_muxer.multiselect_client import MultiselectClient from libp2p.protocol_muxer.multiselect_client import MultiselectClient
from libp2p.protocol_muxer.multiselect_communicator import MultiselectCommunicator from libp2p.protocol_muxer.multiselect_communicator import MultiselectCommunicator
@ -176,9 +177,16 @@ class Swarm(INetwork):
muxed_stream = await muxed_conn.open_stream() muxed_stream = await muxed_conn.open_stream()
# Perform protocol muxing to determine protocol to use # Perform protocol muxing to determine protocol to use
selected_protocol = await self.multiselect_client.select_one_of( try:
list(protocol_ids), MultiselectCommunicator(muxed_stream) selected_protocol = await self.multiselect_client.select_one_of(
) list(protocol_ids), MultiselectCommunicator(muxed_stream)
)
except MultiselectClientError as error:
logger.debug("fail to open a stream to peer %s, error=%s", peer_id, error)
await muxed_stream.reset()
raise SwarmException(
"failt to open a stream to peer %s", peer_id
) from error
# Create a net stream with the selected protocol # Create a net stream with the selected protocol
net_stream = NetStream(muxed_stream) net_stream = NetStream(muxed_stream)

View File

@ -19,11 +19,12 @@ from lru import LRU
from libp2p.exceptions import ParseError, ValidationError from libp2p.exceptions import ParseError, ValidationError
from libp2p.host.host_interface import IHost from libp2p.host.host_interface import IHost
from libp2p.io.exceptions import IncompleteReadError from libp2p.io.exceptions import IncompleteReadError
from libp2p.network.exceptions import SwarmException
from libp2p.network.stream.exceptions import StreamEOF, StreamReset
from libp2p.network.stream.net_stream_interface import INetStream from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.peer.id import ID from libp2p.peer.id import ID
from libp2p.typing import TProtocol from libp2p.typing import TProtocol
from libp2p.utils import encode_varint_prefixed, read_varint_prefixed_bytes from libp2p.utils import encode_varint_prefixed, read_varint_prefixed_bytes
from libp2p.network.stream.exceptions import StreamEOF, StreamReset
from .pb import rpc_pb2 from .pb import rpc_pb2
from .pubsub_notifee import PubsubNotifee from .pubsub_notifee import PubsubNotifee
@ -245,7 +246,11 @@ class Pubsub:
# disconnect the peer? # disconnect the peer?
async def _handle_new_peer(self, peer_id: ID) -> None: async def _handle_new_peer(self, peer_id: ID) -> None:
stream: INetStream = await self.host.new_stream(peer_id, self.protocols) try:
stream: INetStream = await self.host.new_stream(peer_id, self.protocols)
except SwarmException as error:
logger.debug("fail to add new peer %s, error %s", peer_id, error)
return
self.peers[peer_id] = stream self.peers[peer_id] = stream

View File

@ -10,10 +10,7 @@ from libp2p.security.secure_transport_interface import ISecureTransport
from libp2p.security.security_multistream import SecurityMultistream from libp2p.security.security_multistream import SecurityMultistream
from libp2p.stream_muxer.abc import IMuxedConn from libp2p.stream_muxer.abc import IMuxedConn
from libp2p.stream_muxer.muxer_multistream import MuxerClassType, MuxerMultistream from libp2p.stream_muxer.muxer_multistream import MuxerClassType, MuxerMultistream
from libp2p.transport.exceptions import ( from libp2p.transport.exceptions import MuxerUpgradeFailure, SecurityUpgradeFailure
MuxerUpgradeFailure,
SecurityUpgradeFailure,
)
from libp2p.typing import TProtocol from libp2p.typing import TProtocol
from .listener_interface import IListener from .listener_interface import IListener