The sweetest fruit salad recipe for energy saving services
Chapter 3. Retry Policy
(This story begins in Chapter 0. Launching an energy-saving rocket to the stars)
At this point, we need to define the retry policy to safely react when consumed services produce any error. We take advantage of retry policy also for delaying the request when the maximum number of connections is reached, as defined in previous chapter for configuring Outbound pressure control
In this case, to protect the consumed service, the request is delayed based on an exponential backoff retry policy, which ensures a later execution but prevents that the retry policy itself overloads the external service even more.
Retry policy configuration is defined as follows:
private fun retryPolicy(): Retry<Any> = Retry.any<Any>()
.exponentialBackoff(firstBackoff, maxBackoff)
.retryMax(maxRetries)
.doOnRetry { retryContext ->
}
And used when performing the request:
private fun performGet(deviceId: String): Mono<Array<DataSource>> {
return webClient.get()
.uri(buildLookupURI(deviceId))
.header(…)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Array<DataSource>::class.java)
.retryWhen(retryPolicy())
.onErrorContinue(RetryExhaustedException::class.java) { throwable,
_-> run {
connectionsStatus.newRejectedConnection()
}
}
}
The trip continues in Chapter 4. Health check