car/src/main/java/androidx/car/moderator/ContentRateLimiter.java
@MainThread
    public boolean tryAcquire(int permits) {
        // Once unlimited mode is enabled, we can acquire any number of permits we want and don't
        // consume the stored permits.
        if (mUnlimitedModeEnabled) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Unlimited mode is enabled.");
            }
            return true;
        }
        double availablePermits = getLastCalculatedPermitCount();
        long nowMs = mElapsedTimeProvider.getElapsedRealtime();

        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, String.format("Requesting: %d, Stored: %f/%f", permits,
                    mLastCalculatedPermitCount, mMaxStoredPermits));
        }
        if (availablePermits <= permits) {
            // Once locked out, the user is prevented from acquiring any more permits until they
            // have waited long enough for a permit to refill. If the user attempts to acquire a
            // permit during this time, the countdown timer until a permit is refilled is reset.
            setLastCalculatedPermitCount(0, nowMs + mFillDelayMs);
            return false;
        } else if (nowMs < mResumeIncrementingMs && mSecondaryFillDelayPermitAvailable) {
            // If a second permit is requested between the time a first permit was requested and
            // the fill delay, allow the second permit to be acquired without decrementing the model
            // and set the point where permits can resume incrementing {@link #mFillDelayMs} in the
            // future.
            setLastCalculatedPermitCount(availablePermits, nowMs + mFillDelayMs);
            // Don't allow a third "free" permit to be acquired in the fill delay fringe.
            mSecondaryFillDelayPermitAvailable = false;

            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Used up free secondary permit");
            }
            return true;
        } else {
            // Decrement the available permits, and set the point where permits can resume
            // incrementing {@link #mFillDelayMs} in the future.
            setLastCalculatedPermitCount(availablePermits - permits, nowMs + mFillDelayMs);

            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, String.format("permits remaining %s, secondary permit available %s",
                        mLastCalculatedPermitCount,
                        mSecondaryFillDelayPermitAvailable));
            }

            mSecondaryFillDelayPermitAvailable = true;
            return true;
        }
    }
/**
     * Checks whether there are enough available permits to acquire.
     *
     * @return {@code true} if unlimited mode is enabled or enough permits are acquirable at the
     * time of this call; {@code false} if there isn't the number of permits requested available
     * currently.
     */
Checks whether there are enough available permits to acquire.
@return  true if unlimited mode is enabled or enough permits are acquirable at the
time of this call;  false if there isn't the number of permits requested available
currently.
Checks whether there are enough available permits to acquire . @return true if unlimited mode is enabled or enough permits are acquirable at the time of this call ; false if there isn ' t the number of permits requested available currently .
@MainThread
    public boolean tryAcquire(int permits) {
        if (mUnlimitedModeEnabled) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Unlimited mode is enabled.");
            }
            return true;
        }
        double availablePermits = getLastCalculatedPermitCount();
        long nowMs = mElapsedTimeProvider.getElapsedRealtime();
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, String.format("Requesting: %d, Stored: %f/%f", permits,
                    mLastCalculatedPermitCount, mMaxStoredPermits));
        }
        if (availablePermits <= permits) {
            setLastCalculatedPermitCount(0, nowMs + mFillDelayMs);
            return false;
        } else if (nowMs < mResumeIncrementingMs && mSecondaryFillDelayPermitAvailable) {
            setLastCalculatedPermitCount(availablePermits, nowMs + mFillDelayMs);
            mSecondaryFillDelayPermitAvailable = false;
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Used up free secondary permit");
            }
            return true;
        } else {
            setLastCalculatedPermitCount(availablePermits - permits, nowMs + mFillDelayMs);
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, String.format("permits remaining %s, secondary permit available %s",
                        mLastCalculatedPermitCount,
                        mSecondaryFillDelayPermitAvailable));
            }
            mSecondaryFillDelayPermitAvailable = true;
            return true;
        }
    }
@ MainThread public boolean tryAcquire ( int permits ) { if ( mUnlimitedModeEnabled ) { if ( Log . isLoggable ( TAG , Log . DEBUG ) ) { Log . d ( TAG , "Unlimited mode is enabled." ) ; } return true ; } double availablePermits = getLastCalculatedPermitCount ( ) ; long nowMs = mElapsedTimeProvider . getElapsedRealtime ( ) ; if ( Log . isLoggable ( TAG , Log . VERBOSE ) ) { Log . v ( TAG , String . format ( "Requesting: %d, Stored: %f/%f" , permits , mLastCalculatedPermitCount , mMaxStoredPermits ) ) ; } if ( availablePermits <= permits ) { setLastCalculatedPermitCount ( 0 , nowMs + mFillDelayMs ) ; return false ; } else if ( nowMs < mResumeIncrementingMs && mSecondaryFillDelayPermitAvailable ) { setLastCalculatedPermitCount ( availablePermits , nowMs + mFillDelayMs ) ; mSecondaryFillDelayPermitAvailable = false ; if ( Log . isLoggable ( TAG , Log . DEBUG ) ) { Log . d ( TAG , "Used up free secondary permit" ) ; } return true ; } else { setLastCalculatedPermitCount ( availablePermits - permits , nowMs + mFillDelayMs ) ; if ( Log . isLoggable ( TAG , Log . VERBOSE ) ) { Log . v ( TAG , String . format ( "permits remaining %s, secondary permit available %s" , mLastCalculatedPermitCount , mSecondaryFillDelayPermitAvailable ) ) ; } mSecondaryFillDelayPermitAvailable = true ; return true ; } }
Checks whether there are enough available permits to acquire.
Checks whether there are enough available permits to acquire .
// Once unlimited mode is enabled, we can acquire any number of permits we want and don't // consume the stored permits. // Once locked out, the user is prevented from acquiring any more permits until they // have waited long enough for a permit to refill. If the user attempts to acquire a // permit during this time, the countdown timer until a permit is refilled is reset. // If a second permit is requested between the time a first permit was requested and // the fill delay, allow the second permit to be acquired without decrementing the model // and set the point where permits can resume incrementing {@link #mFillDelayMs} in the // future. // Don't allow a third "free" permit to be acquired in the fill delay fringe. // Decrement the available permits, and set the point where permits can resume // incrementing {@link #mFillDelayMs} in the future.