-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from DSM-Repo/login-retry
pr: add 5xx error retry strategy
- Loading branch information
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/com/repo/whopper/infrastructure/feign/Custom5xxErrorDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/repo/whopper/infrastructure/feign/Custom5xxRetryer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/repo/whopper/infrastructure/xquare/XquareRetryConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |