Skip to content

Commit

Permalink
Improve responsiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed Apr 24, 2024
1 parent caab451 commit c30b090
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
9 changes: 8 additions & 1 deletion FreePackages/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Steam;
Expand Down Expand Up @@ -127,7 +128,7 @@ internal static class Commands {
return responses.Count > 0 ? String.Join(Environment.NewLine, responses) : null;
}

private static string? ResponseQueueLicense(Bot bot, EAccess access, string licenses, bool useFilter = false) {
private static string? ResponseQueueLicense(Bot bot, EAccess access, string licenses, bool useFilter = false, [CallerMemberName] string? previousMethodName = null) {
if (access < EAccess.Master) {
return null;
}
Expand Down Expand Up @@ -178,6 +179,10 @@ internal static class Commands {
response.AppendLine(FormatBotResponse(bot, PackageHandler.Handlers[bot.BotName].AddPackage(packageType, gameID, useFilter)));
}

if (previousMethodName == nameof(Response)) {
Utilities.InBackground(async() => await PackageHandler.HandleChanges().ConfigureAwait(false));
}

return response.Length > 0 ? response.ToString() : null;
}

Expand All @@ -196,6 +201,8 @@ internal static class Commands {

List<string?> responses = new(results.Where(result => !String.IsNullOrEmpty(result)));

Utilities.InBackground(async() => await PackageHandler.HandleChanges().ConfigureAwait(false));

return responses.Count > 0 ? String.Join(Environment.NewLine, responses) : null;
}

Expand Down
7 changes: 2 additions & 5 deletions FreePackages/Handlers/PackageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ private async static Task<bool> IsReady(uint maxWaitTimeSeconds = 120) {
}

internal async static Task HandleChanges() {
if (!await ProcessChangesSemaphore.WaitAsync(0).ConfigureAwait(false)) {
return;
}

await ProcessChangesSemaphore.WaitAsync().ConfigureAwait(false);
try {
await IsReady().ConfigureAwait(false);

Expand Down Expand Up @@ -485,4 +482,4 @@ internal void AddPackages(HashSet<uint>? appIDs, HashSet<uint>? packageIDs, bool
PackageQueue.AddPackages(packages);
}
}
}
}
19 changes: 16 additions & 3 deletions FreePackages/Handlers/PackageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,31 @@ internal void AddPackage(Package package, HashSet<uint>? appIDsToRemove = null)
}

if (package.Type == EPackageType.Sub && appIDsToRemove != null) {
// Remove duplicates. Whenever we're trying to activate and app and also an package for that app, get rid of the app. Because error messages for packages are more descriptive and useful.
// Used to remove duplicates.
// Whenever we're trying to activate an app and also an package for that app, get rid of the app.
// I only really like to do this because the error messages for packages are more descriptive and useful.
BotCache.RemoveAppPackages(appIDsToRemove);
}

UpdateTimer(DateTime.Now);
}

internal void AddPackages(IEnumerable<Package> packages) {
if (!BotCache.AddPackages(packages)) {
return;
}

UpdateTimer(DateTime.Now);
}

private async Task ProcessQueue() {
DateTime? lastActivation = BotCache.GetLastActivation();
if (lastActivation != null && lastActivation.Value.AddSeconds(DelayBetweenActivationsSeconds) > DateTime.Now) {
UpdateTimer(lastActivation.Value.AddSeconds(DelayBetweenActivationsSeconds));

return;
}

if (!Bot.IsConnectedAndLoggedOn) {
UpdateTimer(DateTime.Now.AddMinutes(1));

Expand All @@ -67,7 +80,7 @@ private async Task ProcessQueue() {
}

if (BotCache.NumActivationsPastHour() >= ActivationsPerHour) {
DateTime resumeTime = BotCache.GetLastActivation()!.Value.AddHours(1).AddMinutes(1);
DateTime resumeTime = lastActivation!.Value.AddHours(1).AddMinutes(1);
Bot.ArchiLogger.LogGenericInfo(String.Format(Strings.ActivationPaused, String.Format("{0:T}", resumeTime)));
UpdateTimer(resumeTime);

Expand Down Expand Up @@ -285,4 +298,4 @@ internal string GetStatus() {
private static int GetMillisecondsFromNow(DateTime then) => Math.Max(0, (int) (then - DateTime.Now).TotalMilliseconds);
private void UpdateTimer(DateTime then) => Timer?.Change(GetMillisecondsFromNow(then), Timeout.Infinite);
}
}
}
4 changes: 3 additions & 1 deletion FreePackages/IPC/Api/FreePackagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ public ActionResult<GenericResponse> QueueLicenses(string botNames, [FromBody] Q
PackageHandler.Handlers[bot.BotName].AddPackages(request.AppIDs, request.PackageIDs, request.UseFilter);
}

Utilities.InBackground(async() => await PackageHandler.HandleChanges().ConfigureAwait(false));

return Ok(new GenericResponse(true));
}
}
}
}

0 comments on commit c30b090

Please sign in to comment.