Skip to content

Commit

Permalink
⚡️ improve perf of ws client
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Mar 19, 2024
1 parent cd66f74 commit 4401b34
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
21 changes: 10 additions & 11 deletions SurrealDb.Net/Internals/SurrealDbEngine.Ws.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public SurrealDbWsEngine(
_configureJsonSerializerOptions = configureJsonSerializerOptions;
_prependJsonSerializerContexts = prependJsonSerializerContexts;
_appendJsonSerializerContexts = appendJsonSerializerContexts;
_wsClient = new WebsocketClient(uri) { IsTextMessageConversionEnabled = false };
_wsClient = new WebsocketClient(uri)
{
IsTextMessageConversionEnabled = false,
IsStreamDisposedAutomatically = false
};
_pinger = new(Ping);

_receiverSubscription = _wsClient
Expand Down Expand Up @@ -133,7 +137,9 @@ as JsonTypeInfo<ISurrealDbWsResponse>
break;
case WebSocketMessageType.Binary:
{
using var stream = _memoryStreamManager.GetStream(message.Binary);
using var stream = message.Stream is not null
? message.Stream
: _memoryStreamManager.GetStream(message.Binary!);

#if NET8_0_OR_GREATER
if (JsonSerializer.IsReflectionEnabledByDefault)
Expand Down Expand Up @@ -944,15 +950,8 @@ await JsonSerializer
.ConfigureAwait(false);
#endif

stream.Seek(0, SeekOrigin.Begin);
using var reader = new StreamReader(stream);
#if NET7_0_OR_GREATER
string payload = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
#else
string payload = await reader.ReadToEndAsync().ConfigureAwait(false);
#endif

_wsClient.Send(payload);
var payload = stream.ToArray();
_wsClient.SendAsText(payload);

var response = await taskCompletionSource.Task.ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
Expand Down
4 changes: 2 additions & 2 deletions SurrealDb.Net/SurrealDb.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageReference Include="Microsoft.Spatial" Version="7.18.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Semver" Version="2.3.0" />
<PackageReference Include="Superpower" Version="3.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="SystemTextJsonPatch" Version="3.0.1" />
<PackageReference Include="Websocket.Client" Version="5.0.0" />
<PackageReference Include="Websocket.Client" Version="5.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 4401b34

Please sign in to comment.