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(rpc): add compression to JSON-RPC #12352

Merged
merged 13 commits into from
Nov 12, 2024

Conversation

TheDhejavu
Copy link
Contributor

@TheDhejavu TheDhejavu commented Nov 6, 2024

Description

Resolves: #9652

This adds a compression layer that wraps tower_http::compression::CompressionLayer and adapts it to work with jsonrpsee's HTTP types. The layer automatically compresses responses based on the client's Accept-Encoding header while preserving the middleware chain's expected body type.

Supports:

  • gzip
  • deflate
  • brotli
  • zstd

Demo

Screenshot 2024-11-11 at 11 35 25 AM

@TheDhejavu TheDhejavu marked this pull request as draft November 6, 2024 16:46
@TheDhejavu TheDhejavu force-pushed the add-compression-to-json-rpc branch from 13c891e to c4c228f Compare November 6, 2024 21:19
@TheDhejavu TheDhejavu marked this pull request as ready for review November 6, 2024 22:03
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

cool, but I think hyper doesn't apply any changes to the body automatically so we'd need to do that

this seems relevant:

paritytech/jsonrpsee#1133

@TheDhejavu
Copy link
Contributor Author

cool, but I think hyper doesn't apply any changes to the body automatically so we'd need to do that

this seems relevant:

paritytech/jsonrpsee#1133

Thanks!!

figured out that reth has tower-http as a dependency and compression middleware is implemented by default which works out-of-the-box with jsonrpee with their recent release here.

in the case of this: paritytech/jsonrpsee#1133 (comment), the implementation is quite similar to what i did here but i will need to modify the body in order to compress it.

this will be the only change i will probably need to make if i use tower compression layer implementation.

use tower_http::compression::CompressionLayer;

impl<RpcMiddleware> RpcServerConfig<RpcMiddleware> {
    //.....other middlewares

    /// Returns a [`CompressionLayer`] that adds compression support (gzip, deflate, brotli, zstd)
    /// based on the client's `Accept-Encoding` header
    const fn maybe_compression_layer() -> Option<CompressionLayer> {
          Some(CompressionLayer::new())
    }
}

wdyt ?

@mattsse
Copy link
Collaborator

mattsse commented Nov 9, 2024

cool, sgtm!

@TheDhejavu TheDhejavu requested a review from gakonst as a code owner November 11, 2024 10:37
@TheDhejavu
Copy link
Contributor Author

cc @mattsse .

@TheDhejavu TheDhejavu requested a review from mattsse November 11, 2024 11:28
@Rjected Rjected added C-enhancement New feature or request C-perf A change motivated by improving speed, memory usage or disk footprint A-rpc Related to the RPC implementation labels Nov 12, 2024
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

great!

only have pedantic style nits

@mattsse mattsse added this pull request to the merge queue Nov 12, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 12, 2024
@mattsse mattsse merged commit 3c56686 into paradigmxyz:main Nov 12, 2024
41 checks passed
@TheDhejavu TheDhejavu deleted the add-compression-to-json-rpc branch November 13, 2024 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rpc Related to the RPC implementation C-enhancement New feature or request C-perf A change motivated by improving speed, memory usage or disk footprint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable GZIP compression for JSON-RPC calls
3 participants