long calculateSecondsUntilNextRefresh()

in core/src/main/java/com/google/cloud/sql/core/RefreshCalculator.java [33:48]


  long calculateSecondsUntilNextRefresh(Instant now, Instant expiration) {
    Duration timeUntilExp = Duration.between(now, expiration);

    if (timeUntilExp.compareTo(Duration.ofHours(1)) < 0) {
      if (timeUntilExp.compareTo(DEFAULT_REFRESH_BUFFER) < 0) {
        // If the time until the certificate expires is less the refresh buffer, schedule the
        // refresh immediately
        return 0;
      }
      // Otherwise schedule a refresh in (timeUntilExp - buffer) seconds
      return timeUntilExp.minus(DEFAULT_REFRESH_BUFFER).getSeconds();
    }

    // If the time until the certificate expires is longer than an hour, return timeUntilExp//2
    return timeUntilExp.dividedBy(2).getSeconds();
  }