Skip to content

feat(json-rpc): add request extensions #2535

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
merged 2 commits into from
May 31, 2025

Conversation

merklefruit
Copy link
Contributor

Motivation

Diving into alloy json-rpc I found that the Tower traits are all implemented for the type RequestPacket.

One big difference that I can see with the regular http::Request is the lack of http extensions which can be nice to write custom layers that use request-specific metadata.

One example use case would be a custom redundancy layer (e.g. as an expansion to FallbackService) that allows per-request redundancy criteria, for instance to require consensus between N transports, or to use the "highest value" returned by N transports.

Solution

I've added an optional extensions field to the RequestMeta inside each request packet.
It should be enough for any Tower layer to query this to define its extra behavior.

Copy link
Member

@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.

reasonable imo, some nits

pub struct RequestMeta {
/// The method name.
pub method: Cow<'static, str>,
/// The request ID.
pub id: Id,
/// Whether the request is a subscription, other than `eth_subscribe`.
is_subscription: bool,
/// Optional HTTP extensions for the request.
Copy link
Member

Choose a reason for hiding this comment

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

I think the docs here are a bit misleading, while this is a type from the http crate, this doesnt have anything to do with http really, can we add a note to this here and the setter+getter that this can be used to store extradata.

Copy link
Contributor Author

@merklefruit merklefruit May 31, 2025

Choose a reason for hiding this comment

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

Done in 2b44c1a

@github-project-automation github-project-automation bot moved this to In Progress in Alloy May 31, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Reviewed in Alloy May 31, 2025
@mattsse mattsse merged commit 7f0bab4 into alloy-rs:main May 31, 2025
27 checks passed
@github-project-automation github-project-automation bot moved this from Reviewed to Done in Alloy May 31, 2025
@merklefruit merklefruit deleted the nico/feat/requestpacket-ext branch June 2, 2025 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants