|
| 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.core.internal.async; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 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.SdkInternalApi; |
| 24 | +import software.amazon.awssdk.annotations.ThreadSafe; |
| 25 | +import software.amazon.awssdk.utils.Logger; |
| 26 | + |
| 27 | +/** |
| 28 | + * Subscription which can emit {@link Subscriber#onNext(T)} signals to a subscriber, based on the demand received with the |
| 29 | + * {@link Subscription#request(long)}. It tracks the outstandingDemand that has not yet been fulfilled and used a Supplier |
| 30 | + * passed to it to create the object it needs to emit. |
| 31 | + * @param <T> the type of object to emit to the subscriber. |
| 32 | + */ |
| 33 | +@SdkInternalApi |
| 34 | +@ThreadSafe |
| 35 | +public final class EmittingSubscription<T> implements Subscription { |
| 36 | + private static final Logger log = Logger.loggerFor(EmittingSubscription.class); |
| 37 | + |
| 38 | + private Subscriber<? super T> downstreamSubscriber; |
| 39 | + private final AtomicBoolean emitting; |
| 40 | + private final AtomicLong outstandingDemand; |
| 41 | + private final Runnable onCancel; |
| 42 | + private final AtomicBoolean isCancelled; |
| 43 | + private final Supplier<T> supplier; |
| 44 | + |
| 45 | + private EmittingSubscription(Builder<T> builder) { |
| 46 | + this.downstreamSubscriber = builder.downstreamSubscriber; |
| 47 | + this.onCancel = builder.onCancel; |
| 48 | + this.supplier = builder.supplier; |
| 49 | + this.isCancelled = new AtomicBoolean(); |
| 50 | + this.outstandingDemand = new AtomicLong(0); |
| 51 | + this.emitting = new AtomicBoolean(); |
| 52 | + } |
| 53 | + |
| 54 | + public static <T> Builder<T> builder() { |
| 55 | + return new Builder<>(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void request(long n) { |
| 60 | + if (n <= 0) { |
| 61 | + downstreamSubscriber.onError(new IllegalArgumentException("Amount requested must be positive")); |
| 62 | + return; |
| 63 | + } |
| 64 | + long newDemand = outstandingDemand.updateAndGet(current -> { |
| 65 | + if (Long.MAX_VALUE - current < n) { |
| 66 | + return Long.MAX_VALUE; |
| 67 | + } |
| 68 | + return current + n; |
| 69 | + }); |
| 70 | + log.trace(() -> String.format("new outstanding demand: %s", newDemand)); |
| 71 | + emit(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void cancel() { |
| 76 | + isCancelled.set(true); |
| 77 | + downstreamSubscriber = null; |
| 78 | + onCancel.run(); |
| 79 | + } |
| 80 | + |
| 81 | + private void emit() { |
| 82 | + do { |
| 83 | + if (!emitting.compareAndSet(false, true)) { |
| 84 | + return; |
| 85 | + } |
| 86 | + try { |
| 87 | + if (doEmit()) { |
| 88 | + return; |
| 89 | + } |
| 90 | + } finally { |
| 91 | + emitting.compareAndSet(true, false); |
| 92 | + } |
| 93 | + } while (outstandingDemand.get() > 0); |
| 94 | + } |
| 95 | + |
| 96 | + private boolean doEmit() { |
| 97 | + long demand = outstandingDemand.get(); |
| 98 | + |
| 99 | + while (demand > 0) { |
| 100 | + if (isCancelled.get()) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + if (outstandingDemand.get() > 0) { |
| 104 | + demand = outstandingDemand.decrementAndGet(); |
| 105 | + T value; |
| 106 | + try { |
| 107 | + value = supplier.get(); |
| 108 | + } catch (Exception e) { |
| 109 | + downstreamSubscriber.onError(e); |
| 110 | + return true; |
| 111 | + } |
| 112 | + downstreamSubscriber.onNext(value); |
| 113 | + } |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + public static class Builder<T> { |
| 119 | + private Subscriber<? super T> downstreamSubscriber; |
| 120 | + private Runnable onCancel; |
| 121 | + private Supplier<T> supplier; |
| 122 | + |
| 123 | + public Builder<T> downstreamSubscriber(Subscriber<? super T> subscriber) { |
| 124 | + this.downstreamSubscriber = subscriber; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + public Builder<T> onCancel(Runnable onCancel) { |
| 129 | + this.onCancel = onCancel; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public Builder<T> supplier(Supplier<T> supplier) { |
| 134 | + this.supplier = supplier; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + public EmittingSubscription<T> build() { |
| 139 | + return new EmittingSubscription<>(this); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | +} |
0 commit comments