|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.utils.async; |
| 17 | + |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.concurrent.atomic.AtomicLong; |
| 20 | +import java.util.function.Supplier; |
| 21 | +import org.reactivestreams.Subscriber; |
| 22 | +import org.reactivestreams.Subscription; |
| 23 | +import software.amazon.awssdk.annotations.SdkProtectedApi; |
| 24 | +import software.amazon.awssdk.utils.Logger; |
| 25 | +import software.amazon.awssdk.utils.Validate; |
| 26 | + |
| 27 | +/** |
| 28 | + * Allows to send trailing data before invoking onComplete on the downstream subscriber. |
| 29 | + * trailingDataIterable will be created when the upstream subscriber has called onComplete. |
| 30 | + */ |
| 31 | +@SdkProtectedApi |
| 32 | +public class AddingTrailingDataSubscriber<T> extends DelegatingSubscriber<T, T> { |
| 33 | + private static final Logger log = Logger.loggerFor(AddingTrailingDataSubscriber.class); |
| 34 | + |
| 35 | + /** |
| 36 | + * The subscription to the upstream subscriber. |
| 37 | + */ |
| 38 | + private Subscription upstreamSubscription; |
| 39 | + |
| 40 | + /** |
| 41 | + * The amount of unfulfilled demand the downstream subscriber has opened against us. |
| 42 | + */ |
| 43 | + private final AtomicLong downstreamDemand = new AtomicLong(0); |
| 44 | + |
| 45 | + /** |
| 46 | + * Whether the upstream subscriber has called onComplete on us. |
| 47 | + */ |
| 48 | + private volatile boolean onCompleteCalledByUpstream = false; |
| 49 | + |
| 50 | + /** |
| 51 | + * Whether the upstream subscriber has called onError on us. |
| 52 | + */ |
| 53 | + private volatile boolean onErrorCalledByUpstream = false; |
| 54 | + |
| 55 | + /** |
| 56 | + * Whether we have called onComplete on the downstream subscriber. |
| 57 | + */ |
| 58 | + private volatile boolean onCompleteCalledOnDownstream = false; |
| 59 | + |
| 60 | + private final Supplier<Iterable<T>> trailingDataIterableSupplier; |
| 61 | + private Iterator<T> trailingDataIterator; |
| 62 | + |
| 63 | + public AddingTrailingDataSubscriber(Subscriber<? super T> subscriber, |
| 64 | + Supplier<Iterable<T>> trailingDataIterableSupplier) { |
| 65 | + super(Validate.paramNotNull(subscriber, "subscriber")); |
| 66 | + this.trailingDataIterableSupplier = Validate.paramNotNull(trailingDataIterableSupplier, "trailingDataIterableSupplier"); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onSubscribe(Subscription subscription) { |
| 71 | + |
| 72 | + if (upstreamSubscription != null) { |
| 73 | + log.warn(() -> "Received duplicate subscription, cancelling the duplicate.", new IllegalStateException()); |
| 74 | + subscription.cancel(); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + upstreamSubscription = subscription; |
| 79 | + |
| 80 | + subscriber.onSubscribe(new Subscription() { |
| 81 | + |
| 82 | + @Override |
| 83 | + public void request(long l) { |
| 84 | + if (onErrorCalledByUpstream || onCompleteCalledOnDownstream) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + addDownstreamDemand(l); |
| 89 | + |
| 90 | + if (onCompleteCalledByUpstream) { |
| 91 | + sendTrailingDataAndCompleteIfNeeded(); |
| 92 | + return; |
| 93 | + } |
| 94 | + upstreamSubscription.request(l); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void cancel() { |
| 99 | + upstreamSubscription.cancel(); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onError(Throwable throwable) { |
| 106 | + onErrorCalledByUpstream = true; |
| 107 | + subscriber.onError(throwable); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void onNext(T t) { |
| 112 | + Validate.paramNotNull(t, "item"); |
| 113 | + downstreamDemand.decrementAndGet(); |
| 114 | + subscriber.onNext(t); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onComplete() { |
| 119 | + onCompleteCalledByUpstream = true; |
| 120 | + sendTrailingDataAndCompleteIfNeeded(); |
| 121 | + } |
| 122 | + |
| 123 | + private void addDownstreamDemand(long l) { |
| 124 | + |
| 125 | + if (l > 0) { |
| 126 | + downstreamDemand.getAndUpdate(current -> { |
| 127 | + long newValue = current + l; |
| 128 | + return newValue >= 0 ? newValue : Long.MAX_VALUE; |
| 129 | + }); |
| 130 | + } else { |
| 131 | + upstreamSubscription.cancel(); |
| 132 | + onError(new IllegalArgumentException("Demand must not be negative")); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private synchronized void sendTrailingDataAndCompleteIfNeeded() { |
| 137 | + if (onCompleteCalledOnDownstream) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + if (trailingDataIterator == null) { |
| 142 | + Iterable<T> supplier = trailingDataIterableSupplier.get(); |
| 143 | + if (supplier == null) { |
| 144 | + completeDownstreamSubscriber(); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + trailingDataIterator = supplier.iterator(); |
| 149 | + } |
| 150 | + |
| 151 | + sendTrailingDataIfNeeded(); |
| 152 | + |
| 153 | + if (!trailingDataIterator.hasNext()) { |
| 154 | + completeDownstreamSubscriber(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void sendTrailingDataIfNeeded() { |
| 159 | + long demand = downstreamDemand.get(); |
| 160 | + |
| 161 | + while (trailingDataIterator.hasNext() && demand > 0) { |
| 162 | + subscriber.onNext(trailingDataIterator.next()); |
| 163 | + demand = downstreamDemand.decrementAndGet(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private void completeDownstreamSubscriber() { |
| 168 | + subscriber.onComplete(); |
| 169 | + onCompleteCalledOnDownstream = true; |
| 170 | + } |
| 171 | +} |
0 commit comments