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

allow user to configure subscriber timeout for input stream #5017

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSS3-1be6ddd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS S3",
"contributor": "benarnao",
"description": "allow user to configure subscriber timeout for input stream"
}
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ static AsyncRequestBody fromInputStream(Consumer<AsyncRequestBodyFromInputStream
/**
* Creates a {@link BlockingInputStreamAsyncRequestBody} to use for writing an input stream to the downstream service.
*
* <p>By default, it will time out if streaming hasn't started within 10 seconds, and you can configure the timeout
* via {@link BlockingInputStreamAsyncRequestBody#builder()}
* <p><b>Example Usage</b>
*
* <p>
Expand All @@ -408,7 +410,9 @@ static AsyncRequestBody fromInputStream(Consumer<AsyncRequestBodyFromInputStream
* }
*/
static BlockingInputStreamAsyncRequestBody forBlockingInputStream(Long contentLength) {
return new BlockingInputStreamAsyncRequestBody(contentLength);
return BlockingInputStreamAsyncRequestBody.builder()
.contentLength(contentLength)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import software.amazon.awssdk.core.exception.NonRetryableException;
import software.amazon.awssdk.core.internal.io.SdkLengthAwareInputStream;
import software.amazon.awssdk.core.internal.util.NoopSubscription;
import software.amazon.awssdk.utils.Validate;
import software.amazon.awssdk.utils.async.InputStreamConsumingPublisher;

/**
Expand All @@ -43,13 +44,18 @@ public final class BlockingInputStreamAsyncRequestBody implements AsyncRequestBo
private final Long contentLength;
private final Duration subscribeTimeout;

BlockingInputStreamAsyncRequestBody(Long contentLength) {
this(contentLength, Duration.ofSeconds(10));
BlockingInputStreamAsyncRequestBody(Builder builder) {
this.contentLength = builder.contentLength;
this.subscribeTimeout = Validate.isPositiveOrNull(builder.subscribeTimeout, "subscribeTimeout") != null ?
builder.subscribeTimeout :
Duration.ofSeconds(10);
}

BlockingInputStreamAsyncRequestBody(Long contentLength, Duration subscribeTimeout) {
this.contentLength = contentLength;
this.subscribeTimeout = subscribeTimeout;
/**
* Creates a default builder for {@link BlockingInputStreamAsyncRequestBody}.
*/
public static Builder builder() {
return new Builder();
}

@Override
Expand Down Expand Up @@ -112,4 +118,42 @@ private void waitForSubscriptionIfNeeded() throws InterruptedException {
+ "BEFORE invoking doBlockingWrite if your caller is single-threaded.");
}
}

public static final class Builder {
private Duration subscribeTimeout;
private Long contentLength;

private Builder() {
}

/**
* Defines how long it should wait for this AsyncRequestBody to be subscribed (to start streaming) before timing out.
* By default, it's 10 seconds.
*
* <p>You may want to increase it if the request may not be executed right away.
*
* @param subscribeTimeout the timeout
* @return Returns a reference to this object so that method calls can be chained together.
*/
public Builder subscribeTimeout(Duration subscribeTimeout) {
this.subscribeTimeout = subscribeTimeout;
return this;
}

/**
* The content length of the output stream.
*
* @param contentLength the content length
* @return Returns a reference to this object so that method calls can be chained together.
*/
public Builder contentLength(Long contentLength) {
this.contentLength = contentLength;
return this;
}

public BlockingInputStreamAsyncRequestBody build() {
return new BlockingInputStreamAsyncRequestBody(this);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public void doBlockingWrite_waitsForSubscription() {

@Test
@Timeout(10)
public void doBlockingWrite_failsIfSubscriptionNeverComes() {
public void doBlockingWrite_overrideSubscribeTimeout_failsIfSubscriptionNeverComes() {
BlockingInputStreamAsyncRequestBody requestBody =
new BlockingInputStreamAsyncRequestBody(0L, Duration.ofSeconds(1));
BlockingInputStreamAsyncRequestBody.builder().contentLength(0L).subscribeTimeout(Duration.ofSeconds(1)).build();
assertThatThrownBy(() -> requestBody.writeInputStream(new StringInputStream("")))
.hasMessageContaining("The service request was not made");
}
Expand Down