From 5857abcc660de3716b5f2fc65691306d36672fdc Mon Sep 17 00:00:00 2001 From: Caio Pelka Date: Thu, 2 Jun 2022 12:29:52 -0700 Subject: [PATCH] fix memory leak by not consuming Put response Signed-off-by: Caio Pelka --- .../lc/dns/ionos/svc/IONOSUpdateService.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lc-dns-ionos-svc/src/main/java/lc/dns/ionos/svc/IONOSUpdateService.java b/lc-dns-ionos-svc/src/main/java/lc/dns/ionos/svc/IONOSUpdateService.java index c83a989a3..0d591ae38 100644 --- a/lc-dns-ionos-svc/src/main/java/lc/dns/ionos/svc/IONOSUpdateService.java +++ b/lc-dns-ionos-svc/src/main/java/lc/dns/ionos/svc/IONOSUpdateService.java @@ -16,6 +16,7 @@ import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; import java.net.URI; @@ -30,11 +31,15 @@ public class IONOSUpdateService extends BasicallyDangerous { private static final MechaLogger logger = MechaLoggerFactory.getLogger(IONOSUpdateService.class); private final ESPClient esp = new ESPClient(); private final SimpleHttpClient httpSimple = new SimpleHttpClient(); - private final CloseableHttpClient http = HttpClients.custom().build(); + private final CloseableHttpClient httpClient; private String est; private static final String urlBlock = "https://api.hosting.ionos.com/dns/v1/zones/"; public IONOSUpdateService() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); + connectionManager.setMaxTotal(100); + connectionManager.setDefaultMaxPerRoute(100); + this.httpClient = HttpClients.custom().setConnectionManager(connectionManager).build(); } @@ -92,7 +97,7 @@ public class IONOSUpdateService extends BasicallyDangerous { put.setHeader("Content-Type", "application/json"); put.setEntity(new StringEntity(updateRecord.toString())); - HttpResponse res = http.execute(get); + HttpResponse res = httpClient.execute(get); JSONArray allZoneData = new JSONArray(EntityUtils.toString(res.getEntity())); logger.info("allZoneData: {}", allZoneData); logger.info("domains: {}", domains.toString()); @@ -104,10 +109,11 @@ public class IONOSUpdateService extends BasicallyDangerous { if (wantedDomain.equals(domain.getString("name"))) { + logger.info("Domain: {}", domain.getString("name")); + get.setURI(URI.create(urlBlock + domain.getString("id"))); - res = http.execute(get); + res = httpClient.execute(get); JSONArray recordData = new JSONObject(EntityUtils.toString(res.getEntity())).getJSONArray("records"); - logger.info("recordData: {}", recordData); for (int k = 0; k < recordData.length(); k++){ JSONObject record = recordData.getJSONObject(k); @@ -115,12 +121,15 @@ public class IONOSUpdateService extends BasicallyDangerous { if (record.getString("type").equals("A")){ logger.info("Updating record:{}, with IP: {}", record.getString("id"), currentIP); put.setURI(URI.create(urlBlock + domain.getString("id") + "/records/" + record.getString("id"))); - http.execute(put); + HttpResponse httpResponse = httpClient.execute(put); + + EntityUtils.consumeQuietly(httpResponse.getEntity()); } } } } } + logger.info("Sleeping"); sleep(60 * 10 * 1000); } } -- GitLab