-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ID: FPCO-30257; HOURS: 4; DONE: 100; Add retry interceptor (#31)
* ID: FPCO-30257; HOURS: 4; DONE: 100; Add retry interceptor * ID: FPCO-30257; Bump up patch version
- Loading branch information
1 parent
5ce1c09
commit e6f6b4d
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
src/main/java/com/fynd/extension/middleware/RetryInterceptor.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,57 @@ | ||
package com.fynd.extension.middleware; | ||
|
||
import java.io.IOException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Response; | ||
|
||
@Slf4j | ||
public class RetryInterceptor implements Interceptor { | ||
|
||
private int maxRetries = Integer.MAX_VALUE; | ||
|
||
public static long calculateRetryTime(int attempt) { | ||
long baseTime = 30 * 1000; // 30 seconds in milliseconds | ||
long increasePerAttempt = 60 * 1000; // 1 minute in milliseconds | ||
|
||
if (attempt <= 3) { | ||
return baseTime; | ||
} else { | ||
return (attempt - 3) * increasePerAttempt; | ||
} | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
int tryCount = 0; | ||
while (tryCount < maxRetries) { | ||
try { | ||
Response response = chain.proceed(chain.request()); | ||
int responseCode = response.code(); | ||
if (responseCode == 502 || responseCode == 503 || responseCode == 504) { | ||
throw new IOException("Request failed - " + tryCount); | ||
} else { | ||
return response; | ||
} | ||
} catch (IOException e) { | ||
log.error("An error occurred: " + e.getMessage()); | ||
if (tryCount == maxRetries - 1) { | ||
throw e; // Stop retrying after reaching max retries | ||
} | ||
long time = calculateRetryTime(tryCount + 1); | ||
log.info("RetryInterceptor " + "Retrying request after " + time + "ms. Attempt: " + (tryCount + 1)); | ||
try { | ||
Thread.sleep(time); | ||
} catch (InterruptedException interruptedEx) { | ||
// Handle the interruption here (optional) | ||
log.error("RetryInterceptor Thread interrupted during retry " + interruptedEx.getMessage()); | ||
throw new IOException("Thread interrupted during retry", interruptedEx); // Re-throw as IOException | ||
} | ||
|
||
} | ||
tryCount++; | ||
} | ||
|
||
return null; // This should never be reached | ||
} | ||
} |
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
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