class BurstyRateLimiterStats extends RateLimiterStats {
  /* The permits per period */
  final long periodPermits;
  /* The nanos per period */
  private final long periodNanos;

  /* Available permits. Can be negative during a deficit. */
  private long availablePermits;
  private long currentPeriod;

  BurstyRateLimiterStats(RateLimiterConfig<?> config, Stopwatch stopwatch) {
    super(stopwatch);
    periodPermits = config.getMaxPermits();
    periodNanos = config.getPeriod().toNanos();
    availablePermits = periodPermits;
  }

  @Override
  public synchronized long acquirePermits(long requestedPermits, Duration maxWaitTime) {<FILL_FUNCTION_BODY>}

  synchronized long getAvailablePermits() {
    return availablePermits;
  }

  synchronized long getCurrentPeriod() {
    return currentPeriod;
  }

  @Override
  synchronized void reset() {
    stopwatch.reset();
    availablePermits = periodPermits;
    currentPeriod = 0;
  }
}

    long currentNanos = stopwatch.elapsedNanos();
    long newCurrentPeriod = currentNanos / periodNanos;

    // Update current period and available permits
    if (currentPeriod < newCurrentPeriod) {
      long elapsedPeriods = newCurrentPeriod - currentPeriod;
      long elapsedPermits = elapsedPeriods * periodPermits;
      currentPeriod = newCurrentPeriod;
      availablePermits = availablePermits < 0 ? availablePermits + elapsedPermits : periodPermits;
    }

    long waitNanos = 0;
    if (requestedPermits > availablePermits) {
      long nextPeriodNanos = (currentPeriod + 1) * periodNanos;
      long nanosToNextPeriod = nextPeriodNanos - currentNanos;
      long permitDeficit = requestedPermits - availablePermits;
      long additionalPeriods = permitDeficit / periodPermits;
      long additionalUnits = permitDeficit % periodPermits;

      // Do not wait for an additional period if we're not using any permits from it
      if (additionalUnits == 0)
        additionalPeriods -= 1;

      // The nanos to wait until the beginning of the next period that will have free permits
      waitNanos = nanosToNextPeriod + (additionalPeriods * periodNanos);

      if (exceedsMaxWaitTime(waitNanos, maxWaitTime))
        return -1;
    }

    availablePermits -= requestedPermits;
    return waitNanos;
  
<methods><variables>final non-sealed dev.failsafe.internal.RateLimiterStats.Stopwatch stopwatch