Skip to content

Commit

Permalink
v12.1.0 - Clean error cache in the background
Browse files Browse the repository at this point in the history
  • Loading branch information
monoman committed Apr 20, 2023
1 parent 52bd642 commit 408f42b
Show file tree
Hide file tree
Showing 66 changed files with 93 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ dotnet_style_qualification_for_property = false:silent
dotnet_style_readonly_field = true:warning
dotnet_style_require_accessibility_modifiers = always:silent

file_header_template = ******************************************************************************************************************************\n \nCopyright (c) 2018-2022 InterlockLedger Network\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met\n\n* Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n\n* Neither the name of the copyright holder nor the names of its\n contributors may be used to endorse or promote products derived from\n this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n******************************************************************************************************************************
file_header_template = ******************************************************************************************************************************\n \nCopyright (c) 2018-2023 InterlockLedger Network\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met\n\n* Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n\n* Neither the name of the copyright holder nor the names of its\n contributors may be used to endorse or promote products derived from\n this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n******************************************************************************************************************************
csharp_style_namespace_declarations = file_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
2 changes: 1 addition & 1 deletion Demo.InterlockLedger.Peer2Peer/ClientLivenessListener.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion Demo.InterlockLedger.Peer2Peer/DemoBaseSink.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion Demo.InterlockLedger.Peer2Peer/DemoClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion Demo.InterlockLedger.Peer2Peer/DemoServer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion Demo.InterlockLedger.Peer2Peer/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion Demo.InterlockLedger.Peer2Peer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/FakeConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/FakeDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/FakeLogging.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/FakeNodeSink.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/SingleUseSocket.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/TestClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/TestConnection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/TestHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/TestListenerForPeer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/TestSink.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/TestSocket.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer.UnitTests/UnitTestPipeline.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer/AbstractNodeSink.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer/ActiveChannel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
4 changes: 2 additions & 2 deletions InterlockLedger.Peer2Peer/ConnectionBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -81,7 +81,7 @@ public IActiveChannel GetChannel(ulong channel)
protected ConnectionBase(string id, INetworkConfig config, CancellationTokenSource source, ILogger logger)
: base(id, config, source, logger) {
_pipeline = null;
_errorCachingLogger = new ErrorCachingLogger(() => Abandon || _stopping, _logger);
_errorCachingLogger = new ErrorCachingLogger(_logger);
NetworkAddress = "?";
}

Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer/ConnectionInitiatedByPeer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer/ConnectionToPeer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer/DummyExternalAccessDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
45 changes: 26 additions & 19 deletions InterlockLedger.Peer2Peer/ErrorCachingLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -36,14 +36,29 @@

namespace InterlockLedger.Peer2Peer
{
public record ErrorCachingLogger(Func<bool> AbandonNow, ILogger Logger)
public class ErrorCachingLogger
{
public ErrorCachingLogger(ILogger logger) => _logger = logger.Required();

public void LogError(string message) {
var now = DateTimeOffset.Now;
if (_errors.TryGetValue(message, out var dateTime) && (now - dateTime).Hours < _hoursOfSilencedDuplicateErrors)
return;
_ = _errors.AddOrUpdate(message, m => LogAt(now, m), (m, lastMoment) => now > lastMoment ? LogAt(now, m) : lastMoment);
}

public static void StopBackgroundProcessing() => _stop = true;

static ErrorCachingLogger() => CleanOldestErrors().RunOnThread(nameof(CleanOldestErrors));

private const int _hoursOfSilencedDuplicateErrors = 8;
private const int _tooManyErrors = 500;
private static readonly ConcurrentDictionary<string, DateTimeOffset> _errors = new();
public async Task CleanOldestErrors() {
while (!AbandonNow()) {
private readonly ILogger _logger;
private static bool _stop;

private static async Task CleanOldestErrors() {
while (!_stop) {
if (_errors.Count > 100) {
var now = DateTimeOffset.Now;
var oldestErrors = _errors.ToArray()
Expand All @@ -52,32 +67,24 @@ public record ErrorCachingLogger(Func<bool> AbandonNow, ILogger Logger)
Remove(oldestErrors);
}
await Task.Delay(5_000);
if (!AbandonNow() && _errors.Count > _tooManyErrors) {
if (_errors.Count > _tooManyErrors) {
var oldestErrors = _errors.ToArray()
.OrderBy(pair => pair.Value)
.Take(_errors.Count - _tooManyErrors);
Remove(oldestErrors);
}
await Task.Delay(10_000);
}
}

public void LogError(string message) {
var now = DateTimeOffset.Now;
if (_errors.TryGetValue(message, out var dateTime) && (now - dateTime).Hours < _hoursOfSilencedDuplicateErrors)
return;
_ = _errors.AddOrUpdate(message, m => LogAt(now, m), (m, lastMoment) => now > lastMoment ? LogAt(now, m) : lastMoment);
static void Remove(IEnumerable<KeyValuePair<string, DateTimeOffset>> errorsToRemove) {
foreach (var error in errorsToRemove)
_ = _errors.TryRemove(error);
}
}

private DateTimeOffset LogAt(DateTimeOffset now, string message) {
Logger.LogError("{now} - {message}", now, message);
_logger.LogError("{now} - {message}", now, message);
return now;
}
private static void Remove(IEnumerable<KeyValuePair<string, DateTimeOffset>> errorsToRemove) {
foreach (var error in errorsToRemove) {
if (!_errors.TryRemove(error))
return;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion InterlockLedger.Peer2Peer/Extensions/SocketExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ******************************************************************************************************************************
//
// Copyright (c) 2018-2022 InterlockLedger Network
// Copyright (c) 2018-2023 InterlockLedger Network
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down
Loading

0 comments on commit 408f42b

Please sign in to comment.