Skip to content

Commit

Permalink
LDEV-4744 - extend timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Nov 2, 2023
1 parent 8cebecb commit 0e15bc5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
Expand Down Expand Up @@ -91,6 +90,7 @@
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import lucee.print;
import lucee.commons.io.IOUtil;
import lucee.commons.io.TemporaryStream;
import lucee.commons.io.log.LogUtil;
Expand All @@ -116,6 +116,7 @@
import lucee.runtime.op.Caster;
import lucee.runtime.op.Decision;
import lucee.runtime.tag.Http;
import lucee.runtime.type.dt.TimeSpan;
import lucee.runtime.type.dt.TimeSpanImpl;
import lucee.runtime.type.util.CollectionUtil;

Expand Down Expand Up @@ -299,9 +300,23 @@ public static HttpClientBuilder getHttpClientBuilder(boolean pooling, String cli
HttpClientBuilder builder = HttpClients.custom();
builder.setConnectionManager(cm).setConnectionManagerShared(true).setConnectionTimeToLive(POOL_CONN_TTL_MS, TimeUnit.MILLISECONDS)
.setConnectionReuseStrategy(new DefaultClientConnectionReuseStrategy());

return builder;
}

public static void setTimeout(HttpClientBuilder builder, TimeSpan timeout) {
print.e("setTimeout:" + timeout.getMillis());
if (timeout == null || timeout.getMillis() <= 0) return;

int ms = (int) timeout.getMillis();
if (ms < 0) ms = Integer.MAX_VALUE;

// builder.setConnectionTimeToLive(ms, TimeUnit.MILLISECONDS);
print.e("setTimeout2:" + ms);
SocketConfig sc = SocketConfig.custom().setSoTimeout(ms).build();
builder.setDefaultSocketConfig(sc);
}

private static Registry<ConnectionSocketFactory> createRegistry() throws GeneralSecurityException {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, null, new java.security.SecureRandom());
Expand Down Expand Up @@ -363,7 +378,7 @@ public static void closeIdleConnections() {

private static HTTPResponse invoke(URL url, HttpUriRequest request, String username, String password, long timeout, boolean redirect, String charset, String useragent,
ProxyData proxy, lucee.commons.net.http.Header[] headers, Map<String, String> formfields, boolean pooling) throws IOException, GeneralSecurityException {
CloseableHttpResponse res = null;
print.e("invoke:" + timeout);
CloseableHttpClient client;
proxy = ProxyDataImpl.validate(proxy, url.getHost());

Expand All @@ -387,7 +402,7 @@ private static HTTPResponse invoke(URL url, HttpUriRequest request, String usern
client = builder.build();
if (context == null) context = new BasicHttpContext();

return new HTTPResponse4Impl(url, context, request, res = client.execute(request, context));
return new HTTPResponse4Impl(url, context, request, client.execute(request, context));
}

private static void setFormFields(HttpUriRequest request, Map<String, String> formfields, String charset) throws IOException {
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/java/lucee/runtime/tag/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ else if (type == HttpParamBean.TYPE_BODY) {
if (!hasHeaderIgnoreCase(req, "User-Agent")) req.setHeader("User-Agent", this.useragent);

// set timeout
setTimeout(builder, checkRemainingTimeout());
setTimeout(builder, Http.checkRemainingTimeout(pageContext, this.timeout));

// set Username and Password
if (this.username != null) {
Expand Down Expand Up @@ -1418,17 +1418,17 @@ private String contentAsString(HTTPResponse4Impl rsp, Charset responseCharset, S
}
try {
try {
str = is == null ? "" : IOUtil.toString(is, responseCharset, checkRemainingTimeout().getMillis());
str = is == null ? "" : IOUtil.toString(is, responseCharset, Http.checkRemainingTimeout(pageContext, this.timeout).getMillis());
}
catch (EOFException eof) {
if (is instanceof CachingGZIPInputStream) {
str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset, checkRemainingTimeout().getMillis());
str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset, Http.checkRemainingTimeout(pageContext, this.timeout).getMillis());
}
else throw eof;
}
}
catch (UnsupportedEncodingException uee) {
str = IOUtil.toString(is, (Charset) null, checkRemainingTimeout().getMillis());
str = IOUtil.toString(is, (Charset) null, Http.checkRemainingTimeout(pageContext, this.timeout).getMillis());
}
}
catch (IOException ioe) {
Expand All @@ -1446,10 +1446,10 @@ private String contentAsString(HTTPResponse4Impl rsp, Charset responseCharset, S
return str;
}

private TimeSpan checkRemainingTimeout() throws RequestTimeoutException {
TimeSpan remaining = PageContextUtil.remainingTime(pageContext, true);
if (this.timeout == null || ((int) this.timeout.getSeconds()) <= 0 || timeout.getSeconds() > remaining.getSeconds()) { // not set
this.timeout = remaining;
public static TimeSpan checkRemainingTimeout(PageContext pc, TimeSpan timeout) throws RequestTimeoutException {
TimeSpan remaining = PageContextUtil.remainingTime(pc, true);
if (timeout == null || ((int) timeout.getSeconds()) <= 0 || (timeout.getSeconds() > remaining.getSeconds() && remaining.getSeconds() > 0)) { // not set
return remaining;
}
return timeout;
}
Expand Down
6 changes: 1 addition & 5 deletions core/src/main/java/lucee/runtime/tag/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
import lucee.runtime.type.util.CollectionUtil;
import lucee.runtime.type.util.KeyConstants;
import lucee.runtime.type.util.ListUtil;
import lucee.runtime.util.PageContextUtil;

/**
* Passes SQL statements to a data source. Not limited to queries.
Expand Down Expand Up @@ -485,10 +484,7 @@ public int doStartTag() throws PageException {
}
// timeout
if (data.datasource instanceof DataSourceImpl && ((DataSourceImpl) data.datasource).getAlwaysSetTimeout()) {
TimeSpan remaining = PageContextUtil.remainingTime(pageContext, true);
if (data.timeout == null || ((int) data.timeout.getSeconds()) <= 0 || data.timeout.getSeconds() > remaining.getSeconds()) { // not set
data.timeout = remaining;
}
data.timeout = Http.checkRemainingTimeout(pageContext, data.timeout);
}

// timezone
Expand Down

0 comments on commit 0e15bc5

Please sign in to comment.