Skip to content

Commit

Permalink
Defer downstream filter execution if no OAuth2AuthorizedClient is found
Browse files Browse the repository at this point in the history
Prior to this change, ServerOAuth2AuthorizedClientExchangeFilterFunction would invoke next.exchange:
- first at assembly time inside the .switchIfEmpty call.
- second at execution time inside .flatMap when a OAuth2AuthorizedClient is found.

While this double-call should not technically cause any functional problems, since the Mono returned by the first call will not be subscribed if a OAuth2AuthorizedClient is found,
it does result in a lot of unnecessary execution and object creation.  There is no technical need to invoke the downstream filters twice.

This change defers the call inside .switchIfEmpty, so that it will only execute at execution time if an OAuth2AuthorizedClient is not found.

After this change, ServerOAuth2AuthorizedClientExchangeFilterFunction will not invoke next.exchange at assembly time, and will only execute next.exchange once per subscription at execution time.

Fixes gh-6719
  • Loading branch information
philsttr authored and jgrandja committed Apr 1, 2019
1 parent ede9693 commit 2df411f
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next)
return authorizedClient(request, next)
.map(authorizedClient -> bearer(request, authorizedClient))
.flatMap(next::exchange)
.switchIfEmpty(next.exchange(request));
.switchIfEmpty(Mono.defer(() -> next.exchange(request)));
}

private Mono<OAuth2AuthorizedClient> authorizedClient(ClientRequest request, ExchangeFunction next) {
Expand Down

0 comments on commit 2df411f

Please sign in to comment.