Skip to content

Too high backoff delay values cause long overflow #215

@BartoszSiemienczuk

Description

@BartoszSiemienczuk

While building a network policy with high delay values I receive a long overflow exception.

java.lang.ArithmeticException: long overflow
	at java.lang.Math.multiplyExact(Math.java:892)
	at java.time.Duration.toNanos(Duration.java:1186)
	at net.jodah.failsafe.RetryPolicy.withBackoff(RetryPolicy.java:394)

The code to replciate this may be something like this:

 final RetryPolicy<Object> retryPolicy = new RetryPolicy<>()
                .withBackoff(500, Integer.MAX_VALUE, ChronoUnit.MILLIS, 1.50)

It then tries to convert that to nanoseconds to compare the values which is causing the overflow.


  public RetryPolicy<R> withBackoff(long delay, long maxDelay, ChronoUnit chronoUnit, double delayFactor) {
   (...)
     Duration delayDuration = Duration.of(delay, chronoUnit);
    Duration maxDelayDuration = Duration.of(maxDelay, chronoUnit);
    Assert.state(maxDuration == null || delayDuration.toNanos() < maxDuration.toNanos(),
      "delay must be less than the maxDuration");
    Assert.isTrue(delayDuration.toNanos() < maxDelayDuration.toNanos(), "delay must be less than the maxDelay");
   (...)
    return this;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions