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

com.azure.cosmos.implementation.BackoffRetryUtility Maven / Gradle / Ivy

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.cosmos.implementation;

import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.function.Function;

/**
 * While this class is public, but it is not part of our published public APIs.
 * This is meant to be internally used only by our sdk.
 */
public class BackoffRetryUtility {

    // transforms a retryFunc to a function which can be used by Observable.retryWhen(.)
    // also it invokes preRetryCallback prior to doing retry.
    public static final Quadruple InitialArgumentValuePolicyArg = Quadruple.with(false, false,
            Duration.ofSeconds(60), 0);

    // a helper method for invoking callback method given the retry policy.
    // it also invokes the pre retry callback prior to retrying

    // a helper method for invoking callback method given the retry policy

    // a helper method for invoking callback method given the retry policy
    static public  Mono executeRetry(Callable> callbackMethod,
                                           IRetryPolicy retryPolicy) {

        return Mono.defer(() -> {
            // TODO: is defer required?
            try {
                return callbackMethod.call();
            } catch (Exception e) {
                return Mono.error(e);
            }
        }).retryWhen(RetryUtils.toRetryWhenFunc(retryPolicy));
    }

    static public  Mono executeAsync(
            Function, Mono> callbackMethod, IRetryPolicy retryPolicy,
            Function, Mono> inBackoffAlternateCallbackMethod,
            Duration minBackoffForInBackoffCallback) {

        return Mono.defer(() -> {
            // TODO: is defer required?
            return callbackMethod.apply(InitialArgumentValuePolicyArg).onErrorResume(
                    RetryUtils.toRetryWithAlternateFunc(callbackMethod, retryPolicy, inBackoffAlternateCallbackMethod,minBackoffForInBackoffCallback));
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy