简体   繁体   中英

Webflux timeout for a chain of Mono

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?

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.

Documentation is pretty clear

timeout - the timeout before the onNext signal from this Mono

So in you example the timeout is only applied to the Mono returned from the last then .

.then( ... )
.timeout(3000L)

the correct answer is:

3) Only for the last operation (nOperation)

Since this is last in the chain.

There is no timeout for the sum of operations, why would you need that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM