Updated
This commit is contained in:
1 parent
17d7426b44
commit
a0959ea2c7
11 files changed
+171
-35
No files matched your search
@@ -86,6 +86,7 @@ namespace EonaCat.LogStack.LogClient
|
||||
entry.Properties = JsonHelper.ToJson(properties);
|
||||
|
||||
_logQueue.Enqueue(entry);
|
||||
TrimQueue();
|
||||
|
||||
if (_logQueue.Count >= _options.BatchSize)
|
||||
{
|
||||
@@ -93,6 +94,22 @@ namespace EonaCat.LogStack.LogClient
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops the oldest entries once the queue exceeds the configured maximum size,
|
||||
/// so a server outage cannot grow the queue without bound.
|
||||
/// </summary>
|
||||
private void TrimQueue()
|
||||
{
|
||||
while (_logQueue.Count > _options.MaxQueueSize && _logQueue.TryDequeue(out _))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsExpired(LogEntry entry)
|
||||
{
|
||||
return DateTime.UtcNow - entry.Timestamp > _options.MaxRetention;
|
||||
}
|
||||
|
||||
private async Task FlushAsync()
|
||||
{
|
||||
if (_logQueue.IsEmpty)
|
||||
@@ -106,6 +123,12 @@ namespace EonaCat.LogStack.LogClient
|
||||
var batch = new List<LogEntry>();
|
||||
while (batch.Count < _options.BatchSize && _logQueue.TryDequeue(out var entry))
|
||||
{
|
||||
// Drop stale entries instead of holding/sending them indefinitely.
|
||||
if (IsExpired(entry))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
batch.Add(entry);
|
||||
}
|
||||
|
||||
@@ -155,10 +178,17 @@ namespace EonaCat.LogStack.LogClient
|
||||
Console.WriteLine($"[LogCentral] Failed to send logs to EonaCat: {ex.Message}");
|
||||
}
|
||||
|
||||
// Only hold onto entries that are still within the retention window;
|
||||
// never retry/re-queue indefinitely, and never exceed the max queue size.
|
||||
foreach (var entry in batch)
|
||||
{
|
||||
_logQueue.Enqueue(entry);
|
||||
if (!IsExpired(entry))
|
||||
{
|
||||
_logQueue.Enqueue(entry);
|
||||
}
|
||||
}
|
||||
|
||||
TrimQueue();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace EonaCat.LogStack.LogClient
|
||||
public LogCentralEonaCatAdapter(LogBuilder logBuilder, LogCentralClient client)
|
||||
{
|
||||
_client = client;
|
||||
_logBuilder = logBuilder ?? throw new ArgumentNullException(nameof(logBuilder));
|
||||
_logBuilder.OnLog += LogSettings_OnLog;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,5 +17,17 @@ namespace EonaCat.LogStack.LogClient
|
||||
public int BatchSize { get; set; } = 1;
|
||||
public int FlushIntervalSeconds { get; set; } = 5;
|
||||
public bool EnableFallbackLogging { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum number of entries kept in memory while the server is unreachable.
|
||||
/// Oldest entries are dropped once this is exceeded to avoid unbounded memory growth.
|
||||
/// </summary>
|
||||
public int MaxQueueSize { get; set; } = 10000;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum time an entry is kept in memory waiting to be (re)sent. Entries older
|
||||
/// than this are dropped instead of retried/re-queued forever.
|
||||
/// </summary>
|
||||
public TimeSpan MaxRetention { get; set; } = TimeSpan.FromMinutes(10);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user