Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebClient filters should be deferred to the subscription time #22375

Closed
bsideup opened this issue Feb 7, 2019 · 0 comments
Closed

WebClient filters should be deferred to the subscription time #22375

bsideup opened this issue Feb 7, 2019 · 0 comments
Assignees
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

Comments

@bsideup
Copy link

bsideup commented Feb 7, 2019

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));
}

@bclozel bclozel added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Feb 7, 2019
@bclozel bclozel added this to the 5.2 M1 milestone Feb 7, 2019
@bclozel bclozel self-assigned this Feb 11, 2019
@jhoeller jhoeller added the status: backported An issue that has been backported to maintenance branches label Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

3 participants