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

feat: add a configurable maximum batch size for RPC requests #2939

Merged
merged 16 commits into from
May 2, 2024

Conversation

andynog
Copy link
Contributor

@andynog andynog commented Apr 29, 2024

close: #2867

Adds middleware for the JSONRPC server to enforce a configurable maximum batch size for RPC requests.


PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

@andynog andynog requested review from a team as code owners April 29, 2024 21:05
@andynog andynog self-assigned this Apr 29, 2024
@andynog andynog added config rpc P:operator-experience Priority: Improve experience for operators labels Apr 29, 2024
@andynog andynog marked this pull request as draft April 29, 2024 21:06
@andynog
Copy link
Contributor Author

andynog commented Apr 29, 2024

I still need to find a way to implement some unit tests for the batch requests

@andynog
Copy link
Contributor Author

andynog commented Apr 29, 2024

@melekes, do you think that it would be helpful if I implemented some refactoring for this handler

func (h maxBytesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

I thought of combining this into just one middleware. I think doing this as a pre-check middleware might also be more beneficial since if the body is over the max size then it breaks before processing anything. please let me know what you think.

@melekes
Copy link
Contributor

melekes commented Apr 30, 2024

@melekes, do you think that it would be helpful if I implemented some refactoring for this handler

func (h maxBytesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

I thought of combining this into just one middleware. I think doing this as a pre-check middleware might also be more beneficial since if the body is over the max size then it breaks before processing anything. please let me know what you think.

sound good to me 👍

@andynog andynog added the backport-to-v1.x Tell Mergify to backport the PR to v1.x label Apr 30, 2024
@andynog andynog marked this pull request as ready for review April 30, 2024 21:21
@andynog
Copy link
Contributor Author

andynog commented Apr 30, 2024

This should be ready to review @melekes, implemented a test too.

I've tagged it to be back ported to v1, not sure if it should be back ported to others. I think it could without breaking changes. Please let me know what you think.

Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

Thanks @andynog ❤️

rpc/jsonrpc/server/http_server.go Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
@adizere
Copy link
Member

adizere commented May 1, 2024

Makes sense to backport to 0.38 IMO.

@adizere adizere changed the title feat: add a configurable maximum batch size for RPC requests (wip) feat: add a configurable maximum batch size for RPC requests May 1, 2024
@andynog andynog added the backport-to-v0.38.x Tell Mergify to backport the PR to v0.38.x label May 1, 2024
@andynog
Copy link
Contributor Author

andynog commented May 1, 2024

If you want to test this locally you can:

  • make install
  • cometbft init
  • vim ~/.cometbft/config/config.toml
  • change max_request_batch_size = 1
  • cometbft start --proxy_app=kvstore
  • on another terminal, make a curl JSON-RPC batch request sending two requests in the batch:
curl --location --request GET 'http://localhost:26657/' \
--header 'Content-Type: application/json' \
--data '[
    {
        "jsonrpc": "2.0",
        "method": "net_info",
        "params": {},
        "id": 1
    },
    {
        "jsonrpc": "2.0",
        "method": "net_info",
        "params" : {},
        "id": 2
    }
]'
  • you should get an error:
{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request","data":"batch request exceeds maximum (1) allowed number of requests"}}%
  • if you want to test the max body size, edit theconfig.toml file again and set max_body_bytes = 100, then re-start the kvstore app again and send the curl request one more time. This time you should get an error like:
{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request","data":"error reading request body: http: request body too large"}}

@andynog andynog requested a review from melekes May 1, 2024 18:15
Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

👍

@andynog andynog added this pull request to the merge queue May 2, 2024
Merged via the queue into main with commit aa1caba May 2, 2024
38 checks passed
@andynog andynog deleted the andy/2867-rpc-batch-size-config branch May 2, 2024 12:47
mergify bot pushed a commit that referenced this pull request May 2, 2024
close: #2867

Adds middleware for the JSONRPC server to enforce a configurable maximum
batch size for RPC requests.

---

#### PR checklist

- [ ] Tests written/updated
- [X] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [X] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [X] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

(cherry picked from commit aa1caba)
mergify bot pushed a commit that referenced this pull request May 2, 2024
close: #2867

Adds middleware for the JSONRPC server to enforce a configurable maximum
batch size for RPC requests.

---

#### PR checklist

- [ ] Tests written/updated
- [X] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [X] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [X] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

(cherry picked from commit aa1caba)

# Conflicts:
#	docs/references/config/config.toml.md
andynog added a commit that referenced this pull request May 3, 2024
…#2939) (#2980)

close: #2867 

Adds middleware for the JSONRPC server to enforce a configurable maximum
batch size for RPC requests.

---

#### PR checklist

- [ ] Tests written/updated
- [X] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [X] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [X] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
<hr>This is an automatic backport of pull request #2939 done by
[Mergify](https://mergify.com).

---------

Co-authored-by: Andy Nogueira <me@andynogueira.dev>
Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
Copy link
Contributor

@cason cason left a comment

Choose a reason for hiding this comment

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

Thank you for this. :)

I wish we could simplify the code under the node package, moving it to the rpc package (where it belongs), but lets keep this to a next refactoring effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-to-v0.38.x Tell Mergify to backport the PR to v0.38.x backport-to-v1.x Tell Mergify to backport the PR to v1.x config P:operator-experience Priority: Improve experience for operators rpc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jsonrpc: add a batch size limit for RPC requests
4 participants