Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cadc-registry: add default connection and read timeouts and set methods #175

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cadc-registry/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -290,11 +311,13 @@ 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);
download.run();

if (download.getThrowable() != null) {
log.warn("Could not get source from " + remoteSource, download.getThrowable());
log.warn("Could not get source from " + remoteSource + ": " + download.getThrowable());
throw new IOException(download.getThrowable());
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Loading