-
Notifications
You must be signed in to change notification settings - Fork 192
fix: process batch RPC request in parallel #7093
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
Open
hanabi1224
wants to merge
5
commits into
main
Choose a base branch
from
hm/rpc-process-batch-in-parallel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−14
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df7758b
fix: process batch RPC request in parallel
hanabi1224 bf427df
FOREST_RPC_BATCH_MAX_CONCURRENCY
hanabi1224 5628ce0
changelog
hanabi1224 c86a330
cover batch in unit test
hanabi1224 d274cea
no FOREST_RPC_BATCH_MAX_CONCURRENCY
hanabi1224 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,110 @@ | ||
| // Copyright 2019-2026 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use futures::{FutureExt, StreamExt, stream::FuturesOrdered}; | ||
| use jsonrpsee::{ | ||
| MethodResponse, | ||
| core::middleware::{Batch, BatchEntry, Notification}, | ||
| server::{BatchResponseBuilder, middleware::rpc::RpcServiceT}, | ||
| }; | ||
| use tower::Layer; | ||
|
|
||
| /// Parallelize batch RPC requests that are processed in sequence by default | ||
| /// See <https://github.com/paritytech/jsonrpsee/blob/v0.26.0/server/src/middleware/rpc.rs#L157> | ||
| /// | ||
| /// Note that such parallelization is allowed as per the [`JSON-RPC` specification](https://www.jsonrpc.org/specification#:~:text=6%20Batch) | ||
| #[derive(Clone, derive_more::Constructor)] | ||
| pub(super) struct ParallelBatchLayer { | ||
| max_response_body_size: usize, | ||
| } | ||
|
|
||
| impl<S> Layer<S> for ParallelBatchLayer { | ||
| type Service = ParallelBatchService<S>; | ||
|
|
||
| fn layer(&self, service: S) -> Self::Service { | ||
| ParallelBatchService { | ||
| service, | ||
| max_response_body_size: self.max_response_body_size, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub(super) struct ParallelBatchService<S> { | ||
| service: S, | ||
| max_response_body_size: usize, | ||
| } | ||
|
|
||
| impl<S> RpcServiceT for ParallelBatchService<S> | ||
| where | ||
| S: RpcServiceT< | ||
| MethodResponse = MethodResponse, | ||
| NotificationResponse = MethodResponse, | ||
| BatchResponse = MethodResponse, | ||
| > + Send | ||
| + Sync | ||
| + 'static, | ||
| { | ||
| type MethodResponse = S::MethodResponse; | ||
| type NotificationResponse = S::NotificationResponse; | ||
| type BatchResponse = S::BatchResponse; | ||
|
|
||
| fn call<'a>( | ||
| &self, | ||
| req: jsonrpsee::types::Request<'a>, | ||
| ) -> impl Future<Output = Self::MethodResponse> + Send + 'a { | ||
| self.service.call(req) | ||
| } | ||
|
|
||
| // Parallelized version of https://github.com/paritytech/jsonrpsee/blob/v0.26.0/server/src/middleware/rpc.rs#L151 | ||
| fn batch<'a>(&self, batch: Batch<'a>) -> impl Future<Output = Self::BatchResponse> + Send + 'a { | ||
| // Process batch in parallel instead of delegating to the inner service, which processes them sequentially. | ||
| let mut batch_rp = BatchResponseBuilder::new_with_limit(self.max_response_body_size); | ||
| let mut got_notification = false; | ||
| // Although it's not neccesary to perserve the order in response, we do it to avoid potential bugs on client side | ||
| // See <https://www.jsonrpc.org/specification#:~:text=6%20Batch> | ||
| let mut tasks = FuturesOrdered::new(); | ||
| for batch_entry in batch.into_iter() { | ||
| match batch_entry { | ||
| Ok(BatchEntry::Call(req)) => { | ||
| tasks.push_back(self.service.call(req).map(Some).boxed()); | ||
| } | ||
| Ok(BatchEntry::Notification(n)) => { | ||
| got_notification = true; | ||
| tasks.push_back(self.service.notification(n).map(|_| None).boxed()); | ||
| } | ||
| Err(err) => { | ||
| let (err, id) = err.into_parts(); | ||
| let rp = MethodResponse::error(id, err); | ||
| tasks.push_back(async move { Some(rp) }.boxed()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async move { | ||
| while let Some(r) = tasks.next().await { | ||
| if let Some(rp) = r | ||
| && let Err(err) = batch_rp.append(rp) | ||
| { | ||
| return err; | ||
| } | ||
| } | ||
|
|
||
| // If the batch is empty and we got a notification, we return an empty response. | ||
| if batch_rp.is_empty() && got_notification { | ||
| MethodResponse::notification() | ||
| } | ||
| // An empty batch is regarded as an invalid request here. | ||
| else { | ||
| MethodResponse::from_batch(batch_rp.finish()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn notification<'a>( | ||
| &self, | ||
| n: Notification<'a>, | ||
| ) -> impl Future<Output = Self::NotificationResponse> + Send + 'a { | ||
| self.service.notification(n) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.