All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.akarnokd.rxjava2.internal.operators.OperatorTimeoutTimed Maven / Gradle / Ivy

/**
 * Copyright 2015 David Karnok and Netflix, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
 * the License for the specific language governing permissions and limitations under the License.
 */

package hu.akarnokd.rxjava2.internal.operators;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.Scheduler;
import hu.akarnokd.rxjava2.Scheduler.Worker;
import hu.akarnokd.rxjava2.disposables.Disposable;
import hu.akarnokd.rxjava2.internal.subscribers.FullArbiterSubscriber;
import hu.akarnokd.rxjava2.internal.subscriptions.*;
import hu.akarnokd.rxjava2.plugins.RxJavaPlugins;
import hu.akarnokd.rxjava2.subscribers.SerializedSubscriber;

public final class OperatorTimeoutTimed implements Operator {
    final long timeout;
    final TimeUnit unit;
    final Scheduler scheduler;
    final Publisher other;
    
    public OperatorTimeoutTimed(long timeout, TimeUnit unit, Scheduler scheduler, Publisher other) {
        this.timeout = timeout;
        this.unit = unit;
        this.scheduler = scheduler;
        this.other = other;
    }

    @Override
    public Subscriber apply(Subscriber t) {
        if (other == null) {
            return new TimeoutTimedSubscriber(
                    new SerializedSubscriber(t), // because errors can race 
                    timeout, unit, scheduler.createWorker());
        }
        return new TimeoutTimedOtherSubscriber(
                t, // the FullArbiter serializes
                timeout, unit, scheduler.createWorker(), other);
    }
    
    static final class TimeoutTimedOtherSubscriber implements Subscriber, Disposable {
        final Subscriber actual;
        final long timeout;
        final TimeUnit unit;
        final Scheduler.Worker worker;
        final Publisher other;
        
        Subscription s; 
        
        final FullArbiter arbiter;

        volatile Disposable timer;
        @SuppressWarnings("rawtypes")
        static final AtomicReferenceFieldUpdater TIMER =
                AtomicReferenceFieldUpdater.newUpdater(TimeoutTimedOtherSubscriber.class, Disposable.class, "timer");

        static final Disposable CANCELLED = new Disposable() {
            @Override
            public void dispose() { }
        };

        static final Disposable NEW_TIMER = new Disposable() {
            @Override
            public void dispose() { }
        };

        volatile long index;
        
        volatile boolean done;
        
        public TimeoutTimedOtherSubscriber(Subscriber actual, long timeout, TimeUnit unit, Worker worker,
                Publisher other) {
            this.actual = actual;
            this.timeout = timeout;
            this.unit = unit;
            this.worker = worker;
            this.other = other;
            this.arbiter = new FullArbiter(actual, this, 8);
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            
            this.s = s;
            if (arbiter.setSubscription(s)) {
                actual.onSubscribe(arbiter);
                
                scheduleTimeout(0L);
            }
        }
        
        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }
            long idx = index + 1;
            index = idx;
            
            if (arbiter.onNext(t, s)) {
                scheduleTimeout(idx);
            }
        }
        
        void scheduleTimeout(final long idx) {
            Disposable d = timer;
            if (d != null) {
                d.dispose();
            }
            
            if (TIMER.compareAndSet(this, d, NEW_TIMER)) {
                d = worker.schedule(new Runnable() {
                    @Override
                    public void run() {
                        if (idx == index) {
                            done = true;
                            s.cancel();
                            disposeTimer();
                            worker.dispose();
                            
                            if (other == null) {
                                actual.onError(new TimeoutException());
                            } else {
                                subscribeNext();
                            }
                        }
                    }
                }, timeout, unit);
                
                if (!TIMER.compareAndSet(this, NEW_TIMER, d)) {
                    d.dispose();
                }
            }
        }
        
        void subscribeNext() {
            other.subscribe(new FullArbiterSubscriber(arbiter));
        }
        
        @Override
        public void onError(Throwable t) {
            if (done) {
                RxJavaPlugins.onError(t);
                return;
            }
            done = true;
            worker.dispose();
            disposeTimer();
            arbiter.onError(t, s);
        }
        
        @Override
        public void onComplete() {
            if (done) {
                return;
            }
            done = true;
            worker.dispose();
            disposeTimer();
            arbiter.onComplete(s);
        }
        
        @Override
        public void dispose() {
            worker.dispose();
            disposeTimer();
        }
        
        public void disposeTimer() {
            Disposable d = timer;
            if (d != CANCELLED) {
                d = TIMER.getAndSet(this, CANCELLED);
                if (d != CANCELLED && d != null) {
                    d.dispose();
                }
            }
        }
    }
    
    static final class TimeoutTimedSubscriber implements Subscriber, Disposable, Subscription {
        final Subscriber actual;
        final long timeout;
        final TimeUnit unit;
        final Scheduler.Worker worker;
        
        Subscription s; 
        
        volatile Disposable timer;
        @SuppressWarnings("rawtypes")
        static final AtomicReferenceFieldUpdater TIMER =
                AtomicReferenceFieldUpdater.newUpdater(TimeoutTimedSubscriber.class, Disposable.class, "timer");

        static final Disposable CANCELLED = new Disposable() {
            @Override
            public void dispose() { }
        };

        static final Disposable NEW_TIMER = new Disposable() {
            @Override
            public void dispose() { }
        };

        volatile long index;
        
        volatile boolean done;
        
        public TimeoutTimedSubscriber(Subscriber actual, long timeout, TimeUnit unit, Worker worker) {
            this.actual = actual;
            this.timeout = timeout;
            this.unit = unit;
            this.worker = worker;
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            
            this.s = s;
            actual.onSubscribe(s);
            scheduleTimeout(0L);
        }
        
        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }
            long idx = index + 1;
            index = idx;

            actual.onNext(t);
            
            scheduleTimeout(idx);
        }
        
        void scheduleTimeout(final long idx) {
            Disposable d = timer;
            if (d != null) {
                d.dispose();
            }
            
            if (TIMER.compareAndSet(this, d, NEW_TIMER)) {
                d = worker.schedule(new Runnable() {
                    @Override
                    public void run() {
                        if (idx == index) {
                            done = true;
                            s.cancel();
                            dispose();
                            
                            actual.onError(new TimeoutException());
                        }
                    }
                }, timeout, unit);
                
                if (!TIMER.compareAndSet(this, NEW_TIMER, d)) {
                    d.dispose();
                }
            }
        }
        
        @Override
        public void onError(Throwable t) {
            if (done) {
                RxJavaPlugins.onError(t);
                return;
            }
            done = true;
            dispose();
            
            actual.onError(t);
        }
        
        @Override
        public void onComplete() {
            if (done) {
                return;
            }
            done = true;
            dispose();
            
            actual.onComplete();
        }
        
        @Override
        public void dispose() {
            worker.dispose();
            disposeTimer();
        }
        
        public void disposeTimer() {
            Disposable d = timer;
            if (d != CANCELLED) {
                d = TIMER.getAndSet(this, CANCELLED);
                if (d != CANCELLED && d != null) {
                    d.dispose();
                }
            }
        }
        
        @Override
        public void request(long n) {
            s.request(n);
        }
        
        @Override
        public void cancel() {
            dispose();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy