Skip to content

Commit

Permalink
Merge pull request #616 from garyttierney/lazy-s3-info
Browse files Browse the repository at this point in the history
Avoid communicating with S3 unless necessary
  • Loading branch information
DiegoPino authored Aug 15, 2024
2 parents 1ea8758 + c543255 commit c41da6c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
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

0 comments on commit c41da6c

Please sign in to comment.