Skip to content

Commit 5a09786

Browse files
fix: close the closable response. (#419)
1 parent 06b128e commit 5a09786

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

core-httpclient-impl/src/main/java/com/optimizely/ab/config/HttpProjectConfigManager.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.optimizely.ab.notification.NotificationCenter;
2525
import org.apache.http.*;
2626
import org.apache.http.client.ClientProtocolException;
27+
import org.apache.http.client.methods.CloseableHttpResponse;
2728
import org.apache.http.client.methods.HttpGet;
2829
import org.apache.http.util.EntityUtils;
2930
import org.slf4j.Logger;
@@ -116,10 +117,10 @@ static ProjectConfig parseProjectConfig(String datafile) throws ConfigParseExcep
116117
@Override
117118
protected ProjectConfig poll() {
118119
HttpGet httpGet = createHttpRequest();
119-
120+
CloseableHttpResponse response = null;
120121
logger.debug("Fetching datafile from: {}", httpGet.getURI());
121122
try {
122-
HttpResponse response = httpClient.execute(httpGet);
123+
response = httpClient.execute(httpGet);
123124
String datafile = getDatafileFromResponse(response);
124125
if (datafile == null) {
125126
return null;
@@ -128,6 +129,15 @@ protected ProjectConfig poll() {
128129
} catch (ConfigParseException | IOException e) {
129130
logger.error("Error fetching datafile", e);
130131
}
132+
finally {
133+
if (response != null) {
134+
try {
135+
response.close();
136+
} catch (IOException e) {
137+
logger.warn(e.getLocalizedMessage());
138+
}
139+
}
140+
}
131141

132142
return null;
133143
}

core-httpclient-impl/src/test/java/com/optimizely/ab/config/HttpProjectConfigManagerTest.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.mockito.Mock;
3737
import org.mockito.runners.MockitoJUnitRunner;
3838

39+
import java.io.IOException;
3940
import java.net.URI;
4041
import java.util.concurrent.TimeUnit;
4142

@@ -48,6 +49,17 @@
4849
@RunWith(MockitoJUnitRunner.class)
4950
public class HttpProjectConfigManagerTest {
5051

52+
static class MyResponse extends BasicHttpResponse implements CloseableHttpResponse {
53+
54+
public MyResponse(ProtocolVersion protocolVersion, Integer status, String body) {
55+
super(protocolVersion, status, body);
56+
}
57+
58+
@Override
59+
public void close() throws IOException {
60+
61+
}
62+
}
5163
@Mock
5264
private OptimizelyHttpClient mockHttpClient;
5365

@@ -246,7 +258,7 @@ public void testInvalidBlockingTimeout() {
246258
@Ignore
247259
public void testGetDatafileHttpResponse2XX() throws Exception {
248260
String modifiedStamp = "Wed, 24 Apr 2019 07:07:07 GMT";
249-
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 200, "TEST");
261+
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 200, "TEST");
250262
getResponse.setEntity(new StringEntity(datafileString));
251263
getResponse.setHeader(HttpHeaders.LAST_MODIFIED, modifiedStamp);
252264

@@ -260,15 +272,15 @@ public void testGetDatafileHttpResponse2XX() throws Exception {
260272

261273
@Test(expected = ClientProtocolException.class)
262274
public void testGetDatafileHttpResponse3XX() throws Exception {
263-
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 300, "TEST");
275+
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 300, "TEST");
264276
getResponse.setEntity(new StringEntity(datafileString));
265277

266278
projectConfigManager.getDatafileFromResponse(getResponse);
267279
}
268280

269281
@Test
270282
public void testGetDatafileHttpResponse304() throws Exception {
271-
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 304, "TEST");
283+
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 304, "TEST");
272284
getResponse.setEntity(new StringEntity(datafileString));
273285

274286
String datafile = projectConfigManager.getDatafileFromResponse(getResponse);
@@ -277,15 +289,15 @@ public void testGetDatafileHttpResponse304() throws Exception {
277289

278290
@Test(expected = ClientProtocolException.class)
279291
public void testGetDatafileHttpResponse4XX() throws Exception {
280-
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 400, "TEST");
292+
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 400, "TEST");
281293
getResponse.setEntity(new StringEntity(datafileString));
282294

283295
projectConfigManager.getDatafileFromResponse(getResponse);
284296
}
285297

286298
@Test(expected = ClientProtocolException.class)
287299
public void testGetDatafileHttpResponse5XX() throws Exception {
288-
HttpResponse getResponse = new BasicHttpResponse(new ProtocolVersion("TEST", 0, 0), 500, "TEST");
300+
CloseableHttpResponse getResponse = new MyResponse(new ProtocolVersion("TEST", 0, 0), 500, "TEST");
289301
getResponse.setEntity(new StringEntity(datafileString));
290302

291303
projectConfigManager.getDatafileFromResponse(getResponse);

0 commit comments

Comments
 (0)