mirror of
https://github.com/logos-storage/logos-storage-nim-cs-dist-tests.git
synced 2026-01-05 23:13:08 +00:00
attempt to sort queue by length
This commit is contained in:
parent
79619869b8
commit
bf21705ead
@ -74,10 +74,10 @@ namespace AutoClient
|
||||
(6, 2),
|
||||
(6, 3),
|
||||
(10, 2),
|
||||
(10, 4),
|
||||
(20, 2),
|
||||
(20, 4),
|
||||
(20, 10)
|
||||
//(10, 4),
|
||||
//(20, 2),
|
||||
//(20, 4),
|
||||
//(20, 10)
|
||||
];
|
||||
|
||||
return options[r.Next(0, options.Length)];
|
||||
|
||||
@ -6,6 +6,8 @@ namespace AutoClient
|
||||
{
|
||||
private readonly List<Cdx> instances;
|
||||
private readonly object instanceLock = new object();
|
||||
private readonly App app;
|
||||
private int printDelay = 10;
|
||||
|
||||
private class Cdx
|
||||
{
|
||||
@ -24,6 +26,7 @@ namespace AutoClient
|
||||
}
|
||||
|
||||
public string Id { get; }
|
||||
public int QueueSize => queue.Count;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
@ -82,9 +85,19 @@ namespace AutoClient
|
||||
}
|
||||
}
|
||||
|
||||
private class CdxComparer : IComparer<Cdx>
|
||||
{
|
||||
public int Compare(Cdx? x, Cdx? y)
|
||||
{
|
||||
if (x == null || y == null) return 0;
|
||||
return x.QueueSize - y.QueueSize;
|
||||
}
|
||||
}
|
||||
|
||||
public LoadBalancer(App app, CodexWrapper[] instances)
|
||||
{
|
||||
this.instances = instances.Select(i => new Cdx(app, i)).ToList();
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
@ -101,24 +114,39 @@ namespace AutoClient
|
||||
{
|
||||
lock (instanceLock)
|
||||
{
|
||||
instances.Sort(new CdxComparer());
|
||||
var i = instances.First();
|
||||
instances.RemoveAt(0);
|
||||
instances.Add(i);
|
||||
|
||||
i.Queue(action);
|
||||
}
|
||||
PrintQueue();
|
||||
}
|
||||
|
||||
public void DispatchOnSpecificCodex(Action<CodexWrapper> action, string id)
|
||||
{
|
||||
lock (instanceLock)
|
||||
{
|
||||
instances.Sort(new CdxComparer());
|
||||
var i = instances.Single(a => a.Id == id);
|
||||
instances.Remove(i);
|
||||
instances.Add(i);
|
||||
|
||||
i.Queue(action);
|
||||
}
|
||||
PrintQueue();
|
||||
}
|
||||
|
||||
private void PrintQueue()
|
||||
{
|
||||
printDelay--;
|
||||
if (printDelay > 0) return;
|
||||
printDelay = 10;
|
||||
|
||||
lock (instanceLock)
|
||||
{
|
||||
foreach (var i in instances)
|
||||
{
|
||||
app.Log.Log($"Queue[{i.Id}] = {i.QueueSize} entries");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CheckErrors()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user