mirror of
https://github.com/logos-storage/logos-storage-nim-cs-dist-tests.git
synced 2026-01-06 23:43:07 +00:00
67 lines
1.5 KiB
C#
67 lines
1.5 KiB
C#
using Logging;
|
|
|
|
namespace BiblioTech
|
|
{
|
|
public class CallDispatcher
|
|
{
|
|
private readonly ILog log;
|
|
private readonly object _lock = new object();
|
|
private readonly List<Action> queue = new List<Action>();
|
|
private readonly AutoResetEvent autoResetEvent = new AutoResetEvent(false);
|
|
|
|
public CallDispatcher(ILog log)
|
|
{
|
|
this.log = log;
|
|
}
|
|
|
|
public void Add(Action call)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
queue.Add(call);
|
|
autoResetEvent.Set();
|
|
if (queue.Count > 100)
|
|
{
|
|
log.Error("Queue overflow!");
|
|
queue.Clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
Worker();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("Exception in CallDispatcher: " + ex);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void Worker()
|
|
{
|
|
autoResetEvent.WaitOne();
|
|
var tasks = Array.Empty<Action>();
|
|
|
|
lock (_lock)
|
|
{
|
|
tasks = queue.ToArray();
|
|
queue.Clear();
|
|
}
|
|
|
|
foreach (var task in tasks)
|
|
{
|
|
task();
|
|
}
|
|
}
|
|
}
|
|
}
|