Skip to content

Commit 0275584

Browse files
committed
[feature] add connection pooling and timeout when downlding files
1 parent 3699bbe commit 0275584

3 files changed

Lines changed: 46 additions & 24 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@
107107
<version>1.4.1</version>
108108
</dependency>
109109

110+
<dependency>
111+
<groupId>org.apache.httpcomponents</groupId>
112+
<artifactId>httpclient</artifactId>
113+
<version>4.5.14</version>
114+
</dependency>
115+
110116
<!-- https://mvnrepository.com/artifact/com.fasterxml.uuid/java-uuid-generator -->
111117
<dependency>
112118
<groupId>com.fasterxml.uuid</groupId>

src/main/java/com/evolvedbinary/bblValidator/service/FileDownloadService.java

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,52 @@
55
import jakarta.inject.Singleton;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
8+
import org.apache.http.HttpStatus;
9+
import org.apache.http.client.config.RequestConfig;
10+
import org.apache.http.client.methods.CloseableHttpResponse;
11+
import org.apache.http.client.methods.HttpGet;
12+
import org.apache.http.impl.client.CloseableHttpClient;
13+
import org.apache.http.impl.client.HttpClients;
14+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
815

916
import java.io.IOException;
1017
import java.io.InputStream;
11-
import java.net.URI;
12-
import java.net.http.HttpClient;
13-
import java.net.http.HttpRequest;
14-
import java.net.http.HttpResponse;
1518
import java.nio.file.Files;
1619
import java.nio.file.Path;
1720
import java.nio.file.StandardCopyOption;
1821
import java.util.UUID;
22+
import java.util.concurrent.TimeUnit;
1923

2024
@Singleton
2125
public class FileDownloadService {
2226

2327
private static final Logger LOG = LoggerFactory.getLogger(FileDownloadService.class);
2428
private static final String TEMP_DIR_NAME = "bbl-validator";
25-
private final HttpClient httpClient;
29+
30+
private final PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;
31+
private final RequestConfig httpRequestConfig;
32+
private final CloseableHttpClient httpClient;
33+
2634
private final Path sharedTempDir;
2735
private final RandomBasedGenerator generator = Generators.randomBasedGenerator();
2836

2937
public FileDownloadService() {
30-
this.httpClient = HttpClient.newBuilder()
31-
.followRedirects(HttpClient.Redirect.NORMAL)
38+
this.poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
39+
40+
this.httpRequestConfig = RequestConfig.custom()
41+
.setConnectTimeout(10_000)
42+
.setSocketTimeout(10_000)
43+
.setConnectionRequestTimeout(3_000)
44+
.build();
45+
46+
this.httpClient = HttpClients
47+
.custom()
48+
.setConnectionManager(poolingHttpClientConnectionManager)
49+
.setDefaultRequestConfig(httpRequestConfig)
50+
.evictIdleConnections(30, TimeUnit.SECONDS)
51+
.evictExpiredConnections()
3252
.build();
53+
3354
try {
3455
this.sharedTempDir = Files.createTempDirectory(TEMP_DIR_NAME);
3556
} catch (final IOException e) {
@@ -49,28 +70,25 @@ public Path downloadToTemp(final String url) throws IOException {
4970
final String filename = generateUuidFilename();
5071
final Path tempFile = sharedTempDir.resolve(filename);
5172

52-
final HttpRequest request = HttpRequest.newBuilder()
53-
.uri(URI.create(url))
54-
.GET()
55-
.build();
73+
final HttpGet httpGet = new HttpGet(url);
5674

57-
final HttpResponse<InputStream> response = httpClient.send(
58-
request,
59-
HttpResponse.BodyHandlers.ofInputStream()
60-
);
75+
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
76+
77+
int statusCode = response.getStatusLine().getStatusCode();
78+
if (statusCode != HttpStatus.SC_OK) {
79+
throw new IOException("Non Resolvable url: " + url);
80+
}
6181

62-
try (InputStream inputStream = response.body()) {
63-
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
82+
try (InputStream inputStream = response.getEntity().getContent()) {
83+
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
84+
}
6485
}
6586

6687
LOG.trace("Downloaded file from {} to {}", url, tempFile);
6788
return tempFile;
6889

6990
} catch (final IllegalArgumentException e) {
7091
throw new IOException("Invalid URL format: " + url, e);
71-
} catch (final InterruptedException e) {
72-
Thread.currentThread().interrupt();
73-
throw new IOException("Download interrupted for URL: " + url, e);
7492
}
7593
}
7694

src/test/java/com/evolvedbinary/bblValidator/controller/ValidateControllerTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ void provideUrlAndValidateInvalidCsvInQueryString() {
297297

298298
@Test
299299
void provideNonResolvableUrlAndValidateCsvFromForm() {
300-
//TODO(YB) seek help
301300
final String url = "https://static.evolvedbinary.com/404.csv";
302301
final String schemaId = "concat";
303302
final Map<String, String> formBody = Map.of(
@@ -323,7 +322,7 @@ void provideNonResolvableUrlAndValidateCsvFromForm() {
323322
final ErrorResponse errorBody = exception.getResponse().getBody(ErrorResponse.class).orElse(null);
324323
assertNotNull(errorBody);
325324
assertEquals(ErrorResponse.Code.NON_RESOLVABLE_URL, errorBody.getCode());
326-
assertEquals("Unable to resolve url : nothing", errorBody.getDescription());
325+
assertEquals("Unable to resolve url : " + url, errorBody.getDescription());
327326
}
328327

329328
@Test
@@ -415,7 +414,6 @@ void provideInvalidUrlFormatAndValidateCsvInQueryString() {
415414

416415
@Test
417416
void provideNonResolvableUrlAndValidateCsvInQueryString() {
418-
//TODO(YB) seek help
419417
final String url = "https://static.evolvedbinary.com/404.csv";
420418
final String schemaId = "concat";
421419

@@ -439,7 +437,7 @@ void provideNonResolvableUrlAndValidateCsvInQueryString() {
439437
final ErrorResponse errorBody = exception.getResponse().getBody(ErrorResponse.class).orElse(null);
440438
assertNotNull(errorBody);
441439
assertEquals(ErrorResponse.Code.NON_RESOLVABLE_URL, errorBody.getCode());
442-
assertEquals("Unable to resolve url : nothing", errorBody.getDescription());
440+
assertEquals("Unable to resolve url : " + url, errorBody.getDescription());
443441
}
444442

445443
@Test

0 commit comments

Comments
 (0)