wabbajack/Wabbajack.Services.OSIntegrated/ResourceSettingsManager.cs
2021-11-09 22:15:13 -07:00

58 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Wabbajack.RateLimiter;
using Wabbajack.Services.OSIntegrated;
namespace Wabbajack.App.Models;
public class ResourceSettingsManager
{
private readonly SettingsManager _manager;
private Dictionary<string,ResourceSetting>? _settings;
public ResourceSettingsManager(SettingsManager manager)
{
_manager = manager;
}
private SemaphoreSlim _lock = new(1);
public async Task<ResourceSetting> GetSettings(string name)
{
await _lock.WaitAsync();
try
{
_settings ??= await _manager.Load<Dictionary<string, ResourceSetting>>("resource_settings");
if (_settings.TryGetValue(name, out var found)) return found;
var newSetting = new ResourceSetting
{
MaxTasks = Environment.ProcessorCount,
MaxThroughput = long.MaxValue
};
_settings.Add(name, newSetting);
await _manager.Save("resource_settings", _settings);
return _settings[name];
}
finally
{
_lock.Release();
}
}
public class ResourceSetting
{
public long MaxTasks { get; set; }
public long MaxThroughput { get; set; }
}
}