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

com.smartystreets.api.RetrySender Maven / Gradle / Ivy

There is a newer version: 3.18.3
Show newest version
package com.smartystreets.api;

import com.smartystreets.api.exceptions.SmartyException;
import com.smartystreets.api.exceptions.TooManyRequestsException;

import java.io.IOException;

public class RetrySender implements Sender {
    public final int MAX_BACKOFF_DURATION = 10;
    private Sender inner;
    private int maxRetries;
    private Sleeper sleeper;
    private Logger logger;

    public RetrySender(int maxRetries, Sleeper sleeper, Logger logger, Sender inner) {
        this.maxRetries = maxRetries;
        this.sleeper = sleeper;
        this.logger = logger;
        this.inner = inner;
    }

    public Response send(Request request) throws SmartyException, IOException {
        for (int i = 0; i <= this.maxRetries; i++) {
            Response response = this.trySend(request, i);

            if (response != null) {
                return response;
            }
        }
        return null;
    }

    private Response trySend(Request request, int attempt) throws SmartyException, IOException {
        try {
            return this.inner.send(request);
        } catch (TooManyRequestsException ex) {
            if (attempt >= this.maxRetries)
                throw ex;
            try {
                this.sleeper.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException ex) {
            if (attempt >= this.maxRetries)
                throw ex;
        }

        this.backoff(attempt);

        return null;
    }

    private void backoff(int attempt) {
        long backoffDuration = Math.min(attempt, MAX_BACKOFF_DURATION);

        this.logger.log("There was an error processing the request. Retrying in "+backoffDuration+" seconds...");

        try {
            this.sleeper.sleep(backoffDuration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy