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

org.zalando.riptide.failsafe.RetryRequestPolicy Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package org.zalando.riptide.failsafe;

import com.google.common.annotations.VisibleForTesting;
import dev.failsafe.Policy;
import dev.failsafe.RetryPolicy;
import dev.failsafe.event.EventListener;
import dev.failsafe.event.ExecutionAttemptedEvent;
import lombok.AllArgsConstructor;
import org.apiguardian.api.API;
import org.springframework.http.client.ClientHttpResponse;
import org.zalando.riptide.RequestArguments;
import org.zalando.riptide.idempotency.IdempotencyPredicate;

import java.util.function.Predicate;

import static lombok.AccessLevel.PRIVATE;
import static org.apiguardian.api.API.Status.EXPERIMENTAL;

@API(status = EXPERIMENTAL)
@AllArgsConstructor(access = PRIVATE)
public final class RetryRequestPolicy implements RequestPolicy {

    private final RetryPolicy policy;
    private final Predicate predicate;
    private final RetryListener listener;

    public RetryRequestPolicy(final RetryPolicy policy) {
        this(policy, new IdempotencyPredicate(), RetryListener.DEFAULT);
    }

    public RetryRequestPolicy withPredicate(
            final Predicate predicate) {
        return new RetryRequestPolicy(policy, predicate, listener);
    }

    public RetryRequestPolicy withListener(final RetryListener listener) {
        return new RetryRequestPolicy(policy, predicate, listener);
    }

    @Override
    public boolean applies(final RequestArguments arguments) {
        return predicate.test(arguments);
    }

    @Override
    public Policy prepare(
            final RequestArguments arguments) {

        return RetryPolicy.builder(policy.getConfig())
                .onFailedAttempt(new RetryListenerAdapter(listener, arguments))
                .build();
    }

    @VisibleForTesting
    @AllArgsConstructor
    static final class RetryListenerAdapter implements
            EventListener> {

        private final RetryListener listener;
        private final RequestArguments arguments;

        @Override
        public void accept(
                final ExecutionAttemptedEvent event) {
            listener.onRetry(arguments, event);
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy