Skip to content

Make shared runtime state immutable for v14 - #3677

Merged
velo merged 2 commits into
14.xfrom
feature/immutability-80-20
Nov 10, 2026
Merged

velo merged 2 commits into
14.xfrom
feature/immutability-80-20

Conversation

@velo

@velo velo commented Nov 10, 2026

Copy link
Copy Markdown
Member

Makes the state that one Feign client shares across threads read-only once it is built. v14 is the release where breaking this API is acceptable. MIGRATION-v14.md gains an "Immutability" section with before/after examples.

Changes

1. Built clients no longer share the builder's interceptor lists
SynchronousMethodHandler.Factory, AsynchronousMethodHandler.Factory and MethodHandlerConfiguration now copy the request and method interceptor lists with List.copyOf. Before, builder.requestInterceptor(...) called after target(...) changed clients that were already built. It could also throw ConcurrentModificationException while requests were in flight.

2. Request.Options is a final value class

  • Removes setMethodOptions/getMethodOptions. They stored per-method options in a map keyed by thread that was never cleared, so it grew without limit under thread pools and virtual threads. The options also only applied on the thread that set them. An Options method parameter still overrides options for a single call.
  • Adds equals/hashCode/toString.
  • Removes Util.getThreadIdentifier().

3. Request is immutable

  • Headers are copied when a request is created, keeping the source map's ordering and case-insensitivity. headers() returns an unmodifiable map.
  • Removes Request.header(...). Adds withHeader(name, values), which returns a copy.
  • Replaces requestTemplate() with methodMetadata() and feignTarget(), the only parts callers read. The template is created per call and can be changed, so it no longer leaks into clients, decoders and metrics. dropwizard-metrics4/5, micrometer and graphql are updated.
  • Removes Response.Builder.requestTemplate(...), which was never read.
  • Micrometer FeignContext swaps in a copy of its carrier for each propagated header. MicrometerObservationCapability now sends getCarrier() in the sync path as well, so tracing headers still reach the wire; FeignHeaderInstrumentationTest checks this.

4. MethodMetadata is read-only once parsed
ReflectiveFeign calls a package-private freeze() after the Contract has parsed the interface. After that, setters throw IllegalStateException and the collection getters return unmodifiable views. A Contract still builds metadata as before.

Not included

  • MethodMetadata.template() still returns the shared, mutable RequestTemplate. Freezing it means guarding every method that changes RequestTemplate, or copying it on every call. That is left for a follow-up.
  • An immutable RequestTemplate pipeline. It would change the Encoder, RequestInterceptor and Contract signatures in about 125 files, with no thread-safety gain, because a template is created per call.

Tests

  • New ImmutabilityTest: builder changes after build, frozen metadata.
  • New tests in RequestTest: headers are copied, and withHeader keeps case-insensitive lookup.
  • OptionsTest: removed the subclass tests and the thread-map tests, added a value-equality test.
  • Full reactor mvn install passes. The only failures are form-spring and GitHubExampleIT, and both fail on 14.x without this change.

velo added 2 commits November 10, 2026 09:01
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
@velo
velo merged commit 08698a1 into 14.x Nov 10, 2026
3 checks passed
@velo
velo deleted the feature/immutability-80-20 branch November 10, 2026 12:20
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.

1 participant