Skip to content
Advertisement

What does timeout operator measure exactly for a Mono chain in Reactor and Webflux?

i’m using Spring Webflux and trying to understand the timeout concept for a chain of Monos.

For example, there’s a chain of Mono calls:

myService.firstOperation()
.then(myService.secondOperation())
...
.then(myService.nOperation())
.timeout(3000L)

How is timeout going to be applied:

1) For the operations in general (sum time of operations)

2) For ech operation (each operation should take no longer than timeout)

3) Only for the last operation (nOperation)

?

I’m almost sure that timeout is applied to the last publisher. If so, how can timeout be applied to to the sum of operations?

Advertisement

Answer

The timeout operator measures the elapsed time between the time of subscription and the onNext/onComplete signal observed by the timeout operator.

Consider the following example:

Mono.delay(Duration.ofMillis(1000))
    .then(Mono.delay(Duration.ofMillis(1000)))
    .then(Mono.delay(Duration.ofMillis(1000)))
    .timeout(Duration.ofMillis(2500))
    .block();

If statement 2 (time measured between operations) or 3 (only the duration of last operation counts) were correct, then the above piece of code wouldn’t throw any error.

However, the case is that timeout operation measures the duration of everything upstream which makes statement 1 (sum of all operations measured) CORRECT.

In the example the sum of all operation (1000 + 1000 + 1000 = 3000ms) is bigger than the configured timeout (2500ms), thus the code results in an error.

Advertisement