Skip to content

Commit

Permalink
Performance - remove needless check for content-length when doing ran…
Browse files Browse the repository at this point in the history
…ge requests.
  • Loading branch information
jrobinso committed Nov 30, 2023
1 parent 88f54a5 commit 9905f86
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
18 changes: 12 additions & 6 deletions src/main/java/org/broad/igv/util/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class HttpUtils {
private static boolean BYTE_RANGE_DISABLED = false;
private Map<URL, Boolean> headURLCache = new HashMap<URL, Boolean>();

private Map<String, Long> contentLengthCache = new HashMap<>();

private class CachedRedirect {
private URL url = null;
private ZonedDateTime expires = null;
Expand Down Expand Up @@ -463,17 +465,21 @@ public long getLastModified(URL url) throws IOException {

public long getContentLength(URL url) throws IOException {

if (contentLengthCache.containsKey(url.toExternalForm())) {
return contentLengthCache.get(url.toExternalForm());
}

long contentLength = -1;
try {
String contentLengthString = getHeaderField(url, "Content-Length");
if (contentLengthString == null) {
return -1;
} else {
return Long.parseLong(contentLengthString);
if (contentLengthString != null) {
contentLength = Long.parseLong(contentLengthString);
}
} catch (Exception e) {
log.error("Error fetching content length", e);
return -1;
}
contentLengthCache.put(url.toExternalForm(), contentLength);
return contentLength;
}

public void updateProxySettings() {
Expand Down Expand Up @@ -856,7 +862,7 @@ else if (code >= 400) {
throw new FileNotFoundException(message);
} else if (code == 401) {
OAuthProvider provider = OAuthUtils.getInstance().getProviderForURL(url);
if(provider == null && GoogleUtils.isGoogleURL(url.toExternalForm())) {
if (provider == null && GoogleUtils.isGoogleURL(url.toExternalForm())) {
provider = OAuthUtils.getInstance().getGoogleProvider();
}
if (provider != null && retries == 0) {
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/org/broad/igv/util/stream/IGVSeekableHTTPStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,24 @@ public InputStream openInputStreamForRange(long start, long end) throws IOExcept

// To safely do a range query we should limit it to the content length. Try to determine content-length,
// there is no guarantee this will succeed.
if (!HttpUtils.isSignedURL(url.toExternalForm())) {
try {
contentLength = HttpUtils.getInstance().getContentLength(url);
} catch (Exception e) {
log.error("Error fetching content-length", e);
}
}

// Check content length, if known
if (contentLength > 0) {
if (start >= contentLength) {
throw new IOException("Start (" + start + ") is > content length(" + contentLength + ")");
} else if (start == contentLength) {

} else {
end = contentLength - 1;
}
}
// if (!HttpUtils.isSignedURL(url.toExternalForm())) {
// try {
// contentLength = HttpUtils.getInstance().getContentLength(url);
// } catch (Exception e) {
// log.error("Error fetching content-length", e);
// }
// }
//
// // Check content length, if known
// if (contentLength > 0) {
// if (start >= contentLength) {
// throw new IOException("Start (" + start + ") is > content length(" + contentLength + ")");
// } else if (start == contentLength) {
//
// } else {
// end = contentLength - 1;
// }
// }

String byteRange = "bytes=" + start + "-" + end;
Map<String, String> params = new HashMap();
Expand Down

0 comments on commit 9905f86

Please sign in to comment.