Skip to content

Commit

Permalink
Merge pull request #96 from DSM-Repo/login-retry
Browse files Browse the repository at this point in the history
pr: add 5xx error retry strategy
  • Loading branch information
dkflfkd53 authored Sep 23, 2024
2 parents e030f9c + 61d71de commit dcb164d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.repo.whopper.infrastructure.feign;

import feign.Response;
import feign.RetryableException;
import feign.codec.ErrorDecoder;

public class Custom5xxErrorDecoder implements ErrorDecoder {
private final ErrorDecoder defaultErrorDecoder = new Default();

@Override
public Exception decode(String methodKey, Response response) {
var exception = defaultErrorDecoder.decode(methodKey, response);

if (response.status() >= 500) {
return new RetryableException(
response.status(),
exception.getMessage(),
response.request().httpMethod(),
exception,
(Long) null,
response.request()
);
}

return exception;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.repo.whopper.infrastructure.feign;

import feign.RetryableException;
import feign.Retryer;

public class Custom5xxRetryer implements Retryer {
private final int maxAttempts;
private final long backoff;
private int attempt;

public Custom5xxRetryer() {
this(2, 100L);
}

public Custom5xxRetryer(int maxAttempts, long backoff) {
this.maxAttempts = maxAttempts;
this.backoff = backoff;
this.attempt = 1;
}

@Override
public void continueOrPropagate(RetryableException e) {
if (attempt++ >= maxAttempts) {
throw e;
}

try {
Thread.sleep(backoff);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}

@Override
public Retryer clone() {
return new Custom5xxRetryer(maxAttempts, backoff);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

@FeignClient(name = "xquare-client", url = "${key.login-api-url}")
@FeignClient(name = "xquare-client", url = "${key.login-api-url}", configuration = XquareRetryConfiguration.class)
public interface XquareClient {
@PostMapping("/user-data")
XquareUserResponse xquareUser(@RequestBody LoginRequest request);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.repo.whopper.infrastructure.xquare;

import com.repo.whopper.infrastructure.feign.Custom5xxErrorDecoder;
import com.repo.whopper.infrastructure.feign.Custom5xxRetryer;
import feign.Retryer;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
class XquareRetryConfiguration {

@Bean
public Retryer retryer() {
return new Custom5xxRetryer();
}

@Bean
public ErrorDecoder errorDecoder() {
return new Custom5xxErrorDecoder();
}
}

0 comments on commit dcb164d

Please sign in to comment.