Skip to content

Commit c252dcc

Browse files
committed
Fixed async issues
1 parent ca25751 commit c252dcc

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Apps/LogExporterApp/App.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public sealed class App : IDnsApplication, IDnsQueryLogger
4646
long _droppedCount;
4747
DateTime _lastDropLog = DateTime.UtcNow;
4848
static readonly TimeSpan DropLogInterval = TimeSpan.FromSeconds(5);
49+
long _lastDropTicks;
4950
#endregion variables
5051

5152
#region constructor
@@ -170,11 +171,11 @@ public Task InsertLogAsync(DateTime timestamp, DnsDatagram request,
170171
{
171172
Interlocked.Increment(ref _droppedCount);
172173

173-
DateTime now = DateTime.UtcNow;
174-
if (now - _lastDropLog >= DropLogInterval)
174+
long nowTicks = DateTime.UtcNow.Ticks;
175+
long lastTicks = Volatile.Read(ref _lastDropTicks);
176+
if (new TimeSpan(nowTicks - lastTicks) >= DropLogInterval && Interlocked.CompareExchange(ref _lastDropTicks, nowTicks, lastTicks) == lastTicks)
175177
{
176178
long dropped = Interlocked.Exchange(ref _droppedCount, 0);
177-
_lastDropLog = now;
178179
_dnsServer?.WriteLog($"Log export queue full; dropped {dropped} entries over last {DropLogInterval.TotalSeconds:F0}s.");
179180
}
180181
}
@@ -265,9 +266,9 @@ private async Task DrainRemainingLogs(List<LogEntry> batch, CancellationToken to
265266
}
266267
}
267268

268-
if (batch.Count > 0 && !token.IsCancellationRequested)
269+
if (batch.Count > 0)
269270
{
270-
await _sinkDispatcher.DispatchAsync(batch, token).ConfigureAwait(false);
271+
await _sinkDispatcher.DispatchAsync(batch, CancellationToken.None).ConfigureAwait(false);
271272
batch.Clear();
272273
}
273274
}

0 commit comments

Comments
 (0)