|
| 1 | +/* |
| 2 | + * Copyright 2015-2018 the original author or authors. |
| 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 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.rsocket.internal; |
| 18 | + |
| 19 | +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
| 20 | +import javax.annotation.Nullable; |
| 21 | +import org.reactivestreams.Publisher; |
| 22 | +import org.reactivestreams.Subscriber; |
| 23 | +import org.reactivestreams.Subscription; |
| 24 | +import reactor.core.CoreSubscriber; |
| 25 | +import reactor.core.publisher.Flux; |
| 26 | +import reactor.core.publisher.Operators; |
| 27 | + |
| 28 | +/** */ |
| 29 | +public class RateLimitableRequestPublisher<T> extends Flux<T> implements Subscription { |
| 30 | + |
| 31 | + private static final int NOT_CANCELED_STATE = 0; |
| 32 | + private static final int CANCELED_STATE = 1; |
| 33 | + |
| 34 | + private final Publisher<T> source; |
| 35 | + |
| 36 | + private volatile int canceled; |
| 37 | + private static final AtomicIntegerFieldUpdater<RateLimitableRequestPublisher> CANCELED = |
| 38 | + AtomicIntegerFieldUpdater.newUpdater(RateLimitableRequestPublisher.class, "canceled"); |
| 39 | + |
| 40 | + private final long prefetch; |
| 41 | + private final long limit; |
| 42 | + |
| 43 | + private long externalRequested; // need sync |
| 44 | + private int pendingToFulfil; // need sync since should be checked/zerroed in onNext |
| 45 | + // and increased in request |
| 46 | + private int deliveredElements; // no need to sync since increased zerroed only in |
| 47 | + // the request method |
| 48 | + |
| 49 | + private boolean subscribed; |
| 50 | + |
| 51 | + private @Nullable Subscription internalSubscription; |
| 52 | + |
| 53 | + private RateLimitableRequestPublisher(Publisher<T> source, long prefetch) { |
| 54 | + this.source = source; |
| 55 | + this.prefetch = prefetch; |
| 56 | + this.limit = prefetch == Integer.MAX_VALUE ? Integer.MAX_VALUE : (prefetch - (prefetch >> 2)); |
| 57 | + } |
| 58 | + |
| 59 | + public static <T> RateLimitableRequestPublisher<T> wrap(Publisher<T> source, long prefetch) { |
| 60 | + return new RateLimitableRequestPublisher<>(source, prefetch); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void subscribe(CoreSubscriber<? super T> destination) { |
| 65 | + synchronized (this) { |
| 66 | + if (subscribed) { |
| 67 | + throw new IllegalStateException("only one subscriber at a time"); |
| 68 | + } |
| 69 | + |
| 70 | + subscribed = true; |
| 71 | + } |
| 72 | + final InnerOperator s = new InnerOperator(destination); |
| 73 | + |
| 74 | + source.subscribe(s); |
| 75 | + destination.onSubscribe(s); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void request(long n) { |
| 80 | + synchronized (this) { |
| 81 | + long requested = externalRequested; |
| 82 | + if (requested == Long.MAX_VALUE) { |
| 83 | + return; |
| 84 | + } |
| 85 | + externalRequested = Operators.addCap(n, requested); |
| 86 | + } |
| 87 | + |
| 88 | + requestN(); |
| 89 | + } |
| 90 | + |
| 91 | + private void requestN() { |
| 92 | + final long r; |
| 93 | + final Subscription s; |
| 94 | + |
| 95 | + synchronized (this) { |
| 96 | + s = internalSubscription; |
| 97 | + if (s == null) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + final long er = externalRequested; |
| 102 | + final long p = prefetch; |
| 103 | + final int pendingFulfil = pendingToFulfil; |
| 104 | + |
| 105 | + if (er != Long.MAX_VALUE || p != Integer.MAX_VALUE) { |
| 106 | + // shortcut |
| 107 | + if (pendingFulfil == p) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + r = Math.min(p - pendingFulfil, er); |
| 112 | + if (er != Long.MAX_VALUE) { |
| 113 | + externalRequested -= r; |
| 114 | + } |
| 115 | + if (p != Integer.MAX_VALUE) { |
| 116 | + pendingToFulfil += r; |
| 117 | + } |
| 118 | + } else { |
| 119 | + r = Long.MAX_VALUE; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (r > 0) { |
| 124 | + s.request(r); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void cancel() { |
| 129 | + if (!isCanceled() && CANCELED.compareAndSet(this, NOT_CANCELED_STATE, CANCELED_STATE)) { |
| 130 | + Subscription s; |
| 131 | + |
| 132 | + synchronized (this) { |
| 133 | + s = internalSubscription; |
| 134 | + internalSubscription = null; |
| 135 | + subscribed = false; |
| 136 | + } |
| 137 | + |
| 138 | + if (s != null) { |
| 139 | + s.cancel(); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private boolean isCanceled() { |
| 145 | + return canceled == CANCELED_STATE; |
| 146 | + } |
| 147 | + |
| 148 | + private class InnerOperator implements CoreSubscriber<T>, Subscription { |
| 149 | + final Subscriber<? super T> destination; |
| 150 | + |
| 151 | + private InnerOperator(Subscriber<? super T> destination) { |
| 152 | + this.destination = destination; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void onSubscribe(Subscription s) { |
| 157 | + synchronized (RateLimitableRequestPublisher.this) { |
| 158 | + RateLimitableRequestPublisher.this.internalSubscription = s; |
| 159 | + |
| 160 | + if (isCanceled()) { |
| 161 | + s.cancel(); |
| 162 | + subscribed = false; |
| 163 | + RateLimitableRequestPublisher.this.internalSubscription = null; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + requestN(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onNext(T t) { |
| 172 | + try { |
| 173 | + destination.onNext(t); |
| 174 | + |
| 175 | + if (prefetch == Integer.MAX_VALUE) { |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + final long l = limit; |
| 180 | + int d = deliveredElements + 1; |
| 181 | + |
| 182 | + if (d == l) { |
| 183 | + d = 0; |
| 184 | + final long r; |
| 185 | + final Subscription s; |
| 186 | + |
| 187 | + synchronized (RateLimitableRequestPublisher.this) { |
| 188 | + long er = externalRequested; |
| 189 | + s = internalSubscription; |
| 190 | + |
| 191 | + if (s == null) { |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + if (er >= l) { |
| 196 | + er -= l; |
| 197 | + // keep pendingToFulfil as is since it is eq to prefetch |
| 198 | + r = l; |
| 199 | + } else { |
| 200 | + pendingToFulfil -= l; |
| 201 | + if (er > 0) { |
| 202 | + r = er; |
| 203 | + er = 0; |
| 204 | + pendingToFulfil += r; |
| 205 | + } else { |
| 206 | + r = 0; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + externalRequested = er; |
| 211 | + } |
| 212 | + |
| 213 | + if (r > 0) { |
| 214 | + s.request(r); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + deliveredElements = d; |
| 219 | + } catch (Throwable e) { |
| 220 | + onError(e); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public void onError(Throwable t) { |
| 226 | + destination.onError(t); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public void onComplete() { |
| 231 | + destination.onComplete(); |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public void request(long n) {} |
| 236 | + |
| 237 | + @Override |
| 238 | + public void cancel() { |
| 239 | + RateLimitableRequestPublisher.this.cancel(); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments