Handle StreamClosed in identify protocol handler

This commit is contained in:
NIC619 2019-11-12 18:07:30 +08:00
parent 56d3e50267
commit c4f9ce6bb3
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17

View File

@ -3,6 +3,7 @@ import logging
from multiaddr import Multiaddr from multiaddr import Multiaddr
from libp2p.host.host_interface import IHost from libp2p.host.host_interface import IHost
from libp2p.network.stream.exceptions import StreamClosed
from libp2p.network.stream.net_stream_interface import INetStream from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.typing import StreamHandlerFn, TProtocol from libp2p.typing import StreamHandlerFn, TProtocol
@ -43,8 +44,12 @@ def identify_handler_for(host: IHost) -> StreamHandlerFn:
protobuf = _mk_identify_protobuf(host) protobuf = _mk_identify_protobuf(host)
response = protobuf.SerializeToString() response = protobuf.SerializeToString()
await stream.write(response) try:
await stream.close() await stream.write(response)
logger.debug("successfully handled request for %s from %s", ID, peer_id) except StreamClosed:
logger.debug("Fail to respond to %s request: stream closed", ID)
else:
await stream.close()
logger.debug("successfully handled request for %s from %s", ID, peer_id)
return handle_identify return handle_identify