From a3bc8db24ecb894035cfc5d75b585caf27f5a07e Mon Sep 17 00:00:00 2001 From: Sondre Nordjore Date: Mon, 13 May 2024 12:13:38 +0200 Subject: [PATCH] added retry on refreshToken --- src/main/java/no/fintlabs/oidc/OidcService.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/no/fintlabs/oidc/OidcService.java b/src/main/java/no/fintlabs/oidc/OidcService.java index bb283a1..7fceadf 100644 --- a/src/main/java/no/fintlabs/oidc/OidcService.java +++ b/src/main/java/no/fintlabs/oidc/OidcService.java @@ -23,10 +23,12 @@ import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Mono; +import reactor.util.retry.Retry; import javax.annotation.PostConstruct; import java.net.URI; import java.security.interfaces.RSAPublicKey; +import java.sql.Time; import java.time.Duration; import java.time.LocalDateTime; import java.util.List; @@ -58,6 +60,9 @@ public class OidcService { @Setter private Jwk jwk; + public static final Long RETRY_ATTEMPTS = 5L; + public static final Duration DELAY = Duration.ofSeconds((long) Math.pow(1L, 5L)); + public OidcService(ApplicationConfiguration applicationConfiguration, WebClient webClient, SessionService sessionService, CookieService cookieService, OidcRequestFactory oidcRequestFactory) { this.applicationConfiguration = applicationConfiguration; this.webClient = webClient; @@ -123,6 +128,11 @@ public void refreshToken(String state, Token token) { ) .retrieve() .bodyToMono(Token.class) + .retryWhen(Retry.fixedDelay(RETRY_ATTEMPTS, DELAY) + .doAfterRetry(retrySignal -> log.debug("Retried {} times.", retrySignal.totalRetries())) + .onRetryExhaustedThrow(((retryBackoffSpec, retrySignal) -> retrySignal.failure())) + ) + .doOnSuccess(tokenResponse -> log.debug("Successfully refreshed token for: {}", tokenResponse.getAccessToken())) .doOnError(WebClientResponseException.class, ex -> { log.error("Error occured for clientId: {}", applicationConfiguration.getClientId()); log.error("WebClientResponseException occurred: {}", ex.getMessage());