Performance improvements

This commit is contained in:
Alexandru Macocian
2021-09-29 15:52:44 +02:00
parent 867fa12a6c
commit 5664b09ea8
2 changed files with 46 additions and 24 deletions
+3 -3
View File
@@ -5,13 +5,13 @@
<TargetFrameworks>netcoreapp2.1;net48;netstandard2.0;netcoreapp3.1;net5.0</TargetFrameworks>
<ApplicationIcon />
<StartupObject />
<Version>4.0</Version>
<Version>4.1</Version>
<LangVersion>latest</LangVersion>
<Authors>Alexandru-Victor Macocian</Authors>
<Product>MTSC</Product>
<Description>Modular TCP Server and Client</Description>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<FileVersion>4.0.0.0</FileVersion>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<FileVersion>4.1.0.0</FileVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Platforms>AnyCPU;x64</Platforms>
<PackageProjectUrl>https://github.com/AlexMacocian/MTSC</PackageProjectUrl>
+43 -21
View File
@@ -21,6 +21,7 @@ namespace MTSC.ServerSide.Handlers
private readonly List<IHttpLogger> httpLoggers = new();
private readonly Dictionary<HttpMethods, List<(ExtendedUrl, Type,
Func<Server, HttpRequest, ClientData, RouteEnablerResponse>)>> moduleDictionary = new();
private readonly Dictionary<Type, List<(Attribute, PropertyInfo)>> routePropertyCache = new();
public TimeSpan FragmentsExpirationTime { get; set; } = TimeSpan.FromSeconds(15);
public double MaximumRequestSize { get; set; } = double.MaxValue;
@@ -244,6 +245,7 @@ namespace MTSC.ServerSide.Handlers
foreach ((_, var routeType, _) in routes)
{
server.ServiceManager.RegisterTransient(routeType, routeType);
this.PrepareRoutePropertyCache(routeType);
}
}
}
@@ -270,7 +272,7 @@ namespace MTSC.ServerSide.Handlers
var routeEnablerResponse = routeEnabler.Invoke(server, request, client);
if (routeEnablerResponse is RouteEnablerResponse.RouteEnablerResponseAccept)
{
SetModuleProperties(module, request, urlValues);
this.SetModuleProperties(module, request, urlValues);
try
{
module.CallHandleRequest(request).ContinueWith((task) =>
@@ -362,6 +364,29 @@ namespace MTSC.ServerSide.Handlers
this.moduleDictionary[method].Add((new ExtendedUrl(uri), routeType, routeEnabler));
}
private void PrepareRoutePropertyCache(Type routeType)
{
if (this.routePropertyCache.ContainsKey(routeType))
{
return;
}
var properties = routeType.GetProperties();
var propertyAndAttributesList = new List<(Attribute, PropertyInfo)>();
foreach (var property in properties)
{
foreach (var attribute in property.GetCustomAttributes(true))
{
if (attribute is FromUrlAttribute or FromBodyAttribute or FromHeadersAttribute)
{
propertyAndAttributesList.Add(((Attribute)attribute, property));
}
}
}
this.routePropertyCache[routeType] = propertyAndAttributesList;
}
private bool TryMatchUrl(HttpMethods method, string uri, out List<UrlValue> urlValues, out Type type, out Func<Server, HttpRequest, ClientData, RouteEnablerResponse> routeEnabler)
{
urlValues = null;
@@ -382,32 +407,29 @@ namespace MTSC.ServerSide.Handlers
return false;
}
private static void SetModuleProperties(HttpRouteBase module, HttpRequest httpRequest, List<UrlValue> urlValues)
private void SetModuleProperties(HttpRouteBase module, HttpRequest httpRequest, List<UrlValue> urlValues)
{
var properties = module.GetType().GetProperties();
foreach (var property in properties)
var propertiesAndAttributes = this.routePropertyCache[module.GetType()];
foreach ((var attribute, var propertyInfo) in this.routePropertyCache[module.GetType()])
{
foreach (var attribute in property.GetCustomAttributes(true))
if (attribute is FromUrlAttribute fromUrlAttribute)
{
if (attribute is FromUrlAttribute fromUrlAttribute)
var maybeValue = urlValues.Where(val => val.Placeholder == fromUrlAttribute.Placeholder).FirstOrDefault();
if (maybeValue is not null)
{
var maybeValue = urlValues.Where(val => val.Placeholder == fromUrlAttribute.Placeholder).FirstOrDefault();
if (maybeValue is not null)
{
TryAssignValue(property, maybeValue.Value, module);
}
TryAssignValue(propertyInfo, maybeValue.Value, module);
}
else if (attribute is FromBodyAttribute)
}
else if (attribute is FromBodyAttribute)
{
TryAssignValue(propertyInfo, httpRequest.BodyString, module);
}
else if (attribute is FromHeadersAttribute fromHeadersAttribute)
{
var maybeValue = httpRequest.Headers.Where(kvp => kvp.Key == fromHeadersAttribute.HeaderName).FirstOrDefault();
if (maybeValue.Value is not null)
{
TryAssignValue(property, httpRequest.BodyString, module);
}
else if (attribute is FromHeadersAttribute fromHeadersAttribute)
{
var maybeValue = httpRequest.Headers.Where(kvp => kvp.Key == fromHeadersAttribute.HeaderName).FirstOrDefault();
if (maybeValue.Value is not null)
{
TryAssignValue(property, maybeValue.Value, module);
}
TryAssignValue(propertyInfo, maybeValue.Value, module);
}
}
}