small startup performance optimization

This commit is contained in:
RaidMax 2022-01-28 09:35:01 -06:00
parent 73036dc1c7
commit f4b160b735
28 changed files with 112 additions and 67 deletions

View File

@ -355,9 +355,9 @@ namespace IW4MAdmin.Application
// copy over default config if it doesn't exist
if (!_appConfig.Servers?.Any() ?? true)
{
var defaultConfig = new BaseConfigurationHandler<DefaultSettings>("DefaultSettings").Configuration();
//ConfigHandler.Set((ApplicationConfiguration)new ApplicationConfiguration().Generate());
//var newConfig = ConfigHandler.Configuration();
var defaultHandler = new BaseConfigurationHandler<DefaultSettings>("DefaultSettings");
await defaultHandler.BuildAsync();
var defaultConfig = defaultHandler.Configuration();
_appConfig.AutoMessages = defaultConfig.AutoMessages;
_appConfig.GlobalRules = defaultConfig.GlobalRules;

View File

@ -1,4 +1,5 @@
using IW4MAdmin.Application.Misc;
using System.Threading.Tasks;
using IW4MAdmin.Application.Misc;
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Application.Factories
@ -17,7 +18,17 @@ namespace IW4MAdmin.Application.Factories
/// <returns></returns>
public IConfigurationHandler<T> GetConfigurationHandler<T>(string name) where T : IBaseConfiguration
{
return new BaseConfigurationHandler<T>(name);
var handler = new BaseConfigurationHandler<T>(name);
handler.BuildAsync().Wait();
return handler;
}
/// <inheritdoc/>
public async Task<IConfigurationHandler<T>> GetConfigurationHandlerAsync<T>(string name) where T : IBaseConfiguration
{
var handler = new BaseConfigurationHandler<T>(name);
await handler.BuildAsync();
return handler;
}
}
}

View File

@ -355,7 +355,7 @@ namespace IW4MAdmin
try
{
var factory = _serviceProvider.GetRequiredService<IDatabaseContextFactory>();
await using var context = factory.CreateContext();
await using var context = factory.CreateContext(enableTracking: false);
var messageCount = await context.InboxMessages
.CountAsync(msg => msg.DestinationClientId == E.Origin.ClientId && !msg.IsDelivered);

View File

@ -341,7 +341,11 @@ namespace IW4MAdmin.Application
// setup the static resources (config/master api/translations)
var serviceCollection = new ServiceCollection();
var appConfigHandler = new BaseConfigurationHandler<ApplicationConfiguration>("IW4MAdminSettings");
await appConfigHandler.BuildAsync();
var defaultConfigHandler = new BaseConfigurationHandler<DefaultSettings>("DefaultSettings");
await defaultConfigHandler.BuildAsync();
var commandConfigHandler = new BaseConfigurationHandler<CommandConfiguration>("CommandConfiguration");
await commandConfigHandler.BuildAsync();
var defaultConfig = defaultConfigHandler.Configuration();
var appConfig = appConfigHandler.Configuration();
var masterUri = Utilities.IsDevelopment
@ -380,9 +384,7 @@ namespace IW4MAdmin.Application
.AddSingleton<IServiceCollection>(serviceCollection)
.AddSingleton<IConfigurationHandler<DefaultSettings>, BaseConfigurationHandler<DefaultSettings>>()
.AddSingleton((IConfigurationHandler<ApplicationConfiguration>) appConfigHandler)
.AddSingleton(
new BaseConfigurationHandler<CommandConfiguration>("CommandConfiguration") as
IConfigurationHandler<CommandConfiguration>)
.AddSingleton<IConfigurationHandler<CommandConfiguration>>(commandConfigHandler)
.AddSingleton(appConfig)
.AddSingleton(serviceProvider =>
serviceProvider.GetRequiredService<IConfigurationHandler<CommandConfiguration>>()

View File

@ -1,11 +1,13 @@
using Newtonsoft.Json;
using SharedLibraryCore;
using SharedLibraryCore;
using SharedLibraryCore.Exceptions;
using SharedLibraryCore.Interfaces;
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace IW4MAdmin.Application.Misc
{
@ -15,19 +17,24 @@ namespace IW4MAdmin.Application.Misc
/// <typeparam name="T">base configuration type</typeparam>
public class BaseConfigurationHandler<T> : IConfigurationHandler<T> where T : IBaseConfiguration
{
T _configuration;
private T _configuration;
private readonly SemaphoreSlim _onSaving;
private readonly JsonSerializerOptions _serializerOptions;
public BaseConfigurationHandler(string fn)
public BaseConfigurationHandler(string fileName)
{
_serializerOptions = new JsonSerializerOptions
{
WriteIndented = true,
};
_serializerOptions.Converters.Add(new JsonStringEnumConverter());
_onSaving = new SemaphoreSlim(1, 1);
FileName = Path.Join(Utilities.OperatingDirectory, "Configuration", $"{fn}.json");
Build();
FileName = Path.Join(Utilities.OperatingDirectory, "Configuration", $"{fileName}.json");
}
public BaseConfigurationHandler() : this(typeof(T).Name)
{
_onSaving = new SemaphoreSlim(1, 1);
}
~BaseConfigurationHandler()
@ -37,12 +44,12 @@ namespace IW4MAdmin.Application.Misc
public string FileName { get; }
public void Build()
public async Task BuildAsync()
{
try
{
var configContent = File.ReadAllText(FileName);
_configuration = JsonConvert.DeserializeObject<T>(configContent);
await using var fileStream = File.OpenRead(FileName);
_configuration = await JsonSerializer.DeserializeAsync<T>(fileStream, _serializerOptions);
}
catch (FileNotFoundException)
@ -65,14 +72,9 @@ namespace IW4MAdmin.Application.Misc
try
{
await _onSaving.WaitAsync();
var settings = new JsonSerializerSettings()
{
Formatting = Formatting.Indented
};
settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
var appConfigJson = JsonConvert.SerializeObject(_configuration, settings);
await File.WriteAllTextAsync(FileName, appConfigJson);
await using var fileStream = File.OpenWrite(FileName);
await JsonSerializer.SerializeAsync(fileStream, _configuration, _serializerOptions);
}
finally

View File

@ -168,9 +168,6 @@ namespace IW4MAdmin.Application.Misc
}
}
_scriptEngine.SetValue("_configHandler", new ScriptPluginConfigurationWrapper(Name, _scriptEngine));
await OnLoadAsync(manager);
try
{
if (pluginObject.isParser)
@ -181,6 +178,14 @@ namespace IW4MAdmin.Application.Misc
manager.AdditionalEventParsers.Add(eventParser);
manager.AdditionalRConParsers.Add(rconParser);
}
else
{
var configWrapper = new ScriptPluginConfigurationWrapper(Name, _scriptEngine);
await configWrapper.InitializeAsync();
_scriptEngine.SetValue("_configHandler", configWrapper);
await OnLoadAsync(manager);
}
}
catch (RuntimeBinderException) { }

View File

@ -12,19 +12,24 @@ namespace IW4MAdmin.Application.Misc
public class ScriptPluginConfigurationWrapper
{
private readonly BaseConfigurationHandler<ScriptPluginConfiguration> _handler;
private readonly ScriptPluginConfiguration _config;
private ScriptPluginConfiguration _config;
private readonly string _pluginName;
private readonly Engine _scriptEngine;
public ScriptPluginConfigurationWrapper(string pluginName, Engine scriptEngine)
{
_handler = new BaseConfigurationHandler<ScriptPluginConfiguration>("ScriptPluginSettings");
_config = _handler.Configuration() ??
(ScriptPluginConfiguration) new ScriptPluginConfiguration().Generate();
_pluginName = pluginName;
_scriptEngine = scriptEngine;
}
public async Task InitializeAsync()
{
await _handler.BuildAsync();
_config = _handler.Configuration() ??
(ScriptPluginConfiguration) new ScriptPluginConfiguration().Generate();
}
private static int? AsInteger(double d)
{
return int.TryParse(d.ToString(CultureInfo.InvariantCulture), out var parsed) ? parsed : (int?) null;

View File

@ -23,7 +23,7 @@ namespace Data.Context
{
var link = new EFAliasLink();
context.Clients.Add(new EFClient()
context.Clients.Add(new EFClient
{
Active = false,
Connections = 0,
@ -33,7 +33,7 @@ namespace Data.Context
Masked = true,
NetworkId = 0,
AliasLink = link,
CurrentAlias = new EFAlias()
CurrentAlias = new EFAlias
{
Link = link,
Active = true,

View File

@ -102,7 +102,7 @@ namespace Data.Helpers
{
try
{
await using var context = _contextFactory.CreateContext();
await using var context = _contextFactory.CreateContext(false);
_cachedItems = await context.Set<T>().ToDictionaryAsync(item => item.Id);
}
catch (Exception ex)

View File

@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.25.2" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.28.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -65,6 +65,7 @@ namespace AutomessageFeed
public async Task OnLoadAsync(IManager manager)
{
await _configurationHandler.BuildAsync();
if (_configurationHandler.Configuration() == null)
{
_configurationHandler.Set((Configuration)new Configuration().Generate());

View File

@ -10,7 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.25.2" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.28.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -1,29 +1,25 @@
using LiveRadar.Configuration;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SharedLibraryCore;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Interfaces;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace LiveRadar.Web.Controllers
{
public class RadarController : BaseController
{
private static readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
};
private readonly IManager _manager;
private readonly LiveRadarConfiguration _config;
private static LiveRadarConfiguration _config;
private readonly IConfigurationHandler<LiveRadarConfiguration> _configurationHandler;
public RadarController(IManager manager, IConfigurationHandlerFactory configurationHandlerFactory) : base(manager)
{
_manager = manager;
_config = configurationHandlerFactory.GetConfigurationHandler<LiveRadarConfiguration>("LiveRadarConfiguration").Configuration() ?? new LiveRadarConfiguration();
_configurationHandler =
configurationHandlerFactory.GetConfigurationHandler<LiveRadarConfiguration>("LiveRadarConfiguration");
}
[HttpGet]
@ -46,7 +42,7 @@ namespace LiveRadar.Web.Controllers
[HttpGet]
[Route("Radar/{serverId}/Map")]
public IActionResult Map(long? serverId = null)
public async Task<IActionResult> Map(long? serverId = null)
{
var server = serverId == null ? _manager.GetServers().FirstOrDefault() : _manager.GetServers().FirstOrDefault(_server => _server.EndPoint == serverId);
@ -55,6 +51,12 @@ namespace LiveRadar.Web.Controllers
return NotFound();
}
if (_config == null)
{
await _configurationHandler.BuildAsync();
_config = _configurationHandler.Configuration() ?? new LiveRadarConfiguration();
}
var map = _config.Maps.FirstOrDefault(_map => _map.Name == server.CurrentMap.Name);
if (map == null)

View File

@ -23,7 +23,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.25.2" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.28.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -103,6 +103,7 @@ namespace LiveRadar
public async Task OnLoadAsync(IManager manager)
{
await _configurationHandler.BuildAsync();
if (_configurationHandler.Configuration() == null)
{
_configurationHandler.Set((LiveRadarConfiguration)new LiveRadarConfiguration().Generate());

View File

@ -19,7 +19,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.25.2" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.28.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -76,6 +76,7 @@ namespace IW4MAdmin.Plugins.Login
{
AuthorizedClients = new ConcurrentDictionary<int, bool>();
await _configHandler.BuildAsync();
if (_configHandler.Configuration() == null)
{
_configHandler.Set((Configuration)new Configuration().Generate());

View File

@ -109,6 +109,7 @@ namespace IW4MAdmin.Plugins.ProfanityDeterment
public async Task OnLoadAsync(IManager manager)
{
await _configHandler.BuildAsync();
if (_configHandler.Configuration() == null)
{
_configHandler.Set((Configuration)new Configuration().Generate());

View File

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.25.2" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.28.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -48,6 +48,7 @@ namespace Stats.Client
await LoadServers();
_distributionCache.SetCacheItem((async (set, token) =>
{
await _configurationHandler.BuildAsync();
var validPlayTime = _configurationHandler.Configuration()?.TopPlayersMinPlayTime ?? 3600 * 3;
var distributions = new Dictionary<long, Extensions.LogParams>();
@ -73,6 +74,7 @@ namespace Stats.Client
_maxZScoreCache.SetCacheItem(async (set, token) =>
{
await _configurationHandler.BuildAsync();
var validPlayTime = _configurationHandler.Configuration()?.TopPlayersMinPlayTime ?? 3600 * 3;
var zScore = await set

View File

@ -172,6 +172,7 @@ namespace IW4MAdmin.Plugins.Stats
public async Task OnLoadAsync(IManager manager)
{
await Config.BuildAsync();
// load custom configuration
if (Config.Configuration() == null)
{

View File

@ -17,7 +17,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.25.2" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.28.1" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -35,6 +35,7 @@ namespace IW4MAdmin.Plugins.Welcome
public async Task OnLoadAsync(IManager manager)
{
await _configHandler.BuildAsync();
if (_configHandler.Configuration() == null)
{
_configHandler.Set((WelcomeConfiguration) new WelcomeConfiguration().Generate());

View File

@ -20,7 +20,7 @@
</Target>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.25.2" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.1.28.1" PrivateAssets="All" />
</ItemGroup>
</Project>

View File

@ -6,7 +6,7 @@ namespace SharedLibraryCore.Interfaces
{
string FileName { get; }
Task Save();
void Build();
Task BuildAsync();
T Configuration();
void Set(T config);
}

View File

@ -1,17 +1,27 @@
namespace SharedLibraryCore.Interfaces
using System.Threading.Tasks;
namespace SharedLibraryCore.Interfaces
{
/// <summary>
/// defines the capabilities of the configuration handler factory
/// used to generate new instance of configuration handlers
/// defines the capabilities of the configuration handler factory
/// used to generate new instance of configuration handlers
/// </summary>
public interface IConfigurationHandlerFactory
{
/// <summary>
/// generates a new configuration handler
/// generates a new configuration handler
/// </summary>
/// <typeparam name="T">base configuration type</typeparam>
/// <param name="name">file name of configuration</param>
/// <returns>new configuration handler instance</returns>
IConfigurationHandler<T> GetConfigurationHandler<T>(string name) where T : IBaseConfiguration;
/// <summary>
/// generates a new configuration handler and builds the configuration automatically
/// </summary>
/// <typeparam name="T">base configuration type</typeparam>
/// <param name="name">file name of configuration</param>
/// <returns>new configuration handler instance</returns>
Task<IConfigurationHandler<T>> GetConfigurationHandlerAsync<T>(string name) where T : IBaseConfiguration;
}
}

View File

@ -4,7 +4,7 @@
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<PackageId>RaidMax.IW4MAdmin.SharedLibraryCore</PackageId>
<Version>2022.01.25.2</Version>
<Version>2022.01.28.1</Version>
<Authors>RaidMax</Authors>
<Company>Forever None</Company>
<Configurations>Debug;Release;Prerelease</Configurations>
@ -19,7 +19,7 @@
<IsPackable>true</IsPackable>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>Shared Library for IW4MAdmin</Description>
<PackageVersion>2022.01.25.2</PackageVersion>
<PackageVersion>2022.01.28.1</PackageVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Prerelease|AnyCPU'">