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

com.alibaba.dashscope.protocol.RetryWithDelay Maven / Gradle / Ivy

The newest version!
// Copyright (c) Alibaba, Inc. and its affiliates.

package com.alibaba.dashscope.protocol;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import java.util.concurrent.TimeUnit;

public class RetryWithDelay implements Function, Observable> {
  private final int maxRetries;
  private final int retryDelayMillis;
  private int retryCount;

  public RetryWithDelay(final int maxRetries, final int retryDelayMillis) {
    this.maxRetries = maxRetries;
    this.retryDelayMillis = retryDelayMillis;
    this.retryCount = 0;
  }

  @Override
  public Observable apply(Observable attempts) {
    return attempts.flatMap(
        new Function>() {
          @Override
          public Observable apply(Throwable throwable) {
            if (++retryCount < maxRetries) {
              // When this Observable calls onNext, the original
              // Observable will be retried (i.e. re-subscribed).
              return Observable.timer(retryDelayMillis, TimeUnit.MILLISECONDS);
            }

            // Max retries hit. Just pass the error along.
            return Observable.error(throwable);
          }
        });
  }
}

// observable.retryWhen(new RetryWithDelay(3, 2000));




© 2015 - 2024 Weber Informatics LLC | Privacy Policy