|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 3 | + * <p> |
| 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 | + * <p> |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * <p> |
| 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.internal.operators.flowable; |
| 15 | + |
| 16 | +import io.reactivex.exceptions.MissingBackpressureException; |
| 17 | +import io.reactivex.internal.subscriptions.SubscriptionHelper; |
| 18 | +import io.reactivex.internal.util.BackpressureHelper; |
| 19 | +import io.reactivex.plugins.RxJavaPlugins; |
| 20 | +import org.reactivestreams.Publisher; |
| 21 | +import org.reactivestreams.Subscriber; |
| 22 | +import org.reactivestreams.Subscription; |
| 23 | + |
| 24 | +import java.util.concurrent.atomic.AtomicLong; |
| 25 | + |
| 26 | +public final class FlowableOnBackpressureError<T> extends AbstractFlowableWithUpstream<T, T> { |
| 27 | + |
| 28 | + |
| 29 | + public FlowableOnBackpressureError(Publisher<T> source) { |
| 30 | + super(source); |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void subscribeActual(Subscriber<? super T> s) { |
| 36 | + this.source.subscribe(new BackpressureErrorSubscriber<T>(s)); |
| 37 | + } |
| 38 | + |
| 39 | + static final class BackpressureErrorSubscriber<T> |
| 40 | + extends AtomicLong implements Subscriber<T>, Subscription { |
| 41 | + |
| 42 | + final Subscriber<? super T> actual; |
| 43 | + Subscription s; |
| 44 | + boolean done; |
| 45 | + |
| 46 | + BackpressureErrorSubscriber(Subscriber<? super T> actual) { |
| 47 | + this.actual = actual; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onSubscribe(Subscription s) { |
| 52 | + if (SubscriptionHelper.validate(this.s, s)) { |
| 53 | + this.s = s; |
| 54 | + actual.onSubscribe(this); |
| 55 | + s.request(Long.MAX_VALUE); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onNext(T t) { |
| 61 | + if (done) { |
| 62 | + return; |
| 63 | + } |
| 64 | + long r = get(); |
| 65 | + if (r != 0L) { |
| 66 | + actual.onNext(t); |
| 67 | + if (r != Long.MAX_VALUE) { |
| 68 | + decrementAndGet(); |
| 69 | + } |
| 70 | + } else { |
| 71 | + onError(new MissingBackpressureException("could not emit value due to lack of requests")); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onError(Throwable t) { |
| 77 | + if (done) { |
| 78 | + RxJavaPlugins.onError(t); |
| 79 | + return; |
| 80 | + } |
| 81 | + done = true; |
| 82 | + actual.onError(t); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onComplete() { |
| 87 | + if (done) { |
| 88 | + return; |
| 89 | + } |
| 90 | + done = true; |
| 91 | + actual.onComplete(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void request(long n) { |
| 96 | + if (SubscriptionHelper.validate(n)) { |
| 97 | + BackpressureHelper.add(this, n); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void cancel() { |
| 103 | + s.cancel(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments