Change type of peers in pubsub from list to set

This commit is contained in:
NIC619 2019-12-17 17:55:13 +08:00
parent b4bd997932
commit f10e3099cb
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17

View File

@ -8,6 +8,7 @@ from typing import (
Dict, Dict,
List, List,
NamedTuple, NamedTuple,
Set,
Tuple, Tuple,
Union, Union,
cast, cast,
@ -73,7 +74,7 @@ class Pubsub:
my_topics: Dict[str, "asyncio.Queue[rpc_pb2.Message]"] my_topics: Dict[str, "asyncio.Queue[rpc_pb2.Message]"]
peer_topics: Dict[str, List[ID]] peer_topics: Dict[str, Set[ID]]
peers: Dict[ID, INetStream] peers: Dict[ID, INetStream]
topic_validators: Dict[str, TopicValidator] topic_validators: Dict[str, TopicValidator]
@ -314,7 +315,7 @@ class Pubsub:
for topic in self.peer_topics: for topic in self.peer_topics:
if peer_id in self.peer_topics[topic]: if peer_id in self.peer_topics[topic]:
self.peer_topics[topic].remove(peer_id) self.peer_topics[topic].discard(peer_id)
self.router.remove_peer(peer_id) self.router.remove_peer(peer_id)
@ -353,11 +354,11 @@ class Pubsub:
self.peer_topics[sub_message.topicid] = [origin_id] self.peer_topics[sub_message.topicid] = [origin_id]
elif origin_id not in self.peer_topics[sub_message.topicid]: elif origin_id not in self.peer_topics[sub_message.topicid]:
# Add peer to topic # Add peer to topic
self.peer_topics[sub_message.topicid].append(origin_id) self.peer_topics[sub_message.topicid].add(origin_id)
else: else:
if sub_message.topicid in self.peer_topics: if sub_message.topicid in self.peer_topics:
if origin_id in self.peer_topics[sub_message.topicid]: if origin_id in self.peer_topics[sub_message.topicid]:
self.peer_topics[sub_message.topicid].remove(origin_id) self.peer_topics[sub_message.topicid].discard(origin_id)
# FIXME(mhchia): Change the function name? # FIXME(mhchia): Change the function name?
async def handle_talk(self, publish_message: rpc_pb2.Message) -> None: async def handle_talk(self, publish_message: rpc_pb2.Message) -> None: