Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/AniSprinkles.Core/PageModels/LongPressTapSuppressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,19 @@ public static class LongPressTapSuppressor

/// <summary>True when a long press fired within <see cref="SuppressionWindow"/>.</summary>
public static bool ShouldSuppressTap()
=> Environment.TickCount64 - Interlocked.Read(ref _lastLongPressTicks) < SuppressionWindow.TotalMilliseconds;
=> IsWithinWindow(Environment.TickCount64, Interlocked.Read(ref _lastLongPressTicks));

/// <summary>
/// The window comparison itself, as a pure function of two tick counts.
/// </summary>
/// <remarks>
/// Split out so the boundary behaviour can be tested without stamping the shared static. The
/// stamp is process-wide and lives for <see cref="SuppressionWindow"/>, so a test that set it
/// could suppress a navigate command in any test class running in parallel.
/// </remarks>
internal static bool IsWithinWindow(long nowTicks, long lastLongPressTicks)
=> nowTicks - lastLongPressTicks < SuppressionWindow.TotalMilliseconds;

/// <summary>Clears the stamp, so a test that sets it cannot leak into a parallel test class.</summary>
internal static void Reset() => Interlocked.Exchange(ref _lastLongPressTicks, 0);
}
22 changes: 19 additions & 3 deletions src/AniSprinkles.Core/Services/FileLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

private sealed class FileLogWriter : IDisposable
{
/// <summary>
/// How long <see cref="Dispose"/> waits for the queued backlog to reach disk. The channel
/// holds at most 2048 lines and each one is a small append, so a healthy drain finishes far
/// inside this; the bound exists for a writer wedged on IO, where blocking shutdown any
/// longer would be worse than losing the tail.
/// </summary>
private static readonly TimeSpan FlushTimeout = TimeSpan.FromSeconds(2);

private readonly string _logDirectory;
private readonly string _baseFileName;
private readonly long _maxFileSizeBytes;
Expand Down Expand Up @@ -131,20 +139,26 @@ public void Dispose()
return;
}

// Order matters here. Completing the writer lets the drain loop finish the backlog and
// exit on its own; cancelling the read token first threw the backlog away instead — and
// the backlog at shutdown is the tail of the log, which is the part a diagnostic report
// is being collected for in the first place.
_disposed = true;
_queue.Writer.TryComplete();
_disposeTokenSource.Cancel();

try
{
_backgroundWriterTask.Wait(TimeSpan.FromSeconds(1));
_backgroundWriterTask.Wait(FlushTimeout);
}
catch
{
// Never throw from logging shutdown.
}
finally
{
// Only after the drain has had its chance: this is the backstop for a writer wedged
// on disk IO, not the normal path.
_disposeTokenSource.Cancel();
_disposeTokenSource.Dispose();
}
}
Expand All @@ -155,7 +169,9 @@ private async Task ProcessQueueAsync()
{
await foreach (var line in _queue.Reader.ReadAllAsync(_disposeTokenSource.Token))
{
if (_disposed || _isFaulted)
// Deliberately not checking _disposed: Dispose sets it before waiting for this
// loop to drain, so bailing on it here would discard the queued tail.
if (_isFaulted)
{
return;
}
Expand Down
Loading