/** * Get the current number of stored tokens. Note this is a snapshot of the object, and there is no guarantee that those * tokens will be available at any point in the future. */ public long getStoredTokens() { synchronized (this) { updateTokensStored(System.currentTimeMillis()); } return (long) this.tokensStored; }
public void resetQPS(long qps, long maxBucketSizeInMillis) { Preconditions.checkArgument(qps > 0, "QPS must be positive."); Preconditions.checkArgument(maxBucketSizeInMillis >= 0, "Max bucket size must be non-negative."); long now = System.currentTimeMillis(); synchronized (this) { updateTokensStored(now); if (this.nextTokenAvailableMillis > now) { this.tokensStored -= (this.nextTokenAvailableMillis - now) * this.tokensPerMilli; } this.tokensPerMilli = (double) qps / 1000; this.maxBucketSizeInTokens = this.tokensPerMilli * maxBucketSizeInMillis; } }
/** * Note: this method should only be called while holding the class lock. For performance, the lock is not explicitly * acquired. * * @return the wait until the tokens are available or negative if they can't be acquired in the give timeout. */ private long tryReserveTokens(long tokens, long maxWaitMillis) { long now = System.currentTimeMillis(); long waitUntilNextTokenAvailable = Math.max(0, this.nextTokenAvailableMillis - now); updateTokensStored(now); if (tokens <= this.tokensStored) { this.tokensStored -= tokens; return waitUntilNextTokenAvailable; } double additionalNeededTokens = tokens - this.tokensStored; // casting to long will round towards 0 long additionalWaitForEnoughTokens = (long) (additionalNeededTokens / this.tokensPerMilli) + 1; long totalWait = waitUntilNextTokenAvailable + additionalWaitForEnoughTokens; if (totalWait > maxWaitMillis) { return -1; } this.tokensStored = this.tokensPerMilli * additionalWaitForEnoughTokens - additionalNeededTokens; this.nextTokenAvailableMillis = this.nextTokenAvailableMillis + additionalWaitForEnoughTokens; return totalWait; }
/** * Get the current number of stored tokens. Note this is a snapshot of the object, and there is no guarantee that those * tokens will be available at any point in the future. */ public long getStoredTokens() { synchronized (this) { updateTokensStored(System.currentTimeMillis()); } return (long) this.tokensStored; }
public void resetQPS(long qps, long maxBucketSizeInMillis) { Preconditions.checkArgument(qps > 0, "QPS must be positive."); Preconditions.checkArgument(maxBucketSizeInMillis >= 0, "Max bucket size must be non-negative."); long now = System.currentTimeMillis(); synchronized (this) { updateTokensStored(now); if (this.nextTokenAvailableMillis > now) { this.tokensStored -= (this.nextTokenAvailableMillis - now) * this.tokensPerMilli; } this.tokensPerMilli = (double) qps / 1000; this.maxBucketSizeInTokens = this.tokensPerMilli * maxBucketSizeInMillis; } }
/** * Note: this method should only be called while holding the class lock. For performance, the lock is not explicitly * acquired. * * @return the wait until the tokens are available or negative if they can't be acquired in the give timeout. */ private long tryReserveTokens(long tokens, long maxWaitMillis) { long now = System.currentTimeMillis(); long waitUntilNextTokenAvailable = Math.max(0, this.nextTokenAvailableMillis - now); updateTokensStored(now); if (tokens <= this.tokensStored) { this.tokensStored -= tokens; return waitUntilNextTokenAvailable; } double additionalNeededTokens = tokens - this.tokensStored; // casting to long will round towards 0 long additionalWaitForEnoughTokens = (long) (additionalNeededTokens / this.tokensPerMilli) + 1; long totalWait = waitUntilNextTokenAvailable + additionalWaitForEnoughTokens; if (totalWait > maxWaitMillis) { return -1; } this.tokensStored = this.tokensPerMilli * additionalWaitForEnoughTokens - additionalNeededTokens; this.nextTokenAvailableMillis = this.nextTokenAvailableMillis + additionalWaitForEnoughTokens; return totalWait; }