2021年5月3日星期一

Kotlin Webflux reactor client retry

I am trying to implement a retry extension function for my webclient where it's not a 200 - I know this is a bad design, but this is just an example. I have this:

Client.class

suspend fun getAccountInfo(accountId: String): Account {      return webclient.get()              .uri("/accounts/${accountId}")              .awaitExchange {                  if (it.statusCode() == HttpStatus.OK) {                      return@awaitExchange it.awaitBody<Account>()                  } else { //anything not 200                      return@awaitExchange it.retryIf<Account>(times = 1)                  }              }  }  

Extension function - I modeled this from the awaitBody() function from the org.springframework.web.reactive.function.client class.

suspend inline fun <reified T : Any> ClientResponse.retryIf(times: Long): T =      bodyToMono(object : ParameterizedTypeReference<T>() {}).retry(times).awaitSingle()  

Similar to the awaitBody() method where it works with the bodyToMono(), I'm trying to reference the retry() within the reactor.core.publisher class and add a awaitSingle() at the end.

After running a few tests, I'm seeing:

NoSuchElementException: No value received via onNext for awaitSingle  

Am I missing something to get the webclient to retry?

A quick note:

  • An alternative that I saw was to use Kotlin's kotlinx.coroutines.flow class retry(), where I'd pass in the method of calling the client but I want retries on TIMEOUTS/server exceptions to be handled with the client class making the API request.
https://stackoverflow.com/questions/67378269/kotlin-webflux-reactor-client-retry May 04, 2021 at 11:04AM

没有评论:

发表评论