Skip to content

Commit

Permalink
PI-1478 changes to retry exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister committed Sep 21, 2023
1 parent 81ab95a commit aebb305
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package uk.gov.justice.digital.hmpps.retry

import java.time.Duration
import java.util.concurrent.TimeUnit
import kotlin.reflect.KClass

fun <T> retry(maxRetries: Int, exceptions: List<KClass<out Exception>> = listOf(Exception::class), code: () -> T): T {
fun <T> retry(
maxRetries: Int,
delay: Duration = Duration.ofMillis(100),
exceptions: List<KClass<out Exception>> = listOf(Exception::class),
code: () -> T
): T {
var throwable: Throwable?
(1..maxRetries).forEach { count ->
try {
Expand All @@ -14,8 +21,9 @@ fun <T> retry(maxRetries: Int, exceptions: List<KClass<out Exception>> = listOf(
} else {
e
}

if (throwable != null) {
if (throwable == null) {
TimeUnit.MILLISECONDS.sleep(delay.toMillis() * count * count)
}else{
throw throwable!!
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.gov.justice.digital.hmpps.listener

import feign.FeignException
import feign.RetryableException
import io.awspring.cloud.sqs.annotation.SqsListener
import io.awspring.cloud.sqs.listener.AsyncAdapterBlockingExecutionFailedException
import io.awspring.cloud.sqs.listener.ListenerExecutionFailedException
Expand All @@ -11,6 +10,7 @@ import io.sentry.Sentry
import io.sentry.spring.jakarta.tracing.SentryTransaction
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.context.annotation.Conditional
import org.springframework.dao.CannotAcquireLockException
import org.springframework.jdbc.CannotGetJdbcConnectionException
import org.springframework.orm.ObjectOptimisticLockingFailureException
import org.springframework.stereotype.Component
Expand All @@ -35,7 +35,7 @@ class AwsNotificationListener(
3,
listOf(
FeignException.NotFound::class,
RetryableException::class,
CannotAcquireLockException::class,
ObjectOptimisticLockingFailureException::class,
CannotCreateTransactionException::class,
CannotGetJdbcConnectionException::class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.digital.hmpps.config.feign

import feign.RequestInterceptor
import feign.Retryer
import org.springframework.context.annotation.Bean
import org.springframework.http.HttpHeaders
import org.springframework.security.authentication.AnonymousAuthenticationToken
Expand All @@ -16,6 +17,9 @@ abstract class FeignConfig(

abstract fun registrationId(): String

@Bean
open fun retryer() = Retryer.Default()

@Bean
open fun requestInterceptor() = RequestInterceptor { template ->
template.header(HttpHeaders.AUTHORIZATION, "Bearer ${getAccessToken()}")
Expand Down

0 comments on commit aebb305

Please sign in to comment.