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

io.split.engine.common.Backoff Maven / Gradle / Ivy

There is a newer version: 4.13.0
Show newest version
package io.split.engine.common;

import java.util.concurrent.atomic.AtomicInteger;

import static com.google.common.base.Preconditions.checkNotNull;

public class Backoff {
    private static final long BACKOFF_MAX_SECONDS_ALLOWED = 1800;

    private final long _backoffBase;
    private AtomicInteger _attempt;

    public Backoff(long backoffBase) {
        _backoffBase = checkNotNull(backoffBase);
        _attempt = new AtomicInteger(0);
    }

    public long interval() {
        long interval = _backoffBase * (long) Math.pow(2, _attempt.getAndIncrement());

        return interval >= BACKOFF_MAX_SECONDS_ALLOWED ? BACKOFF_MAX_SECONDS_ALLOWED : interval;
    }

    public synchronized void reset() {
        _attempt.set(0);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy