Skip to content

Commit

Permalink
added loging to ConcurrentPacketBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Dec 17, 2023
1 parent 6f313b0 commit 8e09a94
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Downloader/ConcurrentPacketBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Downloader.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -20,6 +21,7 @@ internal class ConcurrentPacketBuffer<T> : IReadOnlyCollection<T>, IDisposable w
{
private volatile bool _disposed = false;
private long _bufferSize = long.MaxValue;
protected readonly ILogger _logger;
protected readonly SemaphoreSlim _queueConsumeLocker = new SemaphoreSlim(0);
protected readonly PauseTokenSource _addingBlocker = new PauseTokenSource();
protected readonly PauseTokenSource _flushBlocker = new PauseTokenSource();
Expand All @@ -34,14 +36,15 @@ public long BufferSize
}
}

public ConcurrentPacketBuffer(long size) : this()
public ConcurrentPacketBuffer(long size, ILogger logger = null) : this(logger)
{
BufferSize = size;
}

public ConcurrentPacketBuffer()
public ConcurrentPacketBuffer(ILogger logger = null)
{
_queue = new ConcurrentQueue<T>();
_logger = logger;
}

public IEnumerator<T> GetEnumerator()
Expand Down Expand Up @@ -100,7 +103,7 @@ private void StopAddingIfLimitationExceeded(long packetSize)
{
if (BufferSize < packetSize * Count)
{
// Stop writing packets to the queue until the memory is free
_logger.LogDebug($"ConcurrentPacketBuffer: Stop writing packets to the queue on size {packetSize * Count}bytes until the memory is free");
StopAdding();
}
}
Expand All @@ -121,19 +124,20 @@ public async Task WaitToComplete()

public void StopAdding()
{
// stop writing new items to the list by blocking writer threads
_logger.LogDebug("ConcurrentPacketBuffer: stop writing new items to the list by blocking writer threads");
_addingBlocker.Pause();
}

public void ResumeAdding()
{
// resume writing new item to the list
_logger.LogDebug("ConcurrentPacketBuffer: resume writing new item to the list");
_addingBlocker.Resume();
}

public void Dispose()
{
if(_disposed) return;
if (_disposed)
return;

_disposed = true;
StopAdding();
Expand Down

0 comments on commit 8e09a94

Please sign in to comment.