Skip to content

WebClient filters should be deferred to the subscription time #22375

Closed

Description

@bsideup

As observed in spring-attic/spring-cloud-sleuth#1199 and workarounded in spring-attic/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));
}

Activity

  1. added
    type: enhancement
    in: web
    on Feb 7, 2019
  2. added this to the 5.2 M1 milestone on Feb 7, 2019
  3. self-assigned this
    on Feb 11, 2019
  4. added
    status: backported
    on Nov 7, 2019
  5. added a commit that references this issue on Sep 17, 2026
    d463598
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webstatus: backportedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions