-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JSON-RPC response streaming (#6078)
* Add `WebSocketStream` - Wrap a `WebSocket` supporting `Stream` operations * Add single test * Include possible fix notes * Introduce 'CounterStream' - Decorator that counts the bytes written * Do not take ownership of the underlying WebSocket - Do not dispose `_socket` - When disposing, just send an "end of message" * Add 'SendUsingStream' to 'ISocketHandler' - Instead of using 'SendRawAsync' operate on the socket using a 'Stream' * Add more tests - Restructure tests (IPC, WebSockets) * Remove unused safe null operators * Replace implementation with 'Stream's * Use Test annotations * Test 'WebSockets' impl * Add 'NullJsonRpcLocalStats' - Do nothing on 'ReportCall' - Return default value on 'GetMethodStats' - Useful during testing * Initial WebSockets collection tests * Refactor message counting for WebSockets * Refactor IPC tests * Test sending collections through IPC * Increase test case count * Use 'NullJsonRpcLocalStats' when required * Send collections using streams * Invert if branches * Add tests for 'maxBatchResponseBodySize' option * Reorder fields, rename server * Fix random object usages * Throw 'InvalidOperationException' when socket is null
- Loading branch information
1 parent
8571d76
commit fad0455
Showing
8 changed files
with
680 additions
and
46 deletions.
There are no files selected for viewing
443 changes: 443 additions & 0 deletions
443
src/Nethermind/Nethermind.JsonRpc.Test/JsonRpcSocketsClientTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/Nethermind/Nethermind.JsonRpc/NullJsonRpcLocalStats.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Nethermind.JsonRpc; | ||
|
||
public class NullJsonRpcLocalStats : IJsonRpcLocalStats | ||
{ | ||
|
||
public Task ReportCall(RpcReport report, long elapsedMicroseconds = 0, long? size = null) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
public MethodStats GetMethodStats(string methodName) | ||
{ | ||
return new MethodStats(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.IO; | ||
|
||
namespace Nethermind.Sockets; | ||
|
||
public class CounterStream : Stream | ||
{ | ||
private readonly Stream _stream; | ||
|
||
public CounterStream(Stream stream) | ||
{ | ||
_stream = stream ?? throw new ArgumentNullException(nameof(stream)); | ||
} | ||
|
||
public long WrittenBytes { get; private set; } | ||
|
||
public override void Flush() => _stream.Flush(); | ||
|
||
public override int Read(byte[] buffer, int offset, int count) => _stream.Read(buffer, offset, count); | ||
|
||
public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin); | ||
|
||
public override void SetLength(long value) => _stream.SetLength(value); | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
_stream.Write(buffer, offset, count); | ||
WrittenBytes += count; | ||
} | ||
|
||
public override bool CanRead | ||
{ | ||
get => _stream.CanRead; | ||
} | ||
|
||
public override bool CanSeek | ||
{ | ||
get => _stream.CanSeek; | ||
} | ||
|
||
public override bool CanWrite | ||
{ | ||
get => _stream.CanWrite; | ||
} | ||
|
||
public override long Length | ||
{ | ||
get => _stream.Length; | ||
} | ||
|
||
public override bool CanTimeout | ||
{ | ||
get => _stream.CanTimeout; | ||
} | ||
|
||
public override long Position | ||
{ | ||
get => _stream.Position; | ||
set => _stream.Position = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net.WebSockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Nethermind.Sockets; | ||
|
||
public class WebSocketStream : Stream | ||
{ | ||
private WebSocket? _socket; | ||
private readonly WebSocketMessageType _messageType; | ||
|
||
public WebSocketStream(WebSocket socket, WebSocketMessageType messageType) | ||
{ | ||
_socket = socket; | ||
_messageType = messageType; | ||
} | ||
|
||
public override bool CanRead => true; | ||
public override bool CanSeek => false; | ||
public override bool CanWrite => true; | ||
public override long Length => throw new NotSupportedException(); | ||
|
||
public override long Position | ||
{ | ||
get => throw new NotSupportedException(); | ||
set => throw new NotSupportedException(); | ||
} | ||
|
||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
ThrowIfDisposed(); | ||
_ = _socket ?? throw new InvalidOperationException($"The underlying {nameof(WebSocket)} is null"); | ||
|
||
if (_socket.State is WebSocketState.Closed or WebSocketState.CloseReceived or WebSocketState.CloseSent) | ||
{ | ||
return 0; | ||
} | ||
|
||
ArraySegment<byte> segment = new(buffer, offset, count); | ||
WebSocketReceiveResult result = await _socket.ReceiveAsync(segment, cancellationToken); | ||
|
||
if (result.MessageType == WebSocketMessageType.Close) | ||
{ | ||
await _socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Remote close", cancellationToken); | ||
return 0; | ||
} | ||
|
||
return result.Count; | ||
} | ||
|
||
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
ThrowIfDisposed(); | ||
_ = _socket ?? throw new ArgumentNullException(nameof(_socket)); | ||
if (_socket.State != WebSocketState.Open) { throw new IOException($"WebSocket not open ({_socket.State})"); } | ||
|
||
ArraySegment<byte> segment = new(buffer, offset, count); | ||
await _socket.SendAsync(segment, _messageType, false, cancellationToken); | ||
} | ||
|
||
public override void Flush() { } | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
return ReadAsync(buffer, offset, count).GetAwaiter().GetResult(); | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
WriteAsync(buffer, offset, count).GetAwaiter().GetResult(); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
try | ||
{ | ||
if (disposing) | ||
{ | ||
_socket?.SendAsync(ReadOnlyMemory<byte>.Empty, WebSocketMessageType.Text, true, CancellationToken.None); | ||
} | ||
} | ||
finally | ||
{ | ||
_socket = null; | ||
base.Dispose(disposing); | ||
} | ||
} | ||
|
||
private void ThrowIfDisposed() | ||
{ | ||
if (_socket == null) throw new ObjectDisposedException(nameof(_socket)); | ||
} | ||
} |