From 0a52a053758213a682e0d059289e1a1b6027b046 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Sat, 30 Nov 2019 20:02:11 +0800 Subject: [PATCH] Del entry if no more peers subscribe to the topic --- libp2p/pubsub/pubsub.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index 8a85a894..a3827f9f 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -316,7 +316,11 @@ class Pubsub: for topic in self.peer_topics: if peer_id in self.peer_topics[topic]: - self.peer_topics[topic].remove(peer_id) + # Delete the entry if no other peers left + if len(self.peer_topics[topic]) == 1: + del self.peer_topics[topic] + else: + self.peer_topics[topic].remove(peer_id) self.router.remove_peer(peer_id) @@ -361,7 +365,11 @@ class Pubsub: else: if sub_message.topicid in self.peer_topics: if origin_id in self.peer_topics[sub_message.topicid]: - self.peer_topics[sub_message.topicid].remove(origin_id) + # Delete the entry if no other peers left + if len(self.peer_topics[sub_message.topicid]) == 1: + del self.peer_topics[sub_message.topicid] + else: + self.peer_topics[sub_message.topicid].remove(origin_id) # FIXME(mhchia): Change the function name? async def handle_talk(self, publish_message: rpc_pb2.Message) -> None: