Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/streaming jsonrpc batch request #5134

Merged
merged 14 commits into from
Jan 12, 2023

Conversation

asdacap
Copy link
Contributor

@asdacap asdacap commented Jan 12, 2023

  • Stream resuls of jsonrpc request one by one directly to output.
  • Significantly reduce memory allocation in case of batched request.

Changes

  • Refactored JsonRpcResult to contain IEnumerable of JsonRpcResult.Entry, allowing lazy and streaming processing of batched reuest.

Types of changes

What types of changes does your code introduce?

  • New feature (a non-breaking change that adds functionality)

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No: Already have quite a lot of unit tests.

Notes on testing

  • Tested manually with a python script.

{
if (result.IsCollection)
await using MemoryStream resultData = new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are doing such a great job already, maybe we can drop this allocation too? To some reused buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do in next PR though

Copy link
Member

@LukaszRozmej LukaszRozmej Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My ideas are to implement either Stream or TextWriter based on IByteBuffer and use PooledByteBufferAllocator

@asdacap asdacap marked this pull request as ready for review January 12, 2023 14:28
await _handler.SendRawAsync(_jsonOpeningBracket);
await foreach (JsonRpcResult.Entry innerResult in result.BatchedResponses!)
{
if (!isFirst) await _handler.SendRawAsync(_jsonComma);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we dig deeper, we find:

  • In IpcSocketsHandler, _socket.SendAsync(data, SocketFlags.None), not sure if we should set SocketFlags.Truncated or SocketFlags.Partial until we send _jsonClosingBracket.
  • In WebSocketHandler we have _webSocket.SendAsync(data, WebSocketMessageType.Text, true, CancellationToken.None), maybe we should set endOfMessage to false until we send _jsonClosingBracket.

@asdacap asdacap merged commit a3b392e into master Jan 12, 2023
@asdacap asdacap deleted the feature/streaming-jsonrpc-batch-request branch January 12, 2023 14:58
deffrian pushed a commit that referenced this pull request Jan 16, 2023
* Refactor to use a single batched collection

* Using another jsonrpc response

* Make unit test compile

* So now trying enumerable

* Fix test compilation

* some refactor

* fix test

* Fix test

* Fix whitespace

* tiny improvements

* Second time I missed this exact same bug

* Fix may break websocket

Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
kamilchodola pushed a commit that referenced this pull request Jan 18, 2023
* Refactor to use a single batched collection

* Using another jsonrpc response

* Make unit test compile

* So now trying enumerable

* Fix test compilation

* some refactor

* fix test

* Fix test

* Fix whitespace

* tiny improvements

* Second time I missed this exact same bug

* Fix may break websocket

Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
Andrew-Pohl pushed a commit to fuseio/nethermind that referenced this pull request Apr 18, 2023
* Refactor to use a single batched collection

* Using another jsonrpc response

* Make unit test compile

* So now trying enumerable

* Fix test compilation

* some refactor

* fix test

* Fix test

* Fix whitespace

* tiny improvements

* Second time I missed this exact same bug

* Fix may break websocket

Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants