Release connections fix

This commit is contained in:
nvazquez 2018-03-01 16:36:18 -03:00
parent 5fb5890ea3
commit c0c32b4d9b
2 changed files with 17 additions and 32 deletions

View File

@ -22,12 +22,9 @@ package com.cloud.agent.direct.download;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodRetryHandler;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NoHttpResponseException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
@ -43,7 +40,6 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl {
protected HttpClient client;
private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager();
protected HttpMethodRetryHandler myretryhandler;
public static final Logger s_logger = Logger.getLogger(HttpDirectTemplateDownloader.class.getName());
protected GetMethod request;
protected Map<String, String> reqHeaders = new HashMap<>();
@ -51,8 +47,8 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl {
public HttpDirectTemplateDownloader(String url, Long templateId, String destPoolPath, String checksum, Map<String, String> headers) {
super(url, destPoolPath, templateId, checksum);
s_httpClientManager.getParams().setConnectionTimeout(5000);
s_httpClientManager.getParams().setSoTimeout(5000);
client = new HttpClient(s_httpClientManager);
myretryhandler = createRetryTwiceHandler();
request = createRequest(url, headers);
String downloadDir = getDirectDownloadTempPath(templateId);
createTemporaryDirectoryAndFile(downloadDir);
@ -66,7 +62,6 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl {
protected GetMethod createRequest(String downloadUrl, Map<String, String> headers) {
GetMethod request = new GetMethod(downloadUrl);
request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
request.setFollowRedirects(true);
if (MapUtils.isNotEmpty(headers)) {
for (String key : headers.keySet()) {
@ -77,37 +72,18 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl {
return request;
}
protected HttpMethodRetryHandler createRetryTwiceHandler() {
return new HttpMethodRetryHandler() {
@Override
public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
if (executionCount >= 2) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
}
if (!method.isRequestSent()) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
// otherwise do not retry
return false;
}
};
}
@Override
public boolean downloadTemplate() {
try {
client.executeMethod(request);
int status = client.executeMethod(request);
if (status != HttpStatus.SC_OK) {
s_logger.warn("Not able to download template, status code: " + status);
return false;
}
return performDownload();
} catch (IOException e) {
throw new CloudRuntimeException("Error on HTTP request: " + e.getMessage());
}
return performDownload();
}
protected boolean performDownload() {

View File

@ -353,6 +353,8 @@ public class UriUtils {
}
} catch (IOException e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
return null;
}
@ -424,6 +426,8 @@ public class UriUtils {
}
} catch (IOException e) {
s_logger.warn(e.getMessage());
} finally {
getMethod.releaseConnection();
}
return false;
}
@ -444,6 +448,7 @@ public class UriUtils {
status = httpClient.executeMethod(getMethod);
} catch (IOException e) {
s_logger.error("Error retrieving urls form metalink: " + metalinkUrl);
getMethod.releaseConnection();
return null;
}
try {
@ -457,6 +462,8 @@ public class UriUtils {
}
} catch (IOException e) {
s_logger.warn(e.getMessage());
} finally {
getMethod.releaseConnection();
}
return urls;
}
@ -477,6 +484,8 @@ public class UriUtils {
throw new IllegalArgumentException("Cannot reach URL: " + url + " due to: " + hte.getMessage());
} catch (IOException ioe) {
throw new IllegalArgumentException("Cannot reach URL: " + url + " due to: " + ioe.getMessage());
} finally {
httphead.releaseConnection();
}
}
}