Skip to content

service: with_interceptor_arc — register a shared Arc<dyn Interceptor> - #118

Merged
aknott-ant merged 1 commit into
mainfrom
iain/with-interceptor-arc
May 19, 2026
Merged

aknott-ant merged 1 commit into
mainfrom
iain/with-interceptor-arc

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

Why

with_interceptor takes impl Interceptor and Arc::news it internally — the right ergonomics for the common case. But a server that mounts several ConnectRpcServices and wants the same interceptor instance on all of them — say an auth interceptor whose state (a connection pool, a token cache, a rate-limit counter) is process-wide — has no way to register the Arc<dyn Interceptor> it already holds. Each with_interceptor call allocates and stores a separate trait object.

What

with_interceptor_arc(self, interceptor: Arc<dyn Interceptor>) -> Self accepts the caller's Arc directly. with_interceptor now delegates to it, so the only behavioral difference is who calls Arc::new. Same ordering, same Arc<[..]> rebuild-on-registration, same must_use.

Confidence

with_interceptor_arc_shares_one_instance registers one Arc<dyn Interceptor> on two services and asserts the strong count is 3 (caller + 2 services), that cloning a service does not bump the count (the chain is shared via Arc<[..]>, not per-interceptor), and that dropping the services returns the count to 1. cargo test, cargo fmt, cargo clippy --all-targets -- -D warnings, cargo doc all clean (no new warnings).

🤖 Generated with Claude Code

with_interceptor takes ownership and Arc::news a fresh allocation, which
is the right ergonomics for the common case (one interceptor, one
service). It is wasteful — and surprising — when a single interceptor
instance carries process-wide shared state (a connection pool, a token
cache) and should be registered on multiple ConnectRpcServices: each
with_interceptor call allocates a separate trait object even though the
caller already has an Arc in hand.

with_interceptor_arc accepts the caller's Arc<dyn Interceptor> directly.
with_interceptor now delegates to it, so the only difference is who calls
Arc::new.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@iainmcgin
iainmcgin marked this pull request as ready for review May 19, 2026 22:30
@iainmcgin
iainmcgin requested a review from aknott-ant May 19, 2026 22:30
@aknott-ant
aknott-ant merged commit 452074a into main May 19, 2026
12 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants