Skip to content

Commit

Permalink
Hide possible exceptions during shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 committed Dec 13, 2024
1 parent c005c9c commit 5970332
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/libp2p/Libp2p.Protocols.Pubsub/PubsubRouter.Topics.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: MIT

using Microsoft.Extensions.Logging;
using Nethermind.Libp2p.Core;
using Nethermind.Libp2p.Protocols.Pubsub.Dto;
using System.Buffers.Binary;
Expand Down Expand Up @@ -82,34 +83,41 @@ public void Unsubscribe(string topicId)

public void UnsubscribeAll()
{
foreach (PeerId? peerId in fPeers.SelectMany(kv => kv.Value))
try
{
Rpc msg = new Rpc().WithTopics([], topicState.Keys);
foreach (PeerId? peerId in fPeers.SelectMany(kv => kv.Value))
{
Rpc msg = new Rpc().WithTopics([], topicState.Keys);

peerState.GetValueOrDefault(peerId)?.Send(msg);
}
peerState.GetValueOrDefault(peerId)?.Send(msg);
}

Dictionary<PeerId, Rpc> peerMessages = [];
Dictionary<PeerId, Rpc> peerMessages = [];

foreach (PeerId? peerId in gPeers.SelectMany(kv => kv.Value))
{
(peerMessages[peerId] ??= new Rpc())
.WithTopics([], topicState.Keys);
}

foreach (KeyValuePair<string, HashSet<PeerId>> topicMesh in mesh)
{
foreach (PeerId peerId in topicMesh.Value)
foreach (PeerId? peerId in gPeers.SelectMany(kv => kv.Value))
{
(peerMessages[peerId] ??= new Rpc())
.Ensure(r => r.Control.Prune)
.Add(new ControlPrune { TopicID = topicMesh.Key });
.WithTopics([], topicState.Keys);
}
}

foreach (KeyValuePair<PeerId, Rpc> peerMessage in peerMessages)
foreach (KeyValuePair<string, HashSet<PeerId>> topicMesh in mesh.ToDictionary())
{
foreach (PeerId peerId in topicMesh.Value)
{
(peerMessages[peerId] ??= new Rpc())
.Ensure(r => r.Control.Prune)
.Add(new ControlPrune { TopicID = topicMesh.Key });
}
}

foreach (KeyValuePair<PeerId, Rpc> peerMessage in peerMessages)
{
peerState.GetValueOrDefault(peerMessage.Key)?.Send(peerMessage.Value);
}
}
catch (Exception e)
{
peerState.GetValueOrDefault(peerMessage.Key)?.Send(peerMessage.Value);
logger?.LogError(e, $"Error during {nameof(UnsubscribeAll)}");
}
}

Expand Down

0 comments on commit 5970332

Please sign in to comment.