Remove locks in PubsubNotifee

- Change `open_memory_channel(0)` to `open_memory_channel(math.inf)`, to
avoid `peer_queue.send` and `dead_peer_queue.send` blocking. This allows
us to remove the locks.
- Only catch `trio.BrokenResourceError`, which is caused by Pubsub when
it's closing.
This commit is contained in:
mhchia
2020-01-27 14:30:44 +08:00
parent 92ea35e147
commit c3ba67ea87
2 changed files with 19 additions and 34 deletions

View File

@ -95,15 +95,15 @@ class Pubsub(Service, IPubsub):
# Attach this new Pubsub object to the router
self.router.attach(self)
peer_channels = trio.open_memory_channel[ID](0)
dead_peer_channels = trio.open_memory_channel[ID](0)
peer_send, peer_receive = trio.open_memory_channel[ID](math.inf)
dead_peer_send, dead_peer_receive = trio.open_memory_channel[ID](math.inf)
# Only keep the receive channels in `Pubsub`.
# Therefore, we can only close from the receive side.
self.peer_receive_channel = peer_channels[1]
self.dead_peer_receive_channel = dead_peer_channels[1]
self.peer_receive_channel = peer_receive
self.dead_peer_receive_channel = dead_peer_receive
# Register a notifee
self.host.get_network().register_notifee(
PubsubNotifee(peer_channels[0], dead_peer_channels[0])
PubsubNotifee(peer_send, dead_peer_send)
)
# Register stream handlers for each pubsub router protocol to handle