From 51496fe1490df40ce61da57f4a6b8216aa894d57 Mon Sep 17 00:00:00 2001 From: Peter Kurhajec <61538034+PTKu@users.noreply.github.com> Date: Tue, 12 Sep 2023 08:09:26 +0200 Subject: [PATCH] fixes an issue when async call may deadlock the connector (#221) --- .../AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs b/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs index 266685ba..69640e63 100644 --- a/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs +++ b/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs @@ -111,7 +111,7 @@ private ApiHttpClientRequestHandler RequestHandler private readonly object concurentCountMutex = new object(); - private void AntiThrottling(IEnumerable primitives) + private async Task AntiThrottling(IEnumerable primitives) { var concurrent = 0; @@ -124,9 +124,7 @@ private void AntiThrottling(IEnumerable primitives) if (concurrent >= ConcurrentRequestMaxCount) { - // We need this to be like it is to effectively prevent throttling over WebAPI - // `async awaiting` degrades overall comm performance. - Task.Delay(ConcurrentRequestDelay).Wait(); + await Task.Delay(ConcurrentRequestDelay); } } while (concurrent >= ConcurrentRequestMaxCount); @@ -277,7 +275,7 @@ public override async Task ReadBatchAsync(IEnumerable? primitive $"{((OnlinerBase)p).Symbol} | pollings: [{string.Join(";", ((OnlinerBase)p).PollingHolders.Select(a => a.Key.ToString()))}]"))); } - AntiThrottling(primitives); + await AntiThrottling(primitives); var webApiPrimitives = twinPrimitives.Cast().Distinct().ToArray(); @@ -342,7 +340,7 @@ public override async Task WriteBatchAsync(IEnumerable? primitiv try { - AntiThrottling(primitives); + await AntiThrottling(primitives); var responseData = new ApiBulkResponse(); var twinPrimitives = primitives as ITwinPrimitive[] ?? primitives.ToArray();