1
zj
2025-08-02 c353a3666b23f0d5c3f341b3d5d9626c7892f68e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.nq.ws.backo;
 
import java.math.BigDecimal;
import java.math.BigInteger;
 
/**
 * Imported from https://github.com/mokesmokes/backo
 */
public class Backoff {
 
    private long ms = 100;
    private long max = 10000;
    private int factor = 2;
    private double jitter;
    private int attempts;
 
    public Backoff() {}
 
    public long duration() {
        BigInteger ms = BigInteger.valueOf(this.ms)
                .multiply(BigInteger.valueOf(this.factor).pow(this.attempts++));
        if (jitter != 0.0) {
            double rand = Math.random();
            BigInteger deviation = BigDecimal.valueOf(rand)
                    .multiply(BigDecimal.valueOf(jitter))
                    .multiply(new BigDecimal(ms)).toBigInteger();
            ms = (((int) Math.floor(rand * 10)) & 1) == 0 ? ms.subtract(deviation) : ms.add(deviation);
        }
        return ms
                .min(BigInteger.valueOf(this.max))
                .max(BigInteger.valueOf(this.ms))
                .longValue();
    }
 
    public void reset() {
        this.attempts = 0;
    }
 
    public Backoff setMin(long min) {
        this.ms = min;
        return this;
    }
 
    public Backoff setMax(long max) {
        this.max = max;
        return this;
    }
 
    public Backoff setFactor(int factor) {
        this.factor = factor;
        return this;
    }
 
    public Backoff setJitter(double jitter) {
        boolean isValid = jitter >= 0 && jitter < 1;
        if (!isValid) {
            throw new IllegalArgumentException("jitter must be between 0 and 1");
        }
        this.jitter = jitter;
        return this;
    }
 
    public int getAttempts() {
        return this.attempts;
    }
}