Skip to content

Commit

Permalink
fixes an issue when async call may deadlock the connector (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
PTKu authored Sep 12, 2023
1 parent 9b0278d commit 51496fe
Showing 1 changed file with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private ApiHttpClientRequestHandler RequestHandler

private readonly object concurentCountMutex = new object();

private void AntiThrottling(IEnumerable<ITwinPrimitive> primitives)
private async Task AntiThrottling(IEnumerable<ITwinPrimitive> primitives)
{
var concurrent = 0;

Expand All @@ -124,9 +124,7 @@ private void AntiThrottling(IEnumerable<ITwinPrimitive> 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);
Expand Down Expand Up @@ -277,7 +275,7 @@ public override async Task ReadBatchAsync(IEnumerable<ITwinPrimitive>? primitive
$"{((OnlinerBase)p).Symbol} | pollings: [{string.Join(";", ((OnlinerBase)p).PollingHolders.Select(a => a.Key.ToString()))}]")));
}

AntiThrottling(primitives);
await AntiThrottling(primitives);

var webApiPrimitives = twinPrimitives.Cast<IWebApiPrimitive>().Distinct().ToArray();

Expand Down Expand Up @@ -342,7 +340,7 @@ public override async Task WriteBatchAsync(IEnumerable<ITwinPrimitive>? primitiv

try
{
AntiThrottling(primitives);
await AntiThrottling(primitives);

var responseData = new ApiBulkResponse();
var twinPrimitives = primitives as ITwinPrimitive[] ?? primitives.ToArray();
Expand Down

0 comments on commit 51496fe

Please sign in to comment.