Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasticFiasco committed Oct 20, 2021
1 parent caa3884 commit 3fbb401
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ public class LogEventQueue
private readonly long? queueLimitBytes;
private readonly object syncRoot = new();

private long queueBytes;

public LogEventQueue(long? queueLimitBytes = null)
{
if (queueLimitBytes < 1)
throw new ArgumentException("queueLimitBytes must be either null or greater than 0", nameof(queueLimitBytes));

queue = new Queue<string>();
this.queueLimitBytes = queueLimitBytes;

queueBytes = 0;
}

public void Enqueue(string logEvent)
Expand All @@ -45,11 +49,13 @@ public EnqueueResult TryEnqueue(string logEvent)
{
lock (syncRoot)
{
if (queue.Count == queueLimit)
var logEventByteSize = ByteSize.From(logEvent);
if (queueBytes + logEventByteSize > queueLimitBytes)
{
return EnqueueResult.QueueFull;
}

queueBytes += logEventByteSize;
queue.Enqueue(logEvent);
return EnqueueResult.Ok;
}
Expand All @@ -66,13 +72,15 @@ public DequeueResult TryDequeue(long? logEventMaxSize, out string logEvent)
}

logEvent = queue.Peek();
var logEventByteSize = ByteSize.From(logEvent);

if (ByteSize.From(logEvent) > logEventMaxSize)
if (logEventByteSize > logEventMaxSize)
{
logEvent = string.Empty;
return DequeueResult.MaxSizeViolation;
}

queueBytes -= logEventByteSize;
queue.Dequeue();
return DequeueResult.Ok;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public HttpSinkGivenCodeConfigurationShould()
requestUri: "https://www.mylogs.com",
logEventsInBatchLimit: 100,
batchSizeLimitBytes: ByteSize.MB,
queueLimit: 10000,
queueLimitBytes: ByteSize.MB,
period: TimeSpan.FromMilliseconds(1),
textFormatter: new NormalRenderedTextFormatter(),
batchFormatter: new DefaultBatchFormatter(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task StayIdleGivenNoLogEvents()
logEventLimitBytes: null,
logEventsInBatchLimit: null,
batchSizeLimitBytes: null,
queueLimit: null,
queueLimitBytes: null,
period: TimeSpan.FromMilliseconds(1), // 1 ms period
textFormatter: new NormalTextFormatter(),
batchFormatter: new DefaultBatchFormatter(),
Expand All @@ -48,7 +48,7 @@ public async Task RespectLogEventLimitBytes()
logEventLimitBytes: 1, // Is lower than emitted log event
logEventsInBatchLimit: null,
batchSizeLimitBytes: null,
queueLimit: null,
queueLimitBytes: null,
period: TimeSpan.FromMilliseconds(1), // 1 ms period
textFormatter: new NormalTextFormatter(),
batchFormatter: new DefaultBatchFormatter(),
Expand All @@ -65,7 +65,7 @@ public async Task RespectLogEventLimitBytes()
}

[Fact]
public async Task RespectQueueLimit()
public async Task RespectQueueLimitBytes()
{
// Arrange
var httpClient = new HttpClientMock();
Expand All @@ -76,12 +76,14 @@ public async Task RespectQueueLimit()
.Select(number => Some.LogEvent("Event {number}", number))
.ToArray();


// TODO: Calculate the size of the first log event, and set the value as queueLimitBytes
using var sink = new HttpSink(
requestUri: "https://www.mylogs.com",
logEventLimitBytes: null,
logEventsInBatchLimit: null,
batchSizeLimitBytes: null,
queueLimit: 1, // Queue only holds 1 event
queueLimitBytes: 1, // Queue only holds the first event
period: TimeSpan.FromMilliseconds(1), // 1 ms period
textFormatter: new NormalTextFormatter(),
batchFormatter: new DefaultBatchFormatter(),
Expand Down
2 changes: 1 addition & 1 deletion test/Serilog.Sinks.HttpTests/appsettings_http.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"requestUri": "https://www.mylogs.com",
"logEventsInBatchLimit": 100,
"batchSizeLimitBytes": 1048576,
"queueLimit": 10000,
"queueLimitBytes": 1048576,
"period": "00:00:00.001",
"textFormatter": "Serilog.Sinks.Http.TextFormatters.NormalRenderedTextFormatter, Serilog.Sinks.Http",
"batchFormatter": "Serilog.Sinks.Http.BatchFormatters.DefaultBatchFormatter, Serilog.Sinks.Http",
Expand Down

0 comments on commit 3fbb401

Please sign in to comment.