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

io.pyroscope.javaagent.impl.ExponentialBackoff Maven / Gradle / Ivy

There is a newer version: 0.15.2
Show newest version
package io.pyroscope.javaagent.impl;

import java.util.Random;

/**
 * Exponential backoff counter implementing the Full Jitter algorithm from
 * here.
 */
final class ExponentialBackoff {
    private final Random random;

    private final int base;
    private final int cap;

    private int attempt = -1;

    ExponentialBackoff(final int base, final int cap, final Random random) {
        this.base = base;
        this.cap = cap;
        this.random = random;
    }

    final int error() {
        attempt += 1;
        int multiplier = cap / base;
        // from https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.19
        // "If the promoted type of the left-hand operand is int, then only the five lowest-order bits of the right-hand operand are used as the shift distance".
        if (attempt < 32 && (multiplier >> attempt) > 0) {
            multiplier = 1 << attempt;
        }
        return random.nextInt(base * multiplier);
    }

    final void reset() {
        attempt = -1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy