WebClient filters should be deferred to the subscription time #22375
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: backported
An issue that has been backported to maintenance branches
type: enhancement
A general enhancement
Milestone
As observed in spring-cloud/spring-cloud-sleuth#1199 and workarounded in spring-cloud/spring-cloud-sleuth#1206, some
WebClient
filters like Sleuth's tracing propagation apply the modifications on the subscription time, but theClientRequest
is passed as a first argument to the filter even before the subscription:spring-framework/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java
Lines 207 to 211 in 0b9522c
Since some of the filters might be expensive to apply, it makes sense to apply them on subscribe.
Metrics is another subscription-sensitive example.
To keep the
ClientRequest
argument, one could useMono.defer
to defer the execution of the filter to the subscription time, also to alight with the server-side:spring-framework/spring-web/src/main/java/org/springframework/web/server/handler/DefaultWebFilterChain.java
Lines 117 to 123 in d257833
The text was updated successfully, but these errors were encountered: