Skip to content

Commit c695e0a

Browse files
committed
Addressed comments.
1 parent fe57c42 commit c695e0a

File tree

4 files changed

+84
-66
lines changed

4 files changed

+84
-66
lines changed

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/response/CrtResponseAdapter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void onResponseHeadersDone(HttpStream stream, int headerType) {
8080

8181
@Override
8282
public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
83-
CompletableFuture<Void> writeFuture = responsePublisher.write(ByteBuffer.wrap(bodyBytesIn));
83+
CompletableFuture<Void> writeFuture = responsePublisher.send(ByteBuffer.wrap(bodyBytesIn));
8484

8585
if (writeFuture.isDone() && !writeFuture.isCompletedExceptionally()) {
8686
// Optimization: If write succeeded immediately, return non-zero to avoid the extra call back into the CRT.
@@ -89,7 +89,7 @@ public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
8989

9090
writeFuture.whenComplete((result, failure) -> {
9191
if (failure != null) {
92-
die(stream, failure);
92+
failResponseHandlerAndFuture(stream, failure);
9393
return;
9494
}
9595

@@ -111,7 +111,7 @@ public void onResponseComplete(HttpStream stream, int errorCode) {
111111
private void onSuccessfulResponseComplete(HttpStream stream) {
112112
responsePublisher.complete().whenComplete((result, failure) -> {
113113
if (failure != null) {
114-
die(stream, failure);
114+
failResponseHandlerAndFuture(stream, failure);
115115
return;
116116
}
117117

@@ -128,10 +128,10 @@ private void onSuccessfulResponseComplete(HttpStream stream) {
128128
private void onFailedResponseComplete(HttpStream stream, HttpException error) {
129129
log.error(() -> "HTTP response encountered an error.", error);
130130
responsePublisher.error(error);
131-
die(stream, error);
131+
failResponseHandlerAndFuture(stream, error);
132132
}
133133

134-
private void die(HttpStream stream, Throwable error) {
134+
private void failResponseHandlerAndFuture(HttpStream stream, Throwable error) {
135135
callResponseHandlerOnError(error);
136136
completionFuture.completeExceptionally(error);
137137
connection.shutdown();

utils/src/main/java/software/amazon/awssdk/utils/async/SimplePublisher.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,31 @@
3333
import org.reactivestreams.Publisher;
3434
import org.reactivestreams.Subscriber;
3535
import org.reactivestreams.Subscription;
36-
import software.amazon.awssdk.annotations.SdkPublicApi;
36+
import software.amazon.awssdk.annotations.SdkProtectedApi;
3737
import software.amazon.awssdk.utils.Logger;
3838
import software.amazon.awssdk.utils.Validate;
3939

4040
/**
41-
* A {@link Publisher} to which callers can {@link #write(Object)} messages, simplifying the process of implementing a publisher.
41+
* A {@link Publisher} to which callers can {@link #send(Object)} messages, simplifying the process of implementing a publisher.
4242
*
4343
* <p><b>Operations</b>
4444
*
4545
* <p>The {@code SimplePublisher} supports three simplified operations:
4646
* <ol>
47-
* <li>{@link #write(Object)} for sending messages</li>
47+
* <li>{@link #send(Object)} for sending messages</li>
4848
* <li>{@link #complete()} for indicating the successful end of messages</li>
4949
* <li>{@link #error(Throwable)} for indicating the unsuccessful end of messages</li>
5050
* </ol>
5151
*
5252
* Each of these operations returns a {@link CompletableFuture} for indicating when the message has been successfully sent.
5353
*
54-
* <p>Callers are expected to invoke a series of {@link #write(Object)}s followed by a single {@link #complete()} or
54+
* <p>Callers are expected to invoke a series of {@link #send(Object)}s followed by a single {@link #complete()} or
5555
* {@link #error(Throwable)}. See the documentation on each operation for more details.
5656
*
5757
* <p>This publisher will store an unbounded number of messages. It is recommended that callers limit the number of in-flight
58-
* {@link #write(Object)} operations in order to bound the amount of memory used by this publisher.
58+
* {@link #send(Object)} operations in order to bound the amount of memory used by this publisher.
5959
*/
60-
@SdkPublicApi
60+
@SdkProtectedApi
6161
public final class SimplePublisher<T> implements Publisher<T> {
6262
private static final Logger log = Logger.loggerFor(SimplePublisher.class);
6363

@@ -86,7 +86,7 @@ public final class SimplePublisher<T> implements Publisher<T> {
8686
private final AtomicBoolean processingQueue = new AtomicBoolean(false);
8787

8888
/**
89-
* An exception that should be raised to any failed {@link #write(Object)}, {@link #complete()} or {@link #error(Throwable)}
89+
* An exception that should be raised to any failed {@link #send(Object)}, {@link #complete()} or {@link #error(Throwable)}
9090
* operations. This is used to stop accepting messages after the downstream subscription is cancelled or after the
9191
* caller sends a {@code complete()} or {@code #error()}.
9292
*
@@ -100,30 +100,30 @@ public final class SimplePublisher<T> implements Publisher<T> {
100100
private Subscriber<? super T> subscriber;
101101

102102
/**
103-
* Write a message to this publisher.
103+
* Send a message using this publisher.
104104
*
105-
* <p>Messages written to this publisher will eventually be sent to a downstream subscriber, in the order they were
105+
* <p>Messages sent using this publisher will eventually be sent to a downstream subscriber, in the order they were
106106
* written. When the message is sent to the subscriber, the returned future will be completed successfully.
107107
*
108108
* <p>This method may be invoked concurrently when the order of messages is not important.
109109
*
110110
* <p>In the time between when this method is invoked and the returned future is not completed, this publisher stores the
111-
* request message in memory. Callers are recommended to limit the number of writes in progress at a time to bound the
111+
* request message in memory. Callers are recommended to limit the number of sends in progress at a time to bound the
112112
* amount of memory used by this publisher.
113113
*
114114
* <p>The returned future will be completed exceptionally if the downstream subscriber cancels the subscription, or
115-
* if the write call was performed after a {@link #complete()} or {@link #error(Throwable)} call.
115+
* if the {@code send} call was performed after a {@link #complete()} or {@link #error(Throwable)} call.
116116
*
117117
* @param value The message to send. Must not be null.
118118
* @return A future that is completed when the message is sent to the subscriber.
119119
*/
120-
public CompletableFuture<Void> write(T value) {
121-
log.trace(() -> "Received write() with " + value);
120+
public CompletableFuture<Void> send(T value) {
121+
log.trace(() -> "Received send() with " + value);
122122

123123
OnNextQueueEntry<T> entry = new OnNextQueueEntry<>(value);
124124
try {
125125
Validate.notNull(value, "Null cannot be written.");
126-
checkRejectException();
126+
validateRejectState();
127127
eventQueue.add(entry);
128128
processEventQueue();
129129
} catch (RuntimeException t) {
@@ -133,13 +133,13 @@ public CompletableFuture<Void> write(T value) {
133133
}
134134

135135
/**
136-
* Indicate that no more {@link #write(Object)} calls will be made, and that stream of messages is completed successfully.
136+
* Indicate that no more {@link #send(Object)} calls will be made, and that stream of messages is completed successfully.
137137
*
138-
* <p>This can be called before any in-flight {@code write} calls are complete. Such messages will be processed before the
138+
* <p>This can be called before any in-flight {@code send} calls are complete. Such messages will be processed before the
139139
* stream is treated as complete. The returned future will be completed successfully when the {@code complete} is sent to
140140
* the downstream subscriber.
141141
*
142-
* <p>After this method is invoked, any future {@link #write(Object)}, {@code complete()} or {@link #error(Throwable)}
142+
* <p>After this method is invoked, any future {@link #send(Object)}, {@code complete()} or {@link #error(Throwable)}
143143
* calls will be completed exceptionally and not be processed.
144144
*
145145
* <p>The returned future will be completed exceptionally if the downstream subscriber cancels the subscription, or
@@ -153,7 +153,7 @@ public CompletableFuture<Void> complete() {
153153
OnCompleteQueueEntry<T> entry = new OnCompleteQueueEntry<>();
154154

155155
try {
156-
checkRejectException();
156+
validateRejectState();
157157
setRejectExceptionOrThrow(() -> new IllegalStateException("complete() has been invoked"));
158158
eventQueue.add(entry);
159159
processEventQueue();
@@ -164,13 +164,13 @@ public CompletableFuture<Void> complete() {
164164
}
165165

166166
/**
167-
* Indicate that no more {@link #write(Object)} calls will be made, and that streaming of messages has failed.
167+
* Indicate that no more {@link #send(Object)} calls will be made, and that streaming of messages has failed.
168168
*
169-
* <p>This can be called before any in-flight {@code write} calls are complete. Such messages will be processed before the
169+
* <p>This can be called before any in-flight {@code send} calls are complete. Such messages will be processed before the
170170
* stream is treated as being in-error. The returned future will be completed successfully when the {@code error} is
171171
* sent to the downstream subscriber.
172172
*
173-
* <p>After this method is invoked, any future {@link #write(Object)}, {@link #complete()} or {@code #error(Throwable)}
173+
* <p>After this method is invoked, any future {@link #send(Object)}, {@link #complete()} or {@code #error(Throwable)}
174174
* calls will be completed exceptionally and not be processed.
175175
*
176176
* <p>The returned future will be completed exceptionally if the downstream subscriber cancels the subscription, or
@@ -185,7 +185,7 @@ public CompletableFuture<Void> error(Throwable error) {
185185
OnErrorQueueEntry<T> entry = new OnErrorQueueEntry<>(error);
186186

187187
try {
188-
checkRejectException();
188+
validateRejectState();
189189
setRejectExceptionOrThrow(() -> new IllegalStateException("error() has been invoked"));
190190
eventQueue.add(entry);
191191
processEventQueue();
@@ -270,17 +270,13 @@ private void doProcessQueue() {
270270
entryTypesToFail.addAll(asList(ON_NEXT, ON_COMPLETE, ON_ERROR));
271271
log.trace(() -> "Calling onComplete()");
272272
subscriber.onComplete();
273-
outstandingDemand.set(0);
274-
log.trace(() -> "Set demand to 0");
275273
break;
276274
case ON_ERROR:
277275
OnErrorQueueEntry<T> onErrorEntry = (OnErrorQueueEntry<T>) entry;
278276

279277
entryTypesToFail.addAll(asList(ON_NEXT, ON_COMPLETE, ON_ERROR));
280278
log.trace(() -> "Calling onError() with " + onErrorEntry.failure, onErrorEntry.failure);
281279
subscriber.onError(onErrorEntry.failure);
282-
outstandingDemand.set(0);
283-
log.trace(() -> "Set demand to 0");
284280
break;
285281
case CANCEL:
286282
subscriber = null; // Allow subscriber to be garbage collected after cancellation.
@@ -338,7 +334,7 @@ private void panicAndDie(Throwable cause) {
338334
RuntimeException failure = new IllegalStateException("Encountered fatal error in publisher", cause);
339335
rejectException.compareAndSet(null, () -> failure);
340336
entryTypesToFail.addAll(asList(QueueEntry.Type.values()));
341-
subscriber.onError(failure);
337+
subscriber.onError(cause instanceof Error ? cause : failure);
342338

343339
while (true) {
344340
QueueEntry<T> entry = eventQueue.poll();
@@ -356,7 +352,7 @@ private void panicAndDie(Throwable cause) {
356352
/**
357353
* Ensure that {@link #rejectException} is null. If it is not, throw the exception.
358354
*/
359-
private void checkRejectException() {
355+
private void validateRejectState() {
360356
if (rejectException.get() != null) {
361357
throw rejectException.get().get();
362358
}
@@ -385,10 +381,16 @@ public void request(long n) {
385381
+ "amount of data: " + n);
386382
rejectException.compareAndSet(null, () -> failure);
387383
eventQueue.add(new OnErrorQueueEntry<>(failure));
388-
entryTypesToFail.add(ON_NEXT);
384+
entryTypesToFail.addAll(asList(ON_NEXT, ON_COMPLETE));
389385
processEventQueue();
390386
} else {
391-
long newDemand = outstandingDemand.addAndGet(n);
387+
long newDemand = outstandingDemand.updateAndGet(current -> {
388+
if (Long.MAX_VALUE - current < n) {
389+
return Long.MAX_VALUE;
390+
}
391+
392+
return current + n;
393+
});
392394
log.trace(() -> "Increased demand to " + newDemand);
393395
processEventQueue();
394396
}
@@ -412,7 +414,7 @@ public void cancel() {
412414
*/
413415
abstract static class QueueEntry<T> {
414416
/**
415-
* The future that was returned to a {@link #write(Object)}, {@link #complete()} or {@link #error(Throwable)} message.
417+
* The future that was returned to a {@link #send(Object)}, {@link #complete()} or {@link #error(Throwable)} message.
416418
*/
417419
protected final CompletableFuture<Void> resultFuture = new CompletableFuture<>();
418420

@@ -430,7 +432,7 @@ protected enum Type {
430432
}
431433

432434
/**
433-
* An entry added when we get a {@link #write(Object)} call.
435+
* An entry added when we get a {@link #send(Object)} call.
434436
*/
435437
private static final class OnNextQueueEntry<T> extends QueueEntry<T> {
436438
private final T value;

utils/src/test/java/software/amazon/awssdk/utils/async/SimplePublisherTckTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
package software.amazon.awssdk.utils.async;
1717

1818
import org.reactivestreams.Publisher;
19-
import org.reactivestreams.Subscriber;
20-
import org.reactivestreams.Subscription;
2119
import org.reactivestreams.tck.PublisherVerification;
22-
import org.reactivestreams.tck.SubscriberWhiteboxVerification;
2320
import org.reactivestreams.tck.TestEnvironment;
2421

2522
public class SimplePublisherTckTest extends PublisherVerification<Integer> {
@@ -31,7 +28,7 @@ public SimplePublisherTckTest() {
3128
public Publisher<Integer> createPublisher(long elements) {
3229
SimplePublisher<Integer> publisher = new SimplePublisher<>();
3330
for (int i = 0; i < elements; i++) {
34-
publisher.write(i);
31+
publisher.send(i);
3532
}
3633
publisher.complete();
3734
return publisher;

0 commit comments

Comments
 (0)