Skip to content

How to handle timeouts?#

Unis are often used to represent asynchronous operations, like making an HTTP call. So, it’s not rare to need to add a timeout or a deadline on this kind of operation. If we don’t get a response (receive an item in the Mutiny lingo) before that deadline, we consider that the operation failed.

We can then recover from this failure by using a fallback value, retrying, or any other failure handling strategy.

To configure a timeout use Uni.ifNoItem().after(Duration):

1
2
3
Uni<String> uniWithTimeout = uni
        .ifNoItem().after(Duration.ofMillis(100))
        .recoverWithItem("some fallback item");

When the deadline is reached, you can do various actions. First you can simply fail:

Uni<String> uniWithTimeout = uni
        .ifNoItem().after(Duration.ofMillis(100)).fail();

A TimeoutException is propagated in this case. So you can handle it specifically in the downstream:

1
2
3
Uni<String> uniWithTimeout = uni
    .ifNoItem().after(Duration.ofMillis(100)).fail()
    .onFailure(TimeoutException.class).recoverWithItem("we got a timeout");

You can also pass a custom exception:

Uni<String> uniWithTimeout = uni
    .ifNoItem().after(Duration.ofMillis(100)).failWith(() -> new ServiceUnavailableException());

Failing and recovering might be inconvenient. So, you can pass a fallback item or Uni directly:

Uni<String> uniWithTimeout = uni
        .ifNoItem().after(Duration.ofMillis(100)).recoverWithItem(() -> "fallback");
Uni<String> uniWithTimeout = uni
        .ifNoItem().after(Duration.ofMillis(100)).recoverWithUni(() -> someFallbackUni());