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

Avoid communicating with S3 unless necessary #616

Merged
merged 2 commits into from
Aug 15, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package edu.illinois.library.cantaloupe.source;

import java.io.IOException;

/**
* Deferred accessor to {@link S3ObjectInfo}.
*/
@FunctionalInterface
public interface S3ObjectInfoSupplier {
S3ObjectInfo get() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,11 @@ private S3ObjectInfo getObjectInfoUsingDelegateStrategy()

@Override
public StreamFactory newStreamFactory() throws IOException {
S3ObjectInfo info = getObjectInfo();
info.setLength(getObjectAttributes().length);
return new S3StreamFactory(info);
return new S3StreamFactory(() -> {
S3ObjectInfo info = getObjectInfo();
info.setLength(getObjectAttributes().length);
return info;
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ class S3StreamFactory implements StreamFactory {
private static final int DEFAULT_CHUNK_SIZE = 1024 * 512;
private static final int DEFAULT_CHUNK_CACHE_SIZE = 1024 * 1024 * 10;

private S3ObjectInfo objectInfo;
private S3ObjectInfoSupplier objectInfo;

S3StreamFactory(S3ObjectInfo objectInfo) {
S3StreamFactory(S3ObjectInfoSupplier objectInfo) {
this.objectInfo = objectInfo;
}

@Override
public InputStream newInputStream() throws IOException {
final S3ObjectInfo objectInfo = this.objectInfo.get();
final InputStream responseStream =
S3Source.newObjectInputStream(objectInfo);

Expand Down Expand Up @@ -100,6 +101,7 @@ public ImageInputStream newSeekableStream() throws IOException {
LOGGER.debug("newSeekableStream(): using {}-byte chunks",
chunkSize);

final S3ObjectInfo objectInfo = this.objectInfo.get();
final S3HTTPImageInputStreamClient client =
new S3HTTPImageInputStreamClient(objectInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void setUp() throws Exception {
info.setKey(FIXTURE_KEY);
info.setLength(1584);

instance = new S3StreamFactory(info);
instance = new S3StreamFactory(() -> info);
}

@Test
Expand Down
Loading