-
Notifications
You must be signed in to change notification settings - Fork 214
Add batch request limit and response size limit in a RPC batch request #2357
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
Merged
Kourin1996
merged 2 commits into
master
from
Kourin1996/add-batch-request-and-response-limit
Mar 5, 2025
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,11 @@ import ( | |
"github.com/celo-org/celo-blockchain/log" | ||
) | ||
|
||
const ( | ||
BatchRequestLimit = 1000 | ||
BatchResponseMaxSize = 25 * 1000 * 1000 | ||
) | ||
|
||
// handler handles JSON-RPC messages. There is one handler per connection. Note that | ||
// handler is not safe for concurrent use. Message handling never blocks indefinitely | ||
// because RPCs are processed on background goroutines launched by handler. | ||
|
@@ -100,6 +105,13 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) { | |
}) | ||
return | ||
} | ||
// Returns an error if number of requests exceeds an allowed maximum | ||
if len(msgs) > BatchRequestLimit { | ||
h.startCallProc(func(cp *callProc) { | ||
h.conn.writeJSON(cp.ctx, errorMessage(&invalidRequestError{"batch too large"})) | ||
}) | ||
return | ||
} | ||
|
||
// Handle non-call messages first: | ||
calls := make([]*jsonrpcMessage, 0, len(msgs)) | ||
|
@@ -112,10 +124,20 @@ func (h *handler) handleBatch(msgs []*jsonrpcMessage) { | |
return | ||
} | ||
// Process calls on a goroutine because they may block indefinitely: | ||
responseBytes := 0 | ||
h.startCallProc(func(cp *callProc) { | ||
answers := make([]*jsonrpcMessage, 0, len(msgs)) | ||
for _, msg := range calls { | ||
for idx, msg := range calls { | ||
if answer := h.handleCallMsg(cp, msg); answer != nil { | ||
// Once total size of responses exceeds an allowed maximum, | ||
// generate error messages for all remaining calls and stop further processing | ||
if responseBytes += len(answer.Result); responseBytes > BatchResponseMaxSize { | ||
for i := idx; i < len(calls); i++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we added one
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to check before |
||
errMsg := msg.errorResponse(&responseTooLargeError{}) | ||
answers = append(answers, errMsg) | ||
} | ||
break | ||
} | ||
answers = append(answers, answer) | ||
} | ||
} | ||
|
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
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.