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 the ClientRequest is passed as a first argument to the filter even before the subscription:
|
ExchangeFunction exchange = initExchangeFunction(); |
|
ExchangeFunction filteredExchange = (this.filters != null ? this.filters.stream() |
|
.reduce(ExchangeFilterFunction::andThen) |
|
.map(filter -> filter.apply(exchange)) |
|
.orElse(exchange) : exchange); |
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 use Mono.defer to defer the execution of the filter to the subscription time, also to alight with the server-side:
|
@Override |
|
public Mono<Void> filter(ServerWebExchange exchange) { |
|
return Mono.defer(() -> |
|
this.currentFilter != null && this.next != null ? |
|
this.currentFilter.filter(exchange, this.next) : |
|
this.handler.handle(exchange)); |
|
} |
As observed in spring-cloud/spring-cloud-sleuth#1199 and workarounded in spring-cloud/spring-cloud-sleuth#1206, some
WebClientfilters like Sleuth's tracing propagation apply the modifications on the subscription time, but theClientRequestis 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
ClientRequestargument, one could useMono.deferto 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