Skip to content

Conversation

veecore
Copy link

@veecore veecore commented Aug 21, 2025

Motivation

Currently, request modifiers (AddOrigin, UserAgent) are implemented as separate services, each carrying their own impl Service. This leads to duplicated boilerplate, scattered logic, and some unnecessary runtime allocations (e.g. cloning Uri, building headers at call boundaries).

Additionally, the derivation of the endpoint’s origin was handled speculatively in connection.rs rather than directly on Endpoint, making reasoning about the canonical origin less clear.

The goal of this change is to:

  • unify modifier logic for maintainability,
  • reduce redundant allocations and clones,
  • and make origin handling the responsibility of Endpoint.

Solution

  • Unified AddOrigin and UserAgent into request_modifiers.rs.
  • Introduced a generic Modifier<M, T> that centralizes the Service<Request> impl.
  • Future modifiers only need to define an into_fn(), avoiding repeated Service impls.
  • Added Endpoint::get_origin() to move origin derivation closer to the data it represents.
  • Reduced heap allocations by validating and preparing UserAgent once at construction, instead of at every service call.
  • Eliminated extra Uri clones by borrowing where possible.
  • Preserved existing semantics (try_insert still respected for user-provided headers).
  • Added dedicated unit tests for modifiers; all existing tests continue to pass.

✅ No public API changes.
✅ Cleaner internals, less duplication.
✅ Small but meaningful perf improvements (fewer heap allocations/clones).

LucioFranco and others added 11 commits August 8, 2025 09:03
Update the hello-world tutorial readme to reflect changes to package
names and remove reference to deprecated tools.

## Motivation

As someone relatively new to Rust and new to Tonic, I was working
through the hello-world tutorial and found a few issues. I've
successfully built the project with the changes in this PR.

## Solution

- update example Cargo.toml to reflect package name updates
- update example build.rs with correct build tool name
- remove reference to deprecated GUI tool

I've tested this and it runs and builds correctly.

I sort of hate to reference Postman as the recommended GUI but it was
the first one on the list referenced by the now-deprecated Bloom GUI.
Fixes: hyperium#2386

Interop tests are failing since the [protobuf codegen requires the same version as
protoc](https://github.com/protocolbuffers/protobuf/blob/2ae8154f366a6d776bcc3ac931413bc4d99f578e/rust/release_crates/protobuf_codegen/src/lib.rs#L136-L148)
to work. This PR pins the protoc version to fix the tests.
@LucioFranco
Copy link
Member

While I feel like this is a valiant effort, I am not convinced about the improvement to performance here. I think a big change like this would require a lot of data to prove that its worth the complexity. The reason these things are implemented in their own service (at least originally) was to provide a clear cut difference in what they do, trying to abstract over them imo is a bit over engineered without a proven benefit.

@veecore
Copy link
Author

veecore commented Aug 26, 2025

Totally fair — thanks for the feedback. I don’t want to push an abstraction that feels over-engineered at this stage.
I’ll go ahead and extract the clone/allocation reductions as a smaller, focused PR since those are worthwhile regardless. I’ll ping you for review once it’s up.
If later we get more modifiers, the abstraction here might make sense to revisit.

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

Successfully merging this pull request may close these issues.

7 participants