From 04ef683027b0dcbcd643ef1f3442ff80e1c4a8ef Mon Sep 17 00:00:00 2001 From: Patrick Dowler Date: Fri, 15 Dec 2023 10:57:02 -0800 Subject: [PATCH] cadc-registry: add default connection and read timeouts and set methods --- cadc-registry/build.gradle | 2 +- .../ca/nrc/cadc/reg/client/CachingFile.java | 30 +++++++++++++++++-- .../nrc/cadc/reg/client/RegistryClient.java | 25 ++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/cadc-registry/build.gradle b/cadc-registry/build.gradle index f5e0f9f..fa0c6c7 100644 --- a/cadc-registry/build.gradle +++ b/cadc-registry/build.gradle @@ -16,7 +16,7 @@ sourceCompatibility = 1.8 group = 'org.opencadc' -version = '1.7.4' +version = '1.7.5' description = 'OpenCADC Registry client library' def git_url = 'https://github.com/opencadc/reg' diff --git a/cadc-registry/src/main/java/ca/nrc/cadc/reg/client/CachingFile.java b/cadc-registry/src/main/java/ca/nrc/cadc/reg/client/CachingFile.java index 596ec1c..98f8db4 100644 --- a/cadc-registry/src/main/java/ca/nrc/cadc/reg/client/CachingFile.java +++ b/cadc-registry/src/main/java/ca/nrc/cadc/reg/client/CachingFile.java @@ -3,7 +3,7 @@ ******************* CANADIAN ASTRONOMY DATA CENTRE ******************* ************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES ************** * -* (c) 2019. (c) 2019. +* (c) 2023. (c) 2023. * Government of Canada Gouvernement du Canada * National Research Council Conseil national de recherches * Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6 @@ -69,7 +69,8 @@ package ca.nrc.cadc.reg.client; -import ca.nrc.cadc.net.HttpDownload; +import ca.nrc.cadc.net.HttpGet; +import ca.nrc.cadc.net.HttpTransfer; import ca.nrc.cadc.profiler.Profiler; import java.io.ByteArrayOutputStream; import java.io.File; @@ -109,6 +110,8 @@ public class CachingFile { // 10 minutes private static final int DEFAULT_EXPRIY_SECONDS = 10 * 60; + private int connectionTimeout = 3000; // millis + private int readTimeout = 60000; // millis private File localCache; private URL remoteSource; @@ -157,6 +160,24 @@ public CachingFile(File localCache, URL remoteSource, long expirySeconds) { this.expirySeconds = expirySeconds; } + /** + * HTTP connection timeout in milliseconds (default: 30000). + * + * @param connectionTimeout in milliseconds + */ + public void setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } + + /** + * HTTP read timeout in milliseconds (default: 60000). + * + * @param readTimeout in milliseconds + */ + public void setReadTimeout(int readTimeout) { + this.readTimeout = readTimeout; + } + private File checkCacheDirectory(File cacheFile) { Profiler profiler = new Profiler(CachingFile.class); log.debug("Cache file: " + cacheFile); @@ -290,7 +311,10 @@ private String getRemoteContent() throws IOException { private void loadRemoteContent(OutputStream dest) throws IOException { Profiler profiler = new Profiler(CachingFile.class); try { - HttpDownload download = new HttpDownload(remoteSource, dest); + HttpGet download = new HttpGet(remoteSource, dest); + download.setConnectionTimeout(connectionTimeout); + download.setReadTimeout(readTimeout); + log.warn("exec: " + download.getClass().getName() + " with timeouts: " + connectionTimeout + "," + readTimeout); download.run(); if (download.getThrowable() != null) { diff --git a/cadc-registry/src/main/java/ca/nrc/cadc/reg/client/RegistryClient.java b/cadc-registry/src/main/java/ca/nrc/cadc/reg/client/RegistryClient.java index dbe73ff..d4e7290 100644 --- a/cadc-registry/src/main/java/ca/nrc/cadc/reg/client/RegistryClient.java +++ b/cadc-registry/src/main/java/ca/nrc/cadc/reg/client/RegistryClient.java @@ -151,6 +151,9 @@ public String getValue() { private String hostname; private URL regBaseURL; private String capsDomain; + + private int connectionTimeout = 30000; // millis + private int readTimeout = 60000; // millis static { try { @@ -204,6 +207,24 @@ public RegistryClient() { } } + /** + * HTTP connection timeout in milliseconds (default: 30000). + * + * @param connectionTimeout in milliseconds + */ + public void setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } + + /** + * HTTP read timeout in milliseconds (default: 60000). + * + * @param readTimeout in milliseconds + */ + public void setReadTimeout(int readTimeout) { + this.readTimeout = readTimeout; + } + /** * Find out if registry lookup URL was modified by a system property. This * typically indicates that the code is running in a development/test environment. @@ -240,6 +261,8 @@ public URL getAccessURL(Query queryName, URI uri) throws IOException, ResourceNo log.debug("Capabilities cache file: " + queryCacheFile); URL queryURL = new URL(regBaseURL + "/" + queryName.getValue()); CachingFile cachedCapSource = new CachingFile(queryCacheFile, queryURL); + cachedCapSource.setConnectionTimeout(connectionTimeout); + cachedCapSource.setReadTimeout(readTimeout); String map = cachedCapSource.getContent(); InputStream mapStream = new ByteArrayInputStream(map.getBytes(StandardCharsets.UTF_8)); MultiValuedProperties mvp = new MultiValuedProperties(); @@ -285,6 +308,8 @@ public Capabilities getCapabilities(URI resourceID) throws IOException, Resource File capabilitiesFile = this.getCapabilitiesCacheFile(resourceID); CachingFile cachedCapabilities = new CachingFile(capabilitiesFile, serviceCapsURL); + cachedCapabilities.setConnectionTimeout(connectionTimeout); + cachedCapabilities.setReadTimeout(readTimeout); String xml = cachedCapabilities.getContent(); CapabilitiesReader capReader = new CapabilitiesReader(); return capReader.read(xml);