class RateLimiter {

  public boolean checkCredit(double itemCost) {
    long cost = (long) (itemCost / creditsPerNanosecond);
    long credit;
    long currentDebit;
    long balance;
    do {
      currentDebit = debit.get();
      credit = clock.nanoTime();
      balance = credit - currentDebit;
      if (balance > maxBalance) {
        balance = maxBalance;
      }
      balance -= cost;
      if (balance < 0) {
        return false;
      }
    } while (!debit.compareAndSet(currentDebit, credit - balance));
    return true;
  }

    RateLimiter(double creditsPerSecond, double maxBalance, Clock clock);

}