| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.flowable; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.*; |
| 23 |
import io.reactivex.rxjava3.functions.Action; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
|
| 28 |
/** |
| 29 |
* Handle backpressure with a bounded buffer and custom strategy. |
| 30 |
* |
| 31 |
* @param <T> the input and output value type |
| 32 |
*/ |
| 33 |
public final class FlowableOnBackpressureBufferStrategy<T> extends AbstractFlowableWithUpstream<T, T> { |
| 34 |
|
| 35 |
final long bufferSize; |
| 36 |
|
| 37 |
final Action onOverflow; |
| 38 |
|
| 39 |
final BackpressureOverflowStrategy strategy; |
| 40 |
|
| 41 |
public FlowableOnBackpressureBufferStrategy(Flowable<T> source, |
| 42 |
long bufferSize, Action onOverflow, BackpressureOverflowStrategy strategy) { |
| 43 |
super(source); |
| 44 |
this.bufferSize = bufferSize; |
| 45 |
this.onOverflow = onOverflow; |
| 46 |
this.strategy = strategy; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 51 |
source.subscribe(new OnBackpressureBufferStrategySubscriber<>(s, onOverflow, strategy, bufferSize)); |
| 52 |
} |
| 53 |
|
| 54 |
static final class OnBackpressureBufferStrategySubscriber<T> |
| 55 |
extends AtomicInteger |
| 56 |
implements FlowableSubscriber<T>, Subscription { |
| 57 |
|
| 58 |
private static final long serialVersionUID = 3240706908776709697L; |
| 59 |
|
| 60 |
final Subscriber<? super T> downstream; |
| 61 |
|
| 62 |
final Action onOverflow; |
| 63 |
|
| 64 |
final BackpressureOverflowStrategy strategy; |
| 65 |
|
| 66 |
final long bufferSize; |
| 67 |
|
| 68 |
final AtomicLong requested; |
| 69 |
|
| 70 |
final Deque<T> deque; |
| 71 |
|
| 72 |
Subscription upstream; |
| 73 |
|
| 74 |
volatile boolean cancelled; |
| 75 |
|
| 76 |
volatile boolean done; |
| 77 |
Throwable error; |
| 78 |
|
| 79 |
OnBackpressureBufferStrategySubscriber(Subscriber<? super T> actual, Action onOverflow, |
| 80 |
BackpressureOverflowStrategy strategy, long bufferSize) { |
| 81 |
this.downstream = actual; |
| 82 |
this.onOverflow = onOverflow; |
| 83 |
this.strategy = strategy; |
| 84 |
this.bufferSize = bufferSize; |
| 85 |
this.requested = new AtomicLong(); |
| 86 |
this.deque = new ArrayDeque<>(); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void onSubscribe(Subscription s) { |
| 91 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 92 |
this.upstream = s; |
| 93 |
|
| 94 |
downstream.onSubscribe(this); |
| 95 |
|
| 96 |
s.request(Long.MAX_VALUE); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void onNext(T t) { |
| 102 |
if (done) { |
| 103 |
return; |
| 104 |
} |
| 105 |
boolean callOnOverflow = false; |
| 106 |
boolean callError = false; |
| 107 |
Deque<T> dq = deque; |
| 108 |
synchronized (dq) { |
| 109 |
if (dq.size() == bufferSize) { |
| 110 |
switch (strategy) { |
| 111 |
case DROP_LATEST: |
| 112 |
dq.pollLast(); |
| 113 |
dq.offer(t); |
| 114 |
callOnOverflow = true; |
| 115 |
break; |
| 116 |
case DROP_OLDEST: |
| 117 |
dq.poll(); |
| 118 |
dq.offer(t); |
| 119 |
callOnOverflow = true; |
| 120 |
break; |
| 121 |
default: |
| 122 |
// signal error |
| 123 |
callError = true; |
| 124 |
break; |
| 125 |
} |
| 126 |
} else { |
| 127 |
dq.offer(t); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (callOnOverflow) { |
| 132 |
if (onOverflow != null) { |
| 133 |
try { |
| 134 |
onOverflow.run(); |
| 135 |
} catch (Throwable ex) { |
| 136 |
Exceptions.throwIfFatal(ex); |
| 137 |
upstream.cancel(); |
| 138 |
onError(ex); |
| 139 |
} |
| 140 |
} |
| 141 |
} else if (callError) { |
| 142 |
upstream.cancel(); |
| 143 |
onError(new MissingBackpressureException()); |
| 144 |
} else { |
| 145 |
drain(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onError(Throwable t) { |
| 151 |
if (done) { |
| 152 |
RxJavaPlugins.onError(t); |
| 153 |
return; |
| 154 |
} |
| 155 |
error = t; |
| 156 |
done = true; |
| 157 |
drain(); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void onComplete() { |
| 162 |
done = true; |
| 163 |
drain(); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void request(long n) { |
| 168 |
if (SubscriptionHelper.validate(n)) { |
| 169 |
BackpressureHelper.add(requested, n); |
| 170 |
drain(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void cancel() { |
| 176 |
cancelled = true; |
| 177 |
upstream.cancel(); |
| 178 |
|
| 179 |
if (getAndIncrement() == 0) { |
| 180 |
clear(deque); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void clear(Deque<T> dq) { |
| 185 |
synchronized (dq) { |
| 186 |
dq.clear(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void drain() { |
| 191 |
if (getAndIncrement() != 0) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
int missed = 1; |
| 196 |
Deque<T> dq = deque; |
| 197 |
Subscriber<? super T> a = downstream; |
| 198 |
for (;;) { |
| 199 |
long r = requested.get(); |
| 200 |
long e = 0L; |
| 201 |
while (e != r) { |
| 202 |
if (cancelled) { |
| 203 |
clear(dq); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
boolean d = done; |
| 208 |
|
| 209 |
T v; |
| 210 |
|
| 211 |
synchronized (dq) { |
| 212 |
v = dq.poll(); |
| 213 |
} |
| 214 |
|
| 215 |
boolean empty = v == null; |
| 216 |
|
| 217 |
if (d) { |
| 218 |
Throwable ex = error; |
| 219 |
if (ex != null) { |
| 220 |
clear(dq); |
| 221 |
a.onError(ex); |
| 222 |
return; |
| 223 |
} |
| 224 |
if (empty) { |
| 225 |
a.onComplete(); |
| 226 |
return; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if (empty) { |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
a.onNext(v); |
| 235 |
|
| 236 |
e++; |
| 237 |
} |
| 238 |
|
| 239 |
if (e == r) { |
| 240 |
if (cancelled) { |
| 241 |
clear(dq); |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
boolean d = done; |
| 246 |
|
| 247 |
boolean empty; |
| 248 |
|
| 249 |
synchronized (dq) { |
| 250 |
empty = dq.isEmpty(); |
| 251 |
} |
| 252 |
|
| 253 |
if (d) { |
| 254 |
Throwable ex = error; |
| 255 |
if (ex != null) { |
| 256 |
clear(dq); |
| 257 |
a.onError(ex); |
| 258 |
return; |
| 259 |
} |
| 260 |
if (empty) { |
| 261 |
a.onComplete(); |
| 262 |
return; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if (e != 0L) { |
| 268 |
BackpressureHelper.produced(requested, e); |
| 269 |
} |
| 270 |
|
| 271 |
missed = addAndGet(-missed); |
| 272 |
if (missed == 0) { |
| 273 |
break; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |