| 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.concurrent.atomic.AtomicLong; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 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.*; |
| 25 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 26 |
import io.reactivex.rxjava3.operators.*; |
| 27 |
|
| 28 |
public final class FlowableOnBackpressureBuffer<T> extends AbstractFlowableWithUpstream<T, T> { |
| 29 |
final int bufferSize; |
| 30 |
final boolean unbounded; |
| 31 |
final boolean delayError; |
| 32 |
final Action onOverflow; |
| 33 |
|
| 34 |
public FlowableOnBackpressureBuffer(Flowable<T> source, int bufferSize, boolean unbounded, |
| 35 |
boolean delayError, Action onOverflow) { |
| 36 |
super(source); |
| 37 |
this.bufferSize = bufferSize; |
| 38 |
this.unbounded = unbounded; |
| 39 |
this.delayError = delayError; |
| 40 |
this.onOverflow = onOverflow; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 45 |
source.subscribe(new BackpressureBufferSubscriber<>(s, bufferSize, unbounded, delayError, onOverflow)); |
| 46 |
} |
| 47 |
|
| 48 |
static final class BackpressureBufferSubscriber<T> extends BasicIntQueueSubscription<T> implements FlowableSubscriber<T> { |
| 49 |
|
| 50 |
private static final long serialVersionUID = -2514538129242366402L; |
| 51 |
|
| 52 |
final Subscriber<? super T> downstream; |
| 53 |
final SimplePlainQueue<T> queue; |
| 54 |
final boolean delayError; |
| 55 |
final Action onOverflow; |
| 56 |
|
| 57 |
Subscription upstream; |
| 58 |
|
| 59 |
volatile boolean cancelled; |
| 60 |
|
| 61 |
volatile boolean done; |
| 62 |
Throwable error; |
| 63 |
|
| 64 |
final AtomicLong requested = new AtomicLong(); |
| 65 |
|
| 66 |
boolean outputFused; |
| 67 |
|
| 68 |
BackpressureBufferSubscriber(Subscriber<? super T> actual, int bufferSize, |
| 69 |
boolean unbounded, boolean delayError, Action onOverflow) { |
| 70 |
this.downstream = actual; |
| 71 |
this.onOverflow = onOverflow; |
| 72 |
this.delayError = delayError; |
| 73 |
|
| 74 |
SimplePlainQueue<T> q; |
| 75 |
|
| 76 |
if (unbounded) { |
| 77 |
q = new SpscLinkedArrayQueue<>(bufferSize); |
| 78 |
} else { |
| 79 |
q = new SpscArrayQueue<>(bufferSize); |
| 80 |
} |
| 81 |
|
| 82 |
this.queue = q; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void onSubscribe(Subscription s) { |
| 87 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 88 |
this.upstream = s; |
| 89 |
downstream.onSubscribe(this); |
| 90 |
s.request(Long.MAX_VALUE); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void onNext(T t) { |
| 96 |
if (!queue.offer(t)) { |
| 97 |
upstream.cancel(); |
| 98 |
MissingBackpressureException ex = new MissingBackpressureException("Buffer is full"); |
| 99 |
try { |
| 100 |
onOverflow.run(); |
| 101 |
} catch (Throwable e) { |
| 102 |
Exceptions.throwIfFatal(e); |
| 103 |
ex.initCause(e); |
| 104 |
} |
| 105 |
onError(ex); |
| 106 |
return; |
| 107 |
} |
| 108 |
if (outputFused) { |
| 109 |
downstream.onNext(null); |
| 110 |
} else { |
| 111 |
drain(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void onError(Throwable t) { |
| 117 |
error = t; |
| 118 |
done = true; |
| 119 |
if (outputFused) { |
| 120 |
downstream.onError(t); |
| 121 |
} else { |
| 122 |
drain(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void onComplete() { |
| 128 |
done = true; |
| 129 |
if (outputFused) { |
| 130 |
downstream.onComplete(); |
| 131 |
} else { |
| 132 |
drain(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void request(long n) { |
| 138 |
if (!outputFused) { |
| 139 |
if (SubscriptionHelper.validate(n)) { |
| 140 |
BackpressureHelper.add(requested, n); |
| 141 |
drain(); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void cancel() { |
| 148 |
if (!cancelled) { |
| 149 |
cancelled = true; |
| 150 |
upstream.cancel(); |
| 151 |
|
| 152 |
if (!outputFused && getAndIncrement() == 0) { |
| 153 |
queue.clear(); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
void drain() { |
| 159 |
if (getAndIncrement() == 0) { |
| 160 |
int missed = 1; |
| 161 |
final SimplePlainQueue<T> q = queue; |
| 162 |
final Subscriber<? super T> a = downstream; |
| 163 |
for (;;) { |
| 164 |
|
| 165 |
if (checkTerminated(done, q.isEmpty(), a)) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
long r = requested.get(); |
| 170 |
|
| 171 |
long e = 0L; |
| 172 |
|
| 173 |
while (e != r) { |
| 174 |
boolean d = done; |
| 175 |
T v = q.poll(); |
| 176 |
boolean empty = v == null; |
| 177 |
|
| 178 |
if (checkTerminated(d, empty, a)) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
if (empty) { |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
a.onNext(v); |
| 187 |
|
| 188 |
e++; |
| 189 |
} |
| 190 |
|
| 191 |
if (e == r) { |
| 192 |
boolean d = done; |
| 193 |
boolean empty = q.isEmpty(); |
| 194 |
|
| 195 |
if (checkTerminated(d, empty, a)) { |
| 196 |
return; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if (e != 0L) { |
| 201 |
if (r != Long.MAX_VALUE) { |
| 202 |
requested.addAndGet(-e); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
missed = addAndGet(-missed); |
| 207 |
if (missed == 0) { |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
boolean checkTerminated(boolean d, boolean empty, Subscriber<? super T> a) { |
| 215 |
if (cancelled) { |
| 216 |
queue.clear(); |
| 217 |
return true; |
| 218 |
} |
| 219 |
if (d) { |
| 220 |
if (delayError) { |
| 221 |
if (empty) { |
| 222 |
Throwable e = error; |
| 223 |
if (e != null) { |
| 224 |
a.onError(e); |
| 225 |
} else { |
| 226 |
a.onComplete(); |
| 227 |
} |
| 228 |
return true; |
| 229 |
} |
| 230 |
} else { |
| 231 |
Throwable e = error; |
| 232 |
if (e != null) { |
| 233 |
queue.clear(); |
| 234 |
a.onError(e); |
| 235 |
return true; |
| 236 |
} else |
| 237 |
if (empty) { |
| 238 |
a.onComplete(); |
| 239 |
return true; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public int requestFusion(int mode) { |
| 248 |
if ((mode & ASYNC) != 0) { |
| 249 |
outputFused = true; |
| 250 |
return ASYNC; |
| 251 |
} |
| 252 |
return NONE; |
| 253 |
} |
| 254 |
|
| 255 |
@Nullable |
| 256 |
@Override |
| 257 |
public T poll() { |
| 258 |
return queue.poll(); |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public void clear() { |
| 263 |
queue.clear(); |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public boolean isEmpty() { |
| 268 |
return queue.isEmpty(); |
| 269 |
} |
| 270 |
} |
| 271 |
} |