Java Code Examples for com.google.common.math.LongMath#saturatedAdd()

The following examples show how to use com.google.common.math.LongMath#saturatedAdd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmoothRateLimiter.java    From neural with MIT License 6 votes vote down vote up
@Override
final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
    // 补充令牌
    reSync(nowMicros);

    long returnValue = nextFreeTicketMicros;
    // 获取这次请求消耗的令牌数目
    double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
    double freshPermits = requiredPermits - storedPermitsToSpend;
    long waitMicros = storedPermitsToWaitTime(this.storedPermits,
            storedPermitsToSpend) + (long) (freshPermits * stableIntervalMicros);

    this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
    // 减去消耗的令牌
    this.storedPermits -= storedPermitsToSpend;

    return returnValue;
}
 
Example 2
Source File: SmoothRateLimiter.java    From neural with MIT License 6 votes vote down vote up
@Override
final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
    // 补充令牌
    reSync(nowMicros);

    long returnValue = nextFreeTicketMicros;
    // 获取这次请求消耗的令牌数目
    double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
    double freshPermits = requiredPermits - storedPermitsToSpend;
    long waitMicros = storedPermitsToWaitTime(this.storedPermits,
            storedPermitsToSpend) + (long) (freshPermits * stableIntervalMicros);

    this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
    // 减去消耗的令牌
    this.storedPermits -= storedPermitsToSpend;

    return returnValue;
}
 
Example 3
Source File: TimeoutScheduler.java    From armeria with Apache License 2.0 6 votes vote down vote up
private void extendTimeoutMillis(long adjustmentMillis) {
    if (adjustmentMillis == 0 || timeoutMillis == 0) {
        return;
    }

    final long oldTimeoutMillis = timeoutMillis;
    timeoutMillis = LongMath.saturatedAdd(oldTimeoutMillis, adjustmentMillis);
    final TimeoutController timeoutController = this.timeoutController;
    if (timeoutController != null) {
        if (eventLoop.inEventLoop()) {
            timeoutController.extendTimeout(adjustmentMillis);
        } else {
            eventLoop.execute(() -> timeoutController.extendTimeout(adjustmentMillis));
        }
    } else {
        addPendingTimeoutTask(controller -> controller.extendTimeout(adjustmentMillis));
    }
}
 
Example 4
Source File: OfframpServiceV2.java    From data-highway with Apache License 2.0 5 votes vote down vote up
private void handleRequest(Request request) {
  if (!initialised) {
    consumer.init(request.getCount(), this::sendRebalance);
    initialised = true;
  }
  if (request.getCount() > 0) {
    requested = LongMath.saturatedAdd(requested, request.getCount());
  } else if (request.getCount() < 0) {
    sendError(
        new IllegalArgumentException(
            String.format("Requested count cannot be negative value (given %d)", request.getCount())));
  }
}
 
Example 5
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
  final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
    resync(nowMicros);
    long returnValue = nextFreeTicketMicros;
    double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
    double freshPermits = requiredPermits - storedPermitsToSpend;
    long waitMicros =
      storedPermitsToWaitTime(this.storedPermits, storedPermitsToSpend)
+ (long) (freshPermits * stableIntervalMicros);
    this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
    this.storedPermits -= storedPermitsToSpend;
    return returnValue;
  }
 
Example 6
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
  final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
    resync(nowMicros);
    long returnValue = nextFreeTicketMicros;
    double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
    double freshPermits = requiredPermits - storedPermitsToSpend;
    long waitMicros =
      storedPermitsToWaitTime(this.storedPermits, storedPermitsToSpend)
+ (long) (freshPermits * stableIntervalMicros);
    this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
    this.storedPermits -= storedPermitsToSpend;
    return returnValue;
  }
 
Example 7
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
  resync(nowMicros);
  long returnValue = nextFreeTicketMicros;
  double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
  double freshPermits = requiredPermits - storedPermitsToSpend;
  long waitMicros =
    storedPermitsToWaitTime(this.storedPermits, storedPermitsToSpend)
    + (long) (freshPermits * stableIntervalMicros);
  this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
  this.storedPermits -= storedPermitsToSpend;
  return returnValue;
}
 
Example 8
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
  resync(nowMicros);
  long returnValue = nextFreeTicketMicros;
  double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
  double freshPermits = requiredPermits - storedPermitsToSpend;
  long waitMicros =
    storedPermitsToWaitTime(this.storedPermits, storedPermitsToSpend)
    + (long) (freshPermits * stableIntervalMicros);
  this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
  this.storedPermits -= storedPermitsToSpend;
  return returnValue;
}
 
Example 9
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
  resync(nowMicros);
  long returnValue = nextFreeTicketMicros;
  double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
  double freshPermits = requiredPermits - storedPermitsToSpend;
  long waitMicros =
      storedPermitsToWaitTime(this.storedPermits, storedPermitsToSpend)
          + (long) (freshPermits * stableIntervalMicros);

  this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
  this.storedPermits -= storedPermitsToSpend;
  return returnValue;
}
 
Example 10
Source File: DefaultTimeoutController.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Note that the {@link TimeoutTask} should be set via the {@link #setTimeoutTask(TimeoutTask)} or
 * the {@link #DefaultTimeoutController(TimeoutTask, EventExecutor)} before calling this method.
 *
 * @return {@code true} if the current timeout is extended by the specified {@code adjustmentMillis}.
 *         {@code false} if no timeout was scheduled previously, the timeout has been triggered already
 *         or the {@link TimeoutTask#canSchedule()} returned {@code false}.
 */
@Override
public boolean extendTimeout(long adjustmentMillis) {
    ensureInitialized();
    if (state != State.SCHEDULED || !timeoutTask.canSchedule()) {
        return false;
    }

    if (adjustmentMillis == 0) {
        return true;
    }

    // Cancel the previously scheduled timeout, if exists.
    cancelTimeout();

    // Calculate the amount of time passed since lastStart
    final long currentNanoTime = System.nanoTime();
    final long passedTimeMillis = TimeUnit.NANOSECONDS.toMillis(currentNanoTime - lastExecutionTimeNanos);
    final long newTimeoutMillis = LongMath.saturatedAdd(
            LongMath.saturatedSubtract(timeoutMillis, passedTimeMillis), adjustmentMillis);
    timeoutMillis = newTimeoutMillis;
    lastExecutionTimeNanos = currentNanoTime;

    if (newTimeoutMillis <= 0) {
        invokeTimeoutTask();
        return true;
    }

    state = State.SCHEDULED;
    timeoutFuture = executor.schedule(this::invokeTimeoutTask, newTimeoutMillis, TimeUnit.MILLISECONDS);
    return true;
}
 
Example 11
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenSaturatedAddTwoLongValues_shouldAddThemAndReturnTheResult() {
    long result = LongMath.saturatedAdd(6L, 4L);
    assertEquals(10L, result);
}
 
Example 12
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenSaturatedAddTwoLongValues_shouldAddThemAndReturnIntMaxIfOverflow() {
    long result = LongMath.saturatedAdd(Long.MAX_VALUE, 1000L);
    assertEquals(Long.MAX_VALUE, result);
}
 
Example 13
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenSaturatedAddTwoLongValues_shouldAddThemAndReturnIntMinIfUnderflow() {
    long result = LongMath.saturatedAdd(Long.MIN_VALUE, -1000);
    assertEquals(Long.MIN_VALUE, result);
}