public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
private static int checkPermits(int permits) { Assert.isTrue(permits > 0, StringUtils.format("Requested permits ({}) must be positive", permits)); return permits; } }
private static int checkPermits(int permits) { Assert.isTrue(permits > 0, StringUtils.format("Requested permits ({}) must be positive", permits)); return permits; } }
/** * Starts the stopwatch. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already running. */ public Stopwatch start() { Assert.isTrue(!isRunning, "This stopwatch is already running."); isRunning = true; startTick = ticker.read(); return this; }
/** * Starts the stopwatch. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already running. */ public Stopwatch start() { Assert.isTrue(!isRunning, "This stopwatch is already running."); isRunning = true; startTick = ticker.read(); return this; }
/** * Stops the stopwatch. Future reads will return the fixed duration that had * elapsed up to this point. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already stopped. */ public Stopwatch stop() { long tick = ticker.read(); Assert.isTrue(isRunning, "This stopwatch is already stopped."); isRunning = false; elapsedNanos += tick - startTick; return this; }
/** * Stops the stopwatch. Future reads will return the fixed duration that had * elapsed up to this point. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already stopped. */ public Stopwatch stop() { long tick = ticker.read(); Assert.isTrue(isRunning, "This stopwatch is already stopped."); isRunning = false; elapsedNanos += tick - startTick; return this; }
public void checkProperties() throws ConfigPropertiesIllegalException { Assert.isTrue(getMaxRetryTimes() >= 0, "maxRetryTimes must >= 0."); } }
public void checkProperties() throws ConfigPropertiesIllegalException { Assert.isTrue(getMaxRetryTimes() >= 0, "maxRetryTimes must >= 0."); } }
@Override public final void appendTo(final StringBuffer buffer, final int value) { if (value < 100) { for (int i = mSize; --i >= 2; ) { buffer.append('0'); } buffer.append((char)(value / 10 + '0')); buffer.append((char)(value % 10 + '0')); } else { int digits; if (value < 1000) { digits = 3; } else { Assert.isTrue(value > -1, "Negative values should not be possible"); digits = Integer.toString(value).length(); } for (int i = mSize; --i >= digits; ) { buffer.append('0'); } buffer.append(Integer.toString(value)); } } }
@Override public final void appendTo(final StringBuffer buffer, final int value) { if (value < 100) { for (int i = mSize; --i >= 2; ) { buffer.append('0'); } buffer.append((char)(value / 10 + '0')); buffer.append((char)(value % 10 + '0')); } else { int digits; if (value < 1000) { digits = 3; } else { Assert.isTrue(value > -1, "Negative values should not be possible"); digits = Integer.toString(value).length(); } for (int i = mSize; --i >= digits; ) { buffer.append('0'); } buffer.append(Integer.toString(value)); } } }
/** * Updates the stable rate of this {@code RateLimiter}, that is, the * {@code permitsPerSecond} argument provided in the factory method that * constructed the {@code RateLimiter}. Currently throttled threads will <b>not</b> * be awakened as a result of this invocation, thus they do not observe the new rate; * only subsequent requests will. * <p/> * <p>Note though that, since each request repays (by waiting, if necessary) the cost * of the <i>previous</i> request, this means that the very next request * after an invocation to {@code setRate} will not be affected by the new rate; * it will pay the cost of the previous request, which is in terms of the previous rate. * <p/> * <p>The behavior of the {@code RateLimiter} is not modified in any other way, * e.g. if the {@code RateLimiter} was configured with a warmup period of 20 seconds, * it still has a warmup period of 20 seconds after this method invocation. * * @param permitsPerSecond the new stable rate of this {@code RateLimiter} * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero */ public final void setRate(double permitsPerSecond) { Assert.isTrue( permitsPerSecond > 0.0 && !Double.isNaN(permitsPerSecond), "rate must be positive"); synchronized (mutex()) { doSetRate(permitsPerSecond, stopwatch.readMicros()); } }
/** * Updates the stable rate of this {@code RateLimiter}, that is, the * {@code permitsPerSecond} argument provided in the factory method that * constructed the {@code RateLimiter}. Currently throttled threads will <b>not</b> * be awakened as a result of this invocation, thus they do not observe the new rate; * only subsequent requests will. * <p/> * <p>Note though that, since each request repays (by waiting, if necessary) the cost * of the <i>previous</i> request, this means that the very next request * after an invocation to {@code setRate} will not be affected by the new rate; * it will pay the cost of the previous request, which is in terms of the previous rate. * <p/> * <p>The behavior of the {@code RateLimiter} is not modified in any other way, * e.g. if the {@code RateLimiter} was configured with a warmup period of 20 seconds, * it still has a warmup period of 20 seconds after this method invocation. * * @param permitsPerSecond the new stable rate of this {@code RateLimiter} * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero */ public final void setRate(double permitsPerSecond) { Assert.isTrue( permitsPerSecond > 0.0 && !Double.isNaN(permitsPerSecond), "rate must be positive"); synchronized (mutex()) { doSetRate(permitsPerSecond, stopwatch.readMicros()); } }
/** * Creates a {@code RateLimiter} with the specified stable throughput, given as * "permits per second" (commonly referred to as <i>QPS</i>, queries per second), and a * <i>warmup period</i>, during which the {@code RateLimiter} smoothly ramps up its rate, * until it reaches its maximum rate at the end of the period (as long as there are enough * requests to saturate it). Similarly, if the {@code RateLimiter} is left <i>unused</i> for * a duration of {@code warmupPeriod}, it will gradually return to its "cold" state, * i.e. it will go through the same warming up process as when it was first created. * <p/> * <p>The returned {@code RateLimiter} is intended for cases where the resource that actually * fulfills the requests (e.g., a remote server) needs "warmup" time, rather than * being immediately accessed at the stable (maximum) rate. * <p/> * <p>The returned {@code RateLimiter} starts in a "cold" state (i.e. the warmup period * will follow), and if it is left unused for long enough, it will return to that state. * * @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in * how many permits become available per second * @param warmupPeriod the duration of the period where the {@code RateLimiter} ramps up its * rate, before reaching its stable (maximum) rate * @param unit the time unit of the warmupPeriod argument * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero or * {@code warmupPeriod} is negative */ public static RateLimiter create(double permitsPerSecond, long warmupPeriod, TimeUnit unit) { Assert.isTrue(warmupPeriod >= 0, com.github.ltsopensource.core.commons.utils.StringUtils.format("warmupPeriod must not be negative: {}", warmupPeriod)); return create(SleepingStopwatch.createFromSystemTimer(), permitsPerSecond, warmupPeriod, unit, 3.0); }
/** * Creates a {@code RateLimiter} with the specified stable throughput, given as * "permits per second" (commonly referred to as <i>QPS</i>, queries per second), and a * <i>warmup period</i>, during which the {@code RateLimiter} smoothly ramps up its rate, * until it reaches its maximum rate at the end of the period (as long as there are enough * requests to saturate it). Similarly, if the {@code RateLimiter} is left <i>unused</i> for * a duration of {@code warmupPeriod}, it will gradually return to its "cold" state, * i.e. it will go through the same warming up process as when it was first created. * <p/> * <p>The returned {@code RateLimiter} is intended for cases where the resource that actually * fulfills the requests (e.g., a remote server) needs "warmup" time, rather than * being immediately accessed at the stable (maximum) rate. * <p/> * <p>The returned {@code RateLimiter} starts in a "cold" state (i.e. the warmup period * will follow), and if it is left unused for long enough, it will return to that state. * * @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in * how many permits become available per second * @param warmupPeriod the duration of the period where the {@code RateLimiter} ramps up its * rate, before reaching its stable (maximum) rate * @param unit the time unit of the warmupPeriod argument * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero or * {@code warmupPeriod} is negative */ public static RateLimiter create(double permitsPerSecond, long warmupPeriod, TimeUnit unit) { Assert.isTrue(warmupPeriod >= 0, com.github.ltsopensource.core.commons.utils.StringUtils.format("warmupPeriod must not be negative: {}", warmupPeriod)); return create(SleepingStopwatch.createFromSystemTimer(), permitsPerSecond, warmupPeriod, unit, 3.0); }
@Override public void checkProperties() throws ConfigPropertiesIllegalException { Assert.hasText(getClusterName(), "clusterName must have value."); Assert.hasText(getNodeGroup(), "nodeGroup must have value."); Assert.hasText(getRegistryAddress(), "registryAddress must have value."); Assert.isTrue(getWorkThreads() >= 0, "workThreads must >= 0."); }
@Override public void checkProperties() throws ConfigPropertiesIllegalException { Assert.hasText(getClusterName(), "clusterName must have value."); Assert.hasText(getNodeGroup(), "nodeGroup must have value."); Assert.hasText(getRegistryAddress(), "registryAddress must have value."); Assert.isTrue(getWorkThreads() >= 0, "workThreads must >= 0."); }
/** * Starts the stopwatch. * * @return this {@code Stopwatch} instance * @throws IllegalStateException if the stopwatch is already running. */ public Stopwatch start() { Assert.isTrue(!isRunning, "This stopwatch is already running."); isRunning = true; startTick = ticker.read(); return this; }
@Override public void checkProperties() throws ConfigPropertiesIllegalException { Assert.hasText(getClusterName(), "clusterName must have value."); Assert.hasText(getNodeGroup(), "nodeGroup must have value."); Assert.hasText(getRegistryAddress(), "registryAddress must have value."); Assert.isTrue(getWorkThreads() >= 0, "workThreads must >= 0."); }