Эх сурвалжийг харах

1. fix large amount diconnect and connect
2. remote slq connection recalling .Open()

Robert 1 жил өмнө
parent
commit
5f1be26467

+ 0 - 1
EVCB_OCPP.WSServer/Message/CoreProfileHandler.cs

@@ -1675,7 +1675,6 @@ public partial class ProfileHandler
         string sqlstatement = "SELECT [CarNum] FROM VehicleCustomer where IdTag = '" + startIdTag + "'";
         using (var dbConn = await webDbConnectionFactory.CreateAsync())
         {
-            dbConn.Open();
             toReturn = await dbConn.ExecuteScalarAsync<string>(sqlstatement);
         }
 

+ 7 - 8
EVCB_OCPP.WSServer/Service/MainDbService.cs

@@ -382,15 +382,14 @@ public class MainDbService : IMainDbService
 
     public async Task UpdateMachineConnectionType(string chargeBoxId, int connectionType)
     {
-        using (var db = await contextFactory.CreateDbContextAsync())
-        {
-            var machine = await db.Machines.Where(x => x.ChargeBoxId == chargeBoxId).FirstOrDefaultAsync();
-            if (machine != null)
-            {
-                machine.ConnectionType = connectionType;
-                await db.SaveChangesAsync();
-            }
+        using var semaphoreWrapper = await startupSemaphore.GetToken();
+        using var db = await contextFactory.CreateDbContextAsync();
 
+        var machine = await db.Machines.Where(x => x.ChargeBoxId == chargeBoxId).FirstOrDefaultAsync();
+        if (machine != null)
+        {
+            machine.ConnectionType = connectionType;
+            await db.SaveChangesAsync();
         }
     }
 

+ 27 - 1
EVCB_OCPP.WSServer/Service/WsService/OcppWebsocketService.cs

@@ -1,4 +1,5 @@
-using Microsoft.AspNetCore.Builder;
+using EVCB_OCPP.WSServer.Helper;
+using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
@@ -46,6 +47,8 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
     private readonly IMainDbService mainDbService;
     private readonly ILogger<OcppWebsocketService> logger;
 
+    private readonly QueueSemaphore handshakeSemaphore;
+
     public OcppWebsocketService(
         IConfiguration configuration,
         IServiceProvider serviceProvider,
@@ -56,6 +59,8 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
         this.configuration = configuration;
         this.mainDbService = mainDbService;
         this.logger = logger;
+
+        handshakeSemaphore = new QueueSemaphore(5);
     }
 
     internal override async ValueTask<string> ValidateSupportedPortocol(HttpContext context)
@@ -76,6 +81,24 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
 
     internal override async ValueTask<bool> ValidateHandshake(HttpContext context, WsClientData session)
     {
+        try
+        {
+            using var canValidate = await handshakeSemaphore.GetToken();
+            var result = await ValidateHandshakeUnsafe(context, session);
+            return result;
+        }
+        catch (Exception ex)
+        {
+            logger.LogError(ex.Message);
+            logger.LogError(ex.StackTrace);
+        }
+        return false;
+    }
+
+    internal async ValueTask<bool> ValidateHandshakeUnsafe(HttpContext context, WsClientData session)
+    {
+        if (context.RequestAborted.IsCancellationRequested) return false;
+
         string authHeader = context?.Request?.Headers?.Authorization;
 
         session.ISOCPP20 = context.WebSockets.WebSocketRequestedProtocols.Any(x => x.ToLower() == "ocpp2.0");
@@ -120,6 +143,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
         bool authorizated = false;
 
         var info = await mainDbService.GetMachineIdAndCustomerInfo(session.ChargeBoxId, context.RequestAborted);
+        if (context.RequestAborted.IsCancellationRequested) return false;
         //var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.IsDelete == false).Select(x => new { x.CustomerId, x.Id }).AsNoTracking().FirstOrDefault();
         //session.CustomerName = machine == null ? "Unknown" : db.Customer.Where(x => x.Id == machine.CustomerId).Select(x => x.Name).FirstOrDefault();
         //session.CustomerId = machine == null ? Guid.Empty : machine.CustomerId;
@@ -140,6 +164,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
         //var configVaule = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.SecurityProfile)
         //                  .Select(x => x.ConfigureSetting).FirstOrDefault();
         var configVaule = await mainDbService.GetMachineSecurityProfile(session.ChargeBoxId, context.RequestAborted);
+        if (context.RequestAborted.IsCancellationRequested) return false;
         int.TryParse(configVaule, out securityProfile);
 
         if (session.ISOCPP20)
@@ -168,6 +193,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
                 //authorizationKey = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.AuthorizationKey)
                 //                    .Select(x => x.ConfigureSetting).FirstOrDefault();
                 authorizationKey = await mainDbService.GetMachineAuthorizationKey(session.ChargeBoxId, context.RequestAborted);
+                if (context.RequestAborted.IsCancellationRequested) return false;
 
                 if (session.ISOCPP20)
                 {