From a547d6ebbfe113b7af43132d6872178601e3e1c5 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Wed, 22 Nov 2023 14:55:46 +0100 Subject: [PATCH] refactor: handler invocation performance --- Server/LoggerMessages/PacketLoggerMessages.cs | 2 +- Server/PacketHandlers/IPacketHandler.cs | 18 ++++++++++++- Server/Services/PacketDistributorService.cs | 27 +++++++++++++------ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Server/LoggerMessages/PacketLoggerMessages.cs b/Server/LoggerMessages/PacketLoggerMessages.cs index 3569674..8c31fd1 100644 --- a/Server/LoggerMessages/PacketLoggerMessages.cs +++ b/Server/LoggerMessages/PacketLoggerMessages.cs @@ -41,7 +41,7 @@ internal static partial class PacketLoggerMessages Message = "No PacketHandlers have been found")] public static partial void NoPacketHandlersFound(this ILogger logger); - [LoggerMessage(EventId = 8, Level = LogLevel.Information, + [LoggerMessage(EventId = 8, Level = LogLevel.Trace, Message = "Packet data {PacketData}")] public static partial void PacketData(this ILogger logger, string packetData); diff --git a/Server/PacketHandlers/IPacketHandler.cs b/Server/PacketHandlers/IPacketHandler.cs index 19710dd..f1d8ef6 100644 --- a/Server/PacketHandlers/IPacketHandler.cs +++ b/Server/PacketHandlers/IPacketHandler.cs @@ -7,8 +7,24 @@ using Wonderking.Packets; namespace Server.PacketHandlers; [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] -public interface IPacketHandler where T : IPacket +public interface IPacketHandler : IPacketHandler where T : IPacket { [UsedImplicitly] public Task HandleAsync(T packet, TcpSession session); + + async Task IPacketHandler.TryHandleAsync(IPacket packet, TcpSession session) + { + if (packet is T tPacket) + { + await HandleAsync(tPacket, session); + return true; + } + + return false; + } +} + +public interface IPacketHandler +{ + Task TryHandleAsync(IPacket packet, TcpSession session); } diff --git a/Server/Services/PacketDistributorService.cs b/Server/Services/PacketDistributorService.cs index ca1412a..5b3e6aa 100644 --- a/Server/Services/PacketDistributorService.cs +++ b/Server/Services/PacketDistributorService.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Collections.Immutable; +using System.Diagnostics; using System.Reflection; using DotNext.Collections.Generic; using DotNext.Linq.Expressions; @@ -32,13 +33,15 @@ public class PacketDistributorService : IHostedService private ImmutableDictionary> _deserializationMap; - private ConcurrentDictionary _packetHandlersInstantiation; + private ConcurrentDictionary _packetHandlersInstantiation; + private readonly ActivitySource _activitySource; public PacketDistributorService(ILogger logger, IServiceProvider serviceProvider) { _concurrentQueue = new ConcurrentQueue(); _logger = logger; _serviceProvider = serviceProvider; + _activitySource = new ActivitySource(nameof(Server)); } public Task StartAsync(CancellationToken cancellationToken) @@ -49,13 +52,13 @@ public class PacketDistributorService : IHostedService var wonderkingAssembly = Assembly.GetAssembly(typeof(IPacket)); var packetsTypes = GetPacketsWithId(wonderkingAssembly); var packetHandlers = GetAllPacketHandlersWithId(Assembly.GetExecutingAssembly()); - _packetHandlersInstantiation = new ConcurrentDictionary(); + _packetHandlersInstantiation = new ConcurrentDictionary(); packetHandlers.ForEach(x => { var packetHandler = ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, x.Value); - _packetHandlersInstantiation.TryAdd(x.Key, packetHandler); + _packetHandlersInstantiation.TryAdd(x.Key, packetHandler as IPacketHandler); }); foreach (var packetsType in packetsTypes) { @@ -152,6 +155,7 @@ public class PacketDistributorService : IHostedService private void InvokePacketHandler(RawPacket item) { + IPacket packet; _logger.PacketDequeued(item.Session.Id, item.OperationCode); if (!_deserializationMap.TryGetValue(item.OperationCode, out var value)) { @@ -159,12 +163,19 @@ public class PacketDistributorService : IHostedService return; } - var packet = value(item.MessageBody); - _logger.PacketData(JsonConvert.SerializeObject(packet)); - _packetHandlersInstantiation[item.OperationCode].GetType() - .GetMethod(nameof(IPacketHandler.HandleAsync)) - ?.Invoke(_packetHandlersInstantiation[item.OperationCode], new object[] { packet, item.Session }); + using (var packetParsingActivity = _activitySource.StartActivity("PacketParsing")) + { + packetParsingActivity?.SetTag("PacketId", item.OperationCode); + packet = value(item.MessageBody); + } + using (var packetHandlerActivity = _activitySource.StartActivity("PacketHandler")) + { + packetHandlerActivity?.SetTag("PacketId", item.OperationCode); + _ = _packetHandlersInstantiation[item.OperationCode].TryHandleAsync(packet, item.Session); + } + + _logger.PacketData(JsonConvert.SerializeObject(packet)); _logger.PacketFinished(item.Session.Id, item.OperationCode); } }