maedhroz commented on a change in pull request #1045: URL: https://github.com/apache/cassandra/pull/1045#discussion_r651986078
########## File path: src/java/org/apache/cassandra/utils/NoWaitRateLimiter.java ########## @@ -0,0 +1,261 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.utils; + +import java.util.concurrent.TimeUnit; + +import javax.annotation.concurrent.GuardedBy; +import javax.annotation.concurrent.NotThreadSafe; +import javax.annotation.concurrent.ThreadSafe; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.base.Stopwatch; +import com.google.common.base.Ticker; +import com.google.common.math.LongMath; + +@ThreadSafe +@SuppressWarnings("UnstableApiUsage") +public class NoWaitRateLimiter +{ + @GuardedBy("this") + private Stopwatch stopwatch; + + @GuardedBy("this") + private SmoothRateLimiter delegate; + + private NoWaitRateLimiter(SmoothRateLimiter delegate, Ticker ticker) + { + this.delegate = delegate; + this.stopwatch = Stopwatch.createStarted(ticker); + } + + public static NoWaitRateLimiter create(double permitsPerSecond) + { + return create(permitsPerSecond, Ticker.systemTicker()); + } + + public static NoWaitRateLimiter create(double permitsPerSecond, Ticker ticker) + { + return create(permitsPerSecond, 1.0D, ticker); + } + + public static NoWaitRateLimiter create(double permitsPerSecond, double burstSeconds) + { + return create(permitsPerSecond, burstSeconds, Ticker.systemTicker()); + } + + public static NoWaitRateLimiter create(double permitsPerSecond, double burstSeconds, Ticker ticker) + { + SmoothRateLimiter delegate = new SmoothRateLimiter(burstSeconds); + NoWaitRateLimiter limiter = new NoWaitRateLimiter(delegate, ticker); + limiter.setRate(permitsPerSecond); + return limiter; + } + + @VisibleForTesting + public synchronized void reset(double permitsPerSecond, double burstSeconds, Ticker ticker) + { + this.stopwatch = Stopwatch.createStarted(ticker); + this.delegate = new SmoothRateLimiter(burstSeconds); + setRate(permitsPerSecond); + } + + public final void setRate(double permitsPerSecond) + { + Preconditions.checkArgument(permitsPerSecond > 0.0D && !Double.isNaN(permitsPerSecond), "rate must be positive"); + + synchronized (this) + { + delegate.doSetRate(permitsPerSecond, stopwatch.elapsed(TimeUnit.MICROSECONDS)); + } + } + + public synchronized double getRate() + { + return delegate.getRate(); + } + + /** + * Forces the acquisition of a single permit. + * + * @return microseconds until the acquired permit is available, and zero if it already is + */ + public synchronized long reserveAndGetWaitLength() + { + long nowMicros = this.stopwatch.elapsed(TimeUnit.MICROSECONDS); + long momentAvailable = delegate.reserveEarliestAvailable(1, nowMicros); + return Math.max(momentAvailable - nowMicros, 0L); + } + + public synchronized boolean tryAcquire() + { + long nowMicros = this.stopwatch.elapsed(TimeUnit.MICROSECONDS); + + if (!delegate.canAcquire(nowMicros)) + { + return false; + } + + delegate.reserveEarliestAvailable(1, nowMicros); + + return true; + } + + public synchronized boolean canAcquire() + { + long nowMicros = this.stopwatch.elapsed(TimeUnit.MICROSECONDS); + return delegate.canAcquire(nowMicros); + } + + public synchronized long waitTimeMicros() Review comment: Alternate name: `timeUntilNextPermit()` (Needs JavaDoc either way.) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

