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

Add helper methods to RequestKind and ResponseKind #70

Merged

Conversation

rukai
Copy link
Collaborator

@rukai rukai commented Aug 27, 2024

This PR is a first step towards providing a higher level API for encoding and decoding messages.
Given the existence of the RequestKind and ResponseKind enums which has a variant for every possible request/response, it makes sense that the user would be matching on these enums and calling methods on the contained objects.
So this PR generates encode, decode and header_version methods on RequestKind/ResponseKind, to easily call into the contained object.

As an example, it will drastically simplify the implementation of the project I work on, which previously had these matches written out by hand: https://github.com/shotover/shotover-proxy/pull/1736/files

I think that a further high level API can be written on top of this to simplify header/body encoding/decoding.

Also worth noting that its actually impossible to implement such a match on RequestKind/ResponseKind outside of the kafka-protocol crate due to the #[non_exhaustive].
We currently have to use our own enums instead of kafka-protocol to ensure that we exhaustively handle encoding/decoding every message type.

Compile times

I've measured that this PR increases release build time of kafka-protocol from 7s to 14s.
I dont believe this would impact compile time of the final binary, if these methods are not used.
Still, for cold builds this is pretty bad.

So, wow do you feel about putting RequestKind/ResponseKind behind a feature flag so this doesnt impact users who dont touch RequestKind/ResponseKind?
I think that RequestKind/ResponseKind, doesnt really provide any value without these new methods, so I think including them all in a single feature flag sounds reasonable to me.

Another possible compile time win

Another improvement to compile time that could help here is to add a client feature and a broker feature such that.

  • client enables encoding for requests and decoding for responses
  • broker enables encoding for responses and decoding for requests.

Both features can be enabled for proxy use cases.

This would have the impact for client or broker implementations.
But it would go a in a follow up PR and while it shouldnt be difficult, it will take some time to implement.

@tychedelia tychedelia self-requested a review August 27, 2024 20:24
@tychedelia tychedelia added the enhancement New feature or request label Aug 27, 2024
Copy link
Owner

@tychedelia tychedelia left a comment

Choose a reason for hiding this comment

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

Makes sense to me!

Per your comment about features and compile times, I think that makes a ton of sense. The enums also tend to be less useful for clients. I'm going to put this in a 0.12 milestone with the assumption we can start at least with those features and maybe a client/broker feature later if you have time. 👍

Thanks for the work!

module_file,
r#"
fn decode<T: Decodable>(bytes: &mut bytes::Bytes, version: i16) -> Result<T> {{
T::decode(bytes, version).with_context(|| {{
Copy link
Owner

Choose a reason for hiding this comment

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

nit: needs indentation

@tychedelia tychedelia added this to the 0.12.0 milestone Aug 27, 2024
@rukai rukai force-pushed the add_methods_to_request_kind_and_response_kind branch from e6bf84a to 3659a77 Compare August 27, 2024 22:29
@rukai
Copy link
Collaborator Author

rukai commented Aug 27, 2024

Thanks for feedback, should be addressed now.
I used the name messages_enum for the feature, I dont really like including enum in the feature name, but I also cant think of anything better.

@rukai rukai force-pushed the add_methods_to_request_kind_and_response_kind branch from 3659a77 to 4364265 Compare August 27, 2024 22:32
@rukai rukai force-pushed the add_methods_to_request_kind_and_response_kind branch from 4364265 to 1406639 Compare August 27, 2024 22:35
@@ -32,4 +32,5 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- run: cargo clippy --workspace -- -D warnings
Copy link
Owner

Choose a reason for hiding this comment

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

👍

@tychedelia tychedelia merged commit d24e282 into tychedelia:main Aug 28, 2024
3 checks passed
@tychedelia
Copy link
Owner

@rukai any other changes you have on the horizon or do you want me to release 0.12.0 with only this.

@rukai
Copy link
Collaborator Author

rukai commented Aug 28, 2024

I'll have a look into the client and broker features first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants