Merge pull request #181 from NIC619/fix_handle_unsubscribe

Fix: handle unsubscribe message
This commit is contained in:
NIC Lin
2019-07-21 20:09:51 +08:00
committed by GitHub
4 changed files with 125 additions and 6 deletions

View File

@ -179,9 +179,10 @@ class GossipSub(IPubsubRouter):
subscription announcement
:param topic: topic to join
"""
if topic in self.mesh:
return
# Create mesh[topic] if it does not yet exist
if topic not in self.mesh:
self.mesh[topic] = []
self.mesh[topic] = []
if topic in self.fanout and len(self.fanout[topic]) == self.degree:
# If router already has D peers from the fanout peers of a topic
@ -228,6 +229,8 @@ class GossipSub(IPubsubRouter):
It is invoked after the unsubscription announcement.
:param topic: topic to leave
"""
if topic not in self.mesh:
return
# Notify the peers in mesh[topic] with a PRUNE(topic) message
for peer in self.mesh[topic]:
await self.emit_prune(topic, peer)

View File

@ -107,8 +107,7 @@ class Pubsub():
# to know that it is subscribed to the topic (doesn't
# need everyone to know)
for message in rpc_incoming.subscriptions:
if message.subscribe:
self.handle_subscription(peer_id, message)
self.handle_subscription(peer_id, message)
if should_publish:
# relay message to peers with router
@ -210,6 +209,10 @@ class Pubsub():
:param topic_id: topic_id to subscribe to
"""
# Already subscribed
if topic_id in self.my_topics:
return self.my_topics[topic_id]
# Map topic_id to blocking queue
self.my_topics[topic_id] = asyncio.Queue()
@ -235,9 +238,11 @@ class Pubsub():
:param topic_id: topic_id to unsubscribe from
"""
# Return if we already unsubscribed from the topic
if topic_id not in self.my_topics:
return
# Remove topic_id from map if present
if topic_id in self.my_topics:
del self.my_topics[topic_id]
del self.my_topics[topic_id]
# Create unsubscribe message
packet = rpc_pb2.RPC()