I want to combine result form two Mono
based on some condition. Both Mono
are results of WebClient
calls:
- The first one is a single call expecting fast response.
- The second one is a combination of several calls with a slow response.
The idea to "cancel" the second Mono
if result from the first one satisfies some condition to save time and avoid unnecessary network calls. If the first's Mono
result is not enough zip
it with the second Mono
.
A Kotlin code sample to explain my idea:
fun getResult(): Mono<Result> {
val trivialResultMono: Mono<Result> = webClient.getResult()
val nonTrivialResultMono: Mono<Result> = webClient
.getResult()
.flatMap { webClient.getResult1(it) }
.flatMap { webClient.getResult2(it) }
.flatMap { webClient.getResult2(it) }
//here I need to check if trivial result satisfies some condition,
//for example trivialResult.size > 5 if it's true I just return
//trivialResultMono from getResult() function,
//it it's false something like this:
return Mono.zip(trivialResultMono, nonTrivialResultMono) { trivialResult, nonTrivialResult ->
trivialResult + nonTrivialResult
}
}
UPDATE:
To be more clear let's say that trivialResult comes in 1 second, nonTrivialResult in 2 seconds. I want to get my final result in 1 second in case of trivialResult.size > 5
and in 2 seconds otherwise.
Using just Mono.zip(trivialResultMono, nonTrivialResultMono)
I will always get my final result in 2 seconds.
Using filter + switchIfEmpty
it will take 1 second if trivialResult.size > 5
and 3 seconds otherwise. Please correct me if I wrong.