make mare even less dumpy
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using MareSynchronos.MareConfiguration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
@@ -14,13 +15,15 @@ public sealed class MareMediator : IHostedService
|
||||
private readonly CancellationTokenSource _loopCts = new();
|
||||
private readonly ConcurrentQueue<MessageBase> _messageQueue = new();
|
||||
private readonly PerformanceCollectorService _performanceCollector;
|
||||
private readonly MareConfigService _mareConfigService;
|
||||
private readonly Dictionary<Type, HashSet<SubscriberAction>> _subscriberDict = [];
|
||||
private bool _processQueue = false;
|
||||
private readonly Dictionary<Type, MethodInfo?> _genericExecuteMethods = new();
|
||||
public MareMediator(ILogger<MareMediator> logger, PerformanceCollectorService performanceCollector)
|
||||
public MareMediator(ILogger<MareMediator> logger, PerformanceCollectorService performanceCollector, MareConfigService mareConfigService)
|
||||
{
|
||||
_logger = logger;
|
||||
_performanceCollector = performanceCollector;
|
||||
_mareConfigService = mareConfigService;
|
||||
}
|
||||
|
||||
public void PrintSubscriberInfo()
|
||||
@@ -147,7 +150,7 @@ public sealed class MareMediator : IHostedService
|
||||
var msgType = message.GetType();
|
||||
if (!_genericExecuteMethods.TryGetValue(msgType, out var methodInfo))
|
||||
{
|
||||
_genericExecuteMethods[message.GetType()] = methodInfo = GetType()
|
||||
_genericExecuteMethods[msgType] = methodInfo = GetType()
|
||||
.GetMethod(nameof(ExecuteReflected), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?
|
||||
.MakeGenericMethod(msgType);
|
||||
}
|
||||
@@ -158,14 +161,20 @@ public sealed class MareMediator : IHostedService
|
||||
|
||||
private void ExecuteReflected<T>(List<SubscriberAction> subscribers, T message) where T : MessageBase
|
||||
{
|
||||
var msgTypeName = message.GetType().Name;
|
||||
foreach (SubscriberAction subscriber in subscribers)
|
||||
{
|
||||
try
|
||||
{
|
||||
var isSameThread = message.KeepThreadContext ? "$" : string.Empty;
|
||||
_performanceCollector.LogPerformance(this, $"{isSameThread}Execute>{msgTypeName}+{subscriber.Subscriber.GetType().Name}>{subscriber.Subscriber}",
|
||||
() => ((Action<T>)subscriber.Action).Invoke(message));
|
||||
if (_mareConfigService.Current.LogPerformance)
|
||||
{
|
||||
var isSameThread = message.KeepThreadContext ? "$" : string.Empty;
|
||||
_performanceCollector.LogPerformance(this, $"{isSameThread}Execute>{message.GetType().Name}+{subscriber.Subscriber.GetType().Name}>{subscriber.Subscriber}",
|
||||
() => ((Action<T>)subscriber.Action).Invoke(message));
|
||||
}
|
||||
else
|
||||
{
|
||||
((Action<T>)subscriber.Action).Invoke(message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user