Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Knossos.NET/Models/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public StandaloneServerSettings(MultiCfg multiCfg, string id, string version, in
public int logLevel { get; set; } = 1;
[JsonPropertyName("global_cmdline")]
public string? globalCmdLine { get; set; } = null;
[JsonPropertyName("anti-stuck")]
public bool antiStuck { get; set; } = true;
[JsonPropertyName("force_sse2")]
public bool forceSSE2 { get; set; } = false;
[JsonPropertyName("max_concurrent_subtasks")]
Expand Down Expand Up @@ -724,6 +726,7 @@ public void Load()
closeToTray = tempSettings.closeToTray;
ignoredLauncherUpdates = tempSettings.ignoredLauncherUpdates;
hiddenModIds = tempSettings.hiddenModIds;
antiStuck = tempSettings.antiStuck;
if (hiddenModIds.Any())
{
foreach (var hiddenMod in hiddenModIds)
Expand Down
9 changes: 9 additions & 0 deletions Knossos.NET/ViewModels/GlobalSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public partial class GlobalSettingsViewModel : ViewModelBase
[ObservableProperty]
internal string basePath = string.Empty; //When this is changed settings are saved immediately.

private bool antiStuck = true;
internal bool AntiStuck
{
get { return antiStuck; }
set { if (antiStuck != value) { this.SetProperty(ref antiStuck, value); UnCommitedChanges = true; } }
}

private bool blCfNebula = false;
internal bool BlCfNebula
{
Expand Down Expand Up @@ -669,6 +676,7 @@ public void LoadData()
EnvVars = Knossos.globalSettings.envVars;
ShowDevOptions = Knossos.globalSettings.showDevOptions || NoSystemCMD;
CloseToTray = Knossos.globalSettings.closeToTray;
AntiStuck = Knossos.globalSettings.antiStuck;

/* VIDEO SETTINGS */
//RESOLUTION
Expand Down Expand Up @@ -1249,6 +1257,7 @@ internal void SaveCommand()
Knossos.globalSettings.envVars = EnvVars;
Knossos.globalSettings.showDevOptions = ShowDevOptions;
Knossos.globalSettings.closeToTray = CloseToTray;
Knossos.globalSettings.antiStuck = AntiStuck;

/* VIDEO */
//Resolution
Expand Down
33 changes: 33 additions & 0 deletions Knossos.NET/ViewModels/Templates/Tasks/DownloadFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ private async Task<bool> Download(Uri downloadUrl, string destinationFilePath, F
Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.Download()", "Downloading file: " + downloadUrl);
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

// ANTI-STUCK CONFIG
// If less ANTI_STUCK_MIN_BYTES are downloaded in ANTI_STUCK_CHECK_SECONDS
// The download is restarted with a mirror swap
const int ANTI_STUCK_CHECK_SECONDS = 30;
const long ANTI_STUCK_MIN_BYTES = 102400; // 100 KB (3.33 KB/s)
DateTime lastStallCheck = DateTime.UtcNow;
long bytesAtLastCheck = 0;
// =================================

var httpClient = KnUtils.GetHttpClient();
if (downloadUrl.ToString().ToLower().Contains(".json"))
{
Expand Down Expand Up @@ -219,6 +228,9 @@ private async Task<bool> Download(Uri downloadUrl, string destinationFilePath, F
{
throw new TaskCanceledException();
}
// Reset anti-stuck counter on paused downloads
lastStallCheck = DateTime.UtcNow;
bytesAtLastCheck = totalBytesRead;
}

if (cancellationTokenSource!.IsCancellationRequested)
Expand Down Expand Up @@ -253,6 +265,27 @@ private async Task<bool> Download(Uri downloadUrl, string destinationFilePath, F
stopwatch.Restart();
}

// Anti-Stuck check
if (Knossos.globalSettings.antiStuck && (DateTime.UtcNow - lastStallCheck).TotalSeconds >= ANTI_STUCK_CHECK_SECONDS)
{
long bytesInWindow = totalBytesRead - bytesAtLastCheck;

if (bytesInWindow < ANTI_STUCK_MIN_BYTES)
{
string speedStr = KnUtils.FormatBytes(bytesInWindow / ANTI_STUCK_CHECK_SECONDS) + "/s";
Log.Add(Log.LogSeverity.Warning, "TaskItemViewModel.Download",
$"[ANTI-STUCK] Mirror {CurrentMirror ?? downloadUrl.Host} is stuck (too slow). " +
$"Only {KnUtils.FormatBytes(bytesInWindow)} in {ANTI_STUCK_CHECK_SECONDS}s ({speedStr}). " +
"Changing to a diferent mirror...");

return false;
}

// reset counter
lastStallCheck = DateTime.UtcNow;
bytesAtLastCheck = totalBytesRead;
}
// =================================

if (readCount % 100 == 0)
{
Expand Down
5 changes: 5 additions & 0 deletions Knossos.NET/Views/GlobalSettingsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
<CheckBox Margin="25,0,0,0" IsChecked="{Binding !BlHestia}">hestia.feralhosting.com</CheckBox>
</WrapPanel>
</Grid>
<!-- Anti Stuck -->
<WrapPanel Margin="5,10,0,0" ToolTip.Tip="During file downloads, if less than 100KB are downloaded every 30 seconds, the download will be restarted on a different mirror.">
<Label Margin="0,5,0,0" Width="210">Download anti-stuck</Label>
<ToggleSwitch Margin="1,0,0,0" IsChecked="{Binding AntiStuck}"></ToggleSwitch>
</WrapPanel>
<!-- Log File -->
<WrapPanel Margin="5,10,0,0">
<Label Margin="0,5,0,0" Width="210">Enable Logging</Label>
Expand Down