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

io.mantisrx.runtime.source.http.impl.OperatorResumeOnError Maven / Gradle / Ivy

There is a newer version: 3.1.7
Show newest version
/*
 * Copyright 2019 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 io.mantisrx.runtime.source.http.impl;

import rx.Observable;
import rx.Observable.Operator;
import rx.Scheduler;
import rx.Scheduler.Worker;
import rx.Subscriber;
import rx.functions.Action0;
import rx.schedulers.Schedulers;
import rx.subscriptions.SerialSubscription;


public class OperatorResumeOnError implements Operator {

    private static final Scheduler scheduler = Schedulers.trampoline();
    private final ResumeOnErrorPolicy resumePolicy;
    private final int currentAttempts;


    private OperatorResumeOnError(int currentAttempts, ResumeOnErrorPolicy resumePolicy) {
        this.currentAttempts = currentAttempts;
        this.resumePolicy = resumePolicy;
    }

    public OperatorResumeOnError(ResumeOnErrorPolicy resumePolicy) {
        this(0, resumePolicy);
    }

    @Override
    public Subscriber call(final Subscriber child) {
        final SerialSubscription serialSubscription = new SerialSubscription();
        child.add(serialSubscription);

        return new Subscriber(child) {
            private final Worker worker = scheduler.createWorker();

            @Override
            public void onCompleted() {
                child.onCompleted();
            }

            @Override
            public void onError(final Throwable e) {
                worker.schedule(new Action0() {
                    @Override
                    public void call() {
                        try {
                            int newAttempts = currentAttempts + 1;
                            Observable resume = resumePolicy.call(newAttempts, e);

                            if (resume == null) {
                                child.onError(e);
                            } else {
                                resume = resume.lift(new OperatorResumeOnError<>(
                                        newAttempts,
                                        resumePolicy
                                ));

                                serialSubscription.set(resume.unsafeSubscribe(child));
                            }
                        } catch (Throwable e2) {
                            child.onError(e2);
                        }
                    }
                });
            }

            @Override
            public void onNext(T t) {
                child.onNext(t);
            }

        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy