Skip to content

Commit 47664b7

Browse files
author
Ahmad Noman Musleh
committed
Fixed the issue with using array pool instead of allocating new array on each incoming message
1 parent 8996b0f commit 47664b7

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

src/OpenAPI.Net/OpenAPI.Net.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageTags>cTrader, Open API, Spotware, cTrader</PackageTags>
1010
<Description>A .NET RX library for cTrader Open API</Description>
1111
<PackageId>cTrader.OpenAPI.Net</PackageId>
12-
<Version>1.4.1</Version>
12+
<Version>1.4.2</Version>
1313
<Platforms>AnyCPU</Platforms>
1414
<Company>Spotware</Company>
1515
<Authors>Spotware</Authors>

src/OpenAPI.Net/OpenClient.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ private async Task StartSendingMessages(CancellationToken cancellationToken)
378378
private async void ReadTcp(CancellationToken cancellationToken)
379379
{
380380
var dataLength = new byte[4];
381+
byte[] data = null;
381382

382383
try
383384
{
@@ -399,13 +400,13 @@ private async void ReadTcp(CancellationToken cancellationToken)
399400

400401
if (length <= 0) continue;
401402

402-
var data = new byte[length];
403+
data = ArrayPool<byte>.Shared.Rent(length);
403404

404405
readBytes = 0;
405406

406407
do
407408
{
408-
var count = data.Length - readBytes;
409+
var count = length - readBytes;
409410

410411
readBytes += await _sslStream.ReadAsync(data, readBytes, count, cancellationToken).ConfigureAwait(false);
411412

@@ -415,11 +416,15 @@ private async void ReadTcp(CancellationToken cancellationToken)
415416

416417
var message = ProtoMessage.Parser.ParseFrom(data, 0, length);
417418

419+
ArrayPool<byte>.Shared.Return(data);
420+
418421
OnNext(message);
419422
}
420423
}
421424
catch (Exception ex)
422425
{
426+
if (data is not null) ArrayPool<byte>.Shared.Return(data);
427+
423428
var exception = new ReceiveException(ex);
424429

425430
OnError(exception);

0 commit comments

Comments
 (0)