move marehub to strong api usage (#14)
* move marehub to strong api usage * fix merge issues * latest api * further optimizations
This commit is contained in:
		| @@ -19,7 +19,10 @@ public class GrpcClientIdentificationService : IHostedService | ||||
|     private readonly IdentificationService.IdentificationServiceClient _grpcIdentClient; | ||||
|     private readonly MareMetrics _metrics; | ||||
|     protected ConcurrentDictionary<string, string> OnlineClients = new(StringComparer.Ordinal); | ||||
|     private ConcurrentDictionary<string, string> RemoteCachedIdents = new(StringComparer.Ordinal); | ||||
|     private readonly TimeSpan InvalidateCachedIdent = TimeSpan.FromSeconds(30); | ||||
|     private bool _grpcIsFaulty = false; | ||||
|     private CancellationTokenSource _cacheVerificationCts = new(); | ||||
|  | ||||
|     public GrpcClientIdentificationService(ILogger<GrpcClientIdentificationService> logger, IdentificationService.IdentificationServiceClient gprcIdentClient, MareMetrics metrics, IConfiguration configuration) | ||||
|     { | ||||
| @@ -30,6 +33,46 @@ public class GrpcClientIdentificationService : IHostedService | ||||
|         _metrics = metrics; | ||||
|     } | ||||
|  | ||||
|     private async Task HandleCacheVerification(CancellationToken ct) | ||||
|     { | ||||
|         while (!ct.IsCancellationRequested) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 if (RemoteCachedIdents.Any()) | ||||
|                 { | ||||
|                     MultiUidMessage req = new(); | ||||
|                     req.Uids.AddRange(RemoteCachedIdents.Select(k => new UidMessage() { Uid = k.Key })); | ||||
|                     var cacheResponse = await InvokeOnGrpc(_grpcIdentClient.ValidateCachedIdentsAsync(req)).ConfigureAwait(false); | ||||
|  | ||||
|                     foreach (var entry in cacheResponse.UidWithIdent) | ||||
|                     { | ||||
|                         if (!RemoteCachedIdents.TryGetValue(entry.Uid.Uid, out var ident)) | ||||
|                         { | ||||
|                             continue; | ||||
|                         } | ||||
|  | ||||
|                         if (string.IsNullOrEmpty(entry.Ident.Ident)) | ||||
|                         { | ||||
|                             RemoteCachedIdents.TryRemove(entry.Uid.Uid, out var _); | ||||
|                             continue; | ||||
|                         } | ||||
|  | ||||
|                         RemoteCachedIdents[entry.Uid.Uid] = entry.Ident.Ident; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 _logger.LogWarning(ex, "Error during cached idents verification"); | ||||
|             } | ||||
|             finally | ||||
|             { | ||||
|                 await Task.Delay(InvalidateCachedIdent, ct).ConfigureAwait(false); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public async Task<string?> GetCharacterIdentForUid(string uid) | ||||
|     { | ||||
|         if (OnlineClients.TryGetValue(uid, out string ident)) | ||||
| @@ -37,8 +80,14 @@ public class GrpcClientIdentificationService : IHostedService | ||||
|             return ident; | ||||
|         } | ||||
|  | ||||
|         if (RemoteCachedIdents.TryGetValue(uid, out var cachedIdent)) | ||||
|         { | ||||
|             return cachedIdent; | ||||
|         } | ||||
|  | ||||
|         var result = await InvokeOnGrpc(_grpcIdentClient.GetIdentForUidAsync(new UidMessage { Uid = uid })).ConfigureAwait(false); | ||||
|         if (result == default(CharacterIdentMessage)) return null; | ||||
|         RemoteCachedIdents[uid] = result.Ident; | ||||
|         return result.Ident; | ||||
|     } | ||||
|  | ||||
| @@ -91,10 +140,13 @@ public class GrpcClientIdentificationService : IHostedService | ||||
|     public async Task StartAsync(CancellationToken cancellationToken) | ||||
|     { | ||||
|         await ExecuteOnGrpc(_grpcIdentClient.ClearIdentsForServerAsync(new ServerMessage() { ServerId = _shardName })).ConfigureAwait(false); | ||||
|  | ||||
|         _ = HandleCacheVerification(_cacheVerificationCts.Token); | ||||
|     } | ||||
|  | ||||
|     public async Task StopAsync(CancellationToken cancellationToken) | ||||
|     { | ||||
|         _cacheVerificationCts.Cancel(); | ||||
|         await ExecuteOnGrpc(_grpcIdentClient.ClearIdentsForServerAsync(new ServerMessage() { ServerId = _shardName })).ConfigureAwait(false); | ||||
|     } | ||||
|  | ||||
| @@ -156,4 +208,4 @@ public class GrpcClientIdentificationService : IHostedService | ||||
|             _logger.LogWarning("GRPC connection is faulty"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| } | ||||
| @@ -7,6 +7,7 @@ using MareSynchronosServer.Hubs; | ||||
| using MareSynchronosShared.Data; | ||||
| using MareSynchronosShared.Metrics; | ||||
| using Microsoft.AspNetCore.SignalR; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Microsoft.Extensions.Hosting; | ||||
| @@ -20,12 +21,11 @@ public class SystemInfoService : IHostedService, IDisposable | ||||
|     private readonly IServiceProvider _services; | ||||
|     private readonly GrpcClientIdentificationService _clientIdentService; | ||||
|     private readonly ILogger<SystemInfoService> _logger; | ||||
|     private readonly IHubContext<MareHub> _hubContext; | ||||
|     private readonly IHubContext<MareHub, IMareHub> _hubContext; | ||||
|     private Timer _timer; | ||||
|     private string _shardName; | ||||
|     public SystemInfoDto SystemInfoDto { get; private set; } = new(); | ||||
|  | ||||
|     public SystemInfoService(MareMetrics mareMetrics, IConfiguration configuration, IServiceProvider services, GrpcClientIdentificationService clientIdentService, ILogger<SystemInfoService> logger, IHubContext<MareHub> hubContext) | ||||
|     public SystemInfoService(MareMetrics mareMetrics, IConfiguration configuration, IServiceProvider services, GrpcClientIdentificationService clientIdentService, ILogger<SystemInfoService> logger, IHubContext<MareHub, IMareHub> hubContext) | ||||
|     { | ||||
|         _mareMetrics = mareMetrics; | ||||
|         _services = services; | ||||
| @@ -58,16 +58,16 @@ public class SystemInfoService : IHostedService, IDisposable | ||||
|                 OnlineUsers = (int)_clientIdentService.GetOnlineUsers().Result, | ||||
|             }; | ||||
|  | ||||
|             _hubContext.Clients.All.SendAsync(Api.OnUpdateSystemInfo, SystemInfoDto); | ||||
|             _hubContext.Clients.All.Client_UpdateSystemInfo(SystemInfoDto); | ||||
|  | ||||
|             using var scope = _services.CreateScope(); | ||||
|             using var db = scope.ServiceProvider.GetService<MareDbContext>()!; | ||||
|  | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugePairs, db.ClientPairs.Count()); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugePairsPaused, db.ClientPairs.Count(p => p.IsPaused)); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroups, db.Groups.Count()); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairs, db.GroupPairs.Count()); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairsPaused, db.GroupPairs.Count(p => p.IsPaused)); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugePairs, db.ClientPairs.AsNoTracking().Count()); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugePairsPaused, db.ClientPairs.AsNoTracking().Count(p => p.IsPaused)); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroups, db.Groups.AsNoTracking().Count()); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairs, db.GroupPairs.AsNoTracking().Count()); | ||||
|             _mareMetrics.SetGaugeTo(MetricsAPI.GaugeGroupPairsPaused, db.GroupPairs.AsNoTracking().Count(p => p.IsPaused)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 rootdarkarchon
					rootdarkarchon