Defer ExchangeFilterFunction to subscription time

Prior to this commit, the `ExchangeFilterFunction` instances configured
on a `WebClient` instance would be executed as soon as the `exchange`
method would be called. This behavior is not consistent with the server
side and can confuse filter developers as they'd need to manually
`Mono.defer()` their implementations if they want to record metrics.

This commit defers all `ExchangeFilterFunction` processing at
subscription time.

Fixes gh-22375
This commit is contained in:
Brian Clozel
2019-02-12 10:52:49 +01:00
parent 4e47006a17
commit d463598c09
3 changed files with 46 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -316,7 +316,8 @@ class DefaultWebClient implements WebClient {
ClientRequest request = (this.inserter != null ?
initRequestBuilder().body(this.inserter).build() :
initRequestBuilder().build());
return exchangeFunction.exchange(request).switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR);
return Mono.defer(() -> exchangeFunction.exchange(request))
.switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR);
}
private ClientRequest.Builder initRequestBuilder() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,7 +23,9 @@ import reactor.core.publisher.Mono;
import org.springframework.util.Assert;
/**
* Represents a function that filters an{@linkplain ExchangeFunction exchange function}.
* Represents a function that filters an {@linkplain ExchangeFunction exchange function}.
* <p>The filter is executed when a {@code Subscriber} subscribes to the
* {@code Publisher} returned by the {@code WebClient}.
*
* @author Arjen Poutsma
* @since 5.0