Skip to content

Commit 32afe08

Browse files
committed
add max retries
1 parent a1e485e commit 32afe08

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

src/main/java/com/getindata/connectors/http/internal/config/HttpConnectorConfigConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ public final class HttpConnectorConfigConstants {
9292
public static final String SINK_HTTP_WRITER_THREAD_POOL_SIZE =
9393
GID_CONNECTOR_HTTP + "sink.writer.thread-pool.size";
9494

95+
public static final String LOOKUP_HTTP_MAX_RETRIES =
96+
GID_CONNECTOR_HTTP + "source.lookup.request.max-retries";
97+
98+
public static final String LOOKUP_HTTP_RETRY_TIMEOUT_MS =
99+
GID_CONNECTOR_HTTP + "source.lookup.request.retry-timeout-ms";
100+
95101
// -----------------------------------------------------
96102

97103

src/main/java/com/getindata/connectors/http/internal/table/lookup/JavaNetHttpPollingClient.java

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
@Slf4j
2828
public class JavaNetHttpPollingClient implements PollingClient<RowData> {
2929

30+
public static final String DEFAULT_REQUEST_MAX_RETRIES = "3";
31+
32+
public static final String DEFAULT_REQUEST_RETRY_TIMEOUT_MS = "1000";
33+
3034
private final HttpClient httpClient;
3135

3236
private final HttpStatusCodeChecker statusCodeChecker;
@@ -37,6 +41,10 @@ public class JavaNetHttpPollingClient implements PollingClient<RowData> {
3741

3842
private final HttpPostRequestCallback<HttpLookupSourceRequestEntry> httpPostRequestCallback;
3943

44+
protected final int httpRequestMaxRetries;
45+
46+
protected final int httpRequestRetryTimeoutMs;
47+
4048
public JavaNetHttpPollingClient(
4149
HttpClient httpClient,
4250
DeserializationSchema<RowData> responseBodyDecoder,
@@ -62,6 +70,20 @@ public JavaNetHttpPollingClient(
6270
.build();
6371

6472
this.statusCodeChecker = new ComposeHttpStatusCodeChecker(checkerConfig);
73+
74+
this.httpRequestMaxRetries = Integer.parseInt(
75+
options.getProperties().getProperty(
76+
HttpConnectorConfigConstants.LOOKUP_HTTP_MAX_RETRIES,
77+
DEFAULT_REQUEST_MAX_RETRIES
78+
)
79+
);
80+
81+
this.httpRequestRetryTimeoutMs = Integer.parseInt(
82+
options.getProperties().getProperty(
83+
HttpConnectorConfigConstants.LOOKUP_HTTP_RETRY_TIMEOUT_MS,
84+
DEFAULT_REQUEST_RETRY_TIMEOUT_MS
85+
)
86+
);
6587
}
6688

6789
@Override
@@ -74,15 +96,43 @@ public Optional<RowData> pull(RowData lookupRow) {
7496
}
7597
}
7698

77-
// TODO Add Retry Policy And configure TimeOut from properties
78-
private Optional<RowData> queryAndProcess(RowData lookupData) throws Exception {
79-
99+
private Optional<RowData> queryAndProcess(RowData lookupData) {
80100
HttpLookupSourceRequestEntry request = requestFactory.buildLookupRequest(lookupData);
81-
HttpResponse<String> response = httpClient.send(
82-
request.getHttpRequest(),
83-
BodyHandlers.ofString()
84-
);
85-
return processHttpResponse(response, request);
101+
HttpResponse<String> response = null;
102+
103+
int retryCount = 0;
104+
105+
while (retryCount < this.httpRequestMaxRetries) {
106+
try {
107+
response = httpClient.send(
108+
request.getHttpRequest(),
109+
BodyHandlers.ofString()
110+
);
111+
break;
112+
} catch (IOException e) {
113+
log.error("IOException during HTTP request. Retrying...", e);
114+
retryCount++;
115+
if (retryCount == this.httpRequestMaxRetries) {
116+
log.error("Maximum retries reached. Aborting...");
117+
return Optional.empty();
118+
}
119+
try {
120+
Thread.sleep(this.httpRequestRetryTimeoutMs);
121+
} catch (InterruptedException ie) {
122+
Thread.currentThread().interrupt();
123+
}
124+
} catch (InterruptedException e) {
125+
Thread.currentThread().interrupt();
126+
log.error("HTTP request interrupted. Aborting...", e);
127+
return Optional.empty();
128+
}
129+
}
130+
try {
131+
return processHttpResponse(response, request);
132+
} catch (IOException e) {
133+
log.error("IOException during HTTP response processing.", e);
134+
return Optional.empty();
135+
}
86136
}
87137

88138
private Optional<RowData> processHttpResponse(

src/test/java/com/getindata/connectors/http/internal/table/lookup/JavaNetHttpPollingClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.getindata.connectors.http.internal.table.lookup;
22

3+
//import java.io.IOException;
34
import java.net.URI;
45
import java.net.http.HttpClient;
56
import java.net.http.HttpRequest;
@@ -22,7 +23,6 @@
2223
import org.mockito.junit.jupiter.MockitoExtension;
2324
import static org.assertj.core.api.Assertions.assertThat;
2425

25-
2626
import com.getindata.connectors.http.internal.HeaderPreprocessor;
2727
import com.getindata.connectors.http.internal.config.HttpConnectorConfigConstants;
2828
import com.getindata.connectors.http.internal.table.lookup.querycreators.GenericGetQueryCreator;

0 commit comments

Comments
 (0)