Reactor Timeout Fallback
reactive
Reactor Timeout Fallback
Reactor code with timeout and fallback:
What if you want to ensure the favorite IDs are retrieved in less than 800ms or, if it takes longer, get them from a cache?
userService.getFavorites(userId)
.timeout(Duration.ofMillis(800)) // If the part above emits nothing for more than 800ms, propagate an error.
.onErrorResume(cacheService.cachedFavoritesFor(userId)) // In case of an error, fallback to the cacheService.
.flatMap(favoriteService::getDetails)
.switchIfEmpty(suggestionService.getSuggestions())
.take(5)
.publishOn(UiUtils.uiThreadScheduler())
.subscribe(uiList::show, UiUtils::errorPopup);