Files
socket.io-client-java/src/main/java/io/socket/backo/Backoff.java
Damien Arrachequesne 0cbf01eb25 fix: ensure randomizationFactor is always between 0 and 1
Using a randomizationFactor value above 1 could lead to generating a
negative duration, then throwing:

> java.lang.IllegalArgumentException: delay < 0: -1012
> at java.util.Timer.schedule(Timer.java:454)
> at io.socket.client.Manager.reconnect(Manager.java:544)

This error does not seem related to a long overflow (in the
BigInteger.longValue() operation), because the BigInteger.min(this.max)
operation before should prevent it.

Related: https://github.com/socketio/socket.io-client-java/issues/349
2022-07-02 09:02:48 +02:00

67 lines
1.7 KiB
Java

package io.socket.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;
}
}