-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathLogBufferPool.cs
More file actions
40 lines (33 loc) · 1.04 KB
/
LogBufferPool.cs
File metadata and controls
40 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections.Concurrent;
using ColumnizerLib;
namespace LogExpert.Core.Classes.Log;
public sealed class LogBufferPool (int maxSize)
{
private readonly ConcurrentBag<LogBuffer> _pool = [];
private readonly int _maxSize = maxSize;
public LogBuffer Rent (ILogFileInfo fileInfo, int maxLines)
{
if (_pool.TryTake(out var buffer))
{
buffer.Reinitialise(fileInfo, maxLines);
return buffer;
}
return new LogBuffer(fileInfo, maxLines);
}
/// <summary>
/// Returns a <see cref="LogBuffer"/> to the pool for reuse.
/// </summary>
/// <remarks>
/// Disposing the buffer's content is handled by this method, so callers should not dispose the buffer themselves.
/// </remarks>
/// <param name="buffer">The buffer to return.</param>
public void Return (LogBuffer buffer)
{
ArgumentNullException.ThrowIfNull(buffer);
buffer.DisposeContent();
if (_pool.Count < _maxSize)
{
_pool.Add(buffer);
}
}
}