Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Ensure fairness in low/high priority distributions for connection usage.

nzbdav-dev7af47c6ed05c
diff --git a/backend/Clients/Usenet/Concurrency/PrioritizedSemaphore.cs b/backend/Clients/Usenet/Concurrency/PrioritizedSemaphore.cs
index a5651f95..ed46799c 100644
--- a/backend/Clients/Usenet/Concurrency/PrioritizedSemaphore.cs
+++ b/backend/Clients/Usenet/Concurrency/PrioritizedSemaphore.cs
@@ -23,14 +23,14 @@ public class PrioritizedSemaphore : IDisposable
private int _enteredCount;
private bool _disposed = false;
private readonly Lock _lock = new();
- private readonly Random _random = new();
+ private int _accumulatedOdds;
public PrioritizedSemaphore(int initialAllowed, int maxAllowed, SemaphorePriorityOdds? priorityOdds = null)
{
ArgumentOutOfRangeException.ThrowIfNegative(initialAllowed);
ArgumentOutOfRangeException.ThrowIfNegative(maxAllowed);
ArgumentOutOfRangeException.ThrowIfGreaterThan(initialAllowed, maxAllowed);
- _priorityOdds = priorityOdds ?? new SemaphorePriorityOdds { HighPriorityOdds = 1 };
+ _priorityOdds = priorityOdds ?? new SemaphorePriorityOdds { HighPriorityOdds = 100 };
_enteredCount = maxAllowed - initialAllowed;
_maxAllowed = maxAllowed;
}
@@ -114,13 +114,15 @@ public class PrioritizedSemaphore : IDisposable
{
// if there are both high-priority waiters and low-priority waiters,
// then roll the dice to determine which to release, based on the given odds.
- var result = _random.NextDouble();
+ _accumulatedOdds += _priorityOdds.LowPriorityOdds;
var (one, two) = (_highPriorityWaiters, _lowPriorityWaiters);
- if (result > _priorityOdds.HighPriorityOdds)
+ if (_accumulatedOdds >= 100)
+ {
(one, two) = (two, one);
- toRelease = Release(one);
- if (toRelease == null)
- Release(two);
+ _accumulatedOdds -= 100;
+ }
+
+ toRelease = Release(one) ?? Release(two);
}
if (toRelease == null)
diff --git a/backend/Clients/Usenet/Concurrency/SemaphorePriorityOdds.cs b/backend/Clients/Usenet/Concurrency/SemaphorePriorityOdds.cs
index 6cadbbbb..87732b11 100644
--- a/backend/Clients/Usenet/Concurrency/SemaphorePriorityOdds.cs
+++ b/backend/Clients/Usenet/Concurrency/SemaphorePriorityOdds.cs
@@ -2,6 +2,6 @@
public record SemaphorePriorityOdds
{
- public required double HighPriorityOdds { get; set; }
- public double LowPriorityOdds => 1.0 - HighPriorityOdds;
+ public required int HighPriorityOdds { get; set; }
+ public int LowPriorityOdds => 100 - HighPriorityOdds;
}
\ No newline at end of file
diff --git a/backend/Config/ConfigManager.cs b/backend/Config/ConfigManager.cs
index 2c14ce2e..ff0c4b93 100644
--- a/backend/Config/ConfigManager.cs
+++ b/backend/Config/ConfigManager.cs
@@ -143,7 +143,7 @@ public class ConfigManager
{
var stringValue = StringUtil.EmptyToNull(GetConfigValue("usenet.streaming-priority"));
var numericalValue = int.Parse(stringValue ?? "80");
- return new SemaphorePriorityOdds() { HighPriorityOdds = numericalValue / 100.0 };
+ return new SemaphorePriorityOdds() { HighPriorityOdds = numericalValue };
}
public bool IsEnforceReadonlyWebdavEnabled()