/** * Acquires the given number of permits from this {@code RateLimiter}, blocking until the request be granted. * * @param permits * the number of permits to acquire */ public synchronized void acquire(long acquirePermit) throws InterruptedException { checkArgument(!isClosed(), "Rate limiter is already shutdown"); checkArgument(acquirePermit <= this.permits, "acquiring permits must be less or equal than initialized rate =" + this.permits); // lazy init and start task only once application start using it if (renewTask == null) { renewTask = createTask(); } boolean canAcquire = false; do { canAcquire = acquirePermit < 0 || acquiredPermits < this.permits; if (!canAcquire) { wait(); } else { acquiredPermits += acquirePermit; } } while (!canAcquire); }
/** * Acquires permits from this {@link RateLimiter} if it can be acquired immediately without delay. * * @param permits * the number of permits to acquire * @return {@code true} if the permits were acquired, {@code false} otherwise */ public synchronized boolean tryAcquire(long acquirePermit) { checkArgument(!isClosed(), "Rate limiter is already shutdown"); // lazy init and start task only once application start using it if (renewTask == null) { renewTask = createTask(); } // acquired-permits can't be larger than the rate if (acquirePermit > this.permits) { acquiredPermits = this.permits; return false; } boolean canAcquire = acquirePermit < 0 || acquiredPermits < this.permits; if (canAcquire) { acquiredPermits += acquirePermit; } return canAcquire; }
/** * Acquires the given number of permits from this {@code RateLimiter}, blocking until the request be granted. * * @param permits * the number of permits to acquire */ public synchronized void acquire(long acquirePermit) throws InterruptedException { checkArgument(!isClosed(), "Rate limiter is already shutdown"); checkArgument(acquirePermit <= this.permits, "acquiring permits must be less or equal than initialized rate =" + this.permits); // lazy init and start task only once application start using it if (renewTask == null) { renewTask = createTask(); } boolean canAcquire = false; do { canAcquire = acquirePermit < 0 || acquiredPermits < this.permits; if (!canAcquire) { wait(); } else { acquiredPermits += acquirePermit; } } while (!canAcquire); }
/** * Acquires permits from this {@link RateLimiter} if it can be acquired immediately without delay. * * @param permits * the number of permits to acquire * @return {@code true} if the permits were acquired, {@code false} otherwise */ public synchronized boolean tryAcquire(long acquirePermit) { checkArgument(!isClosed(), "Rate limiter is already shutdown"); // lazy init and start task only once application start using it if (renewTask == null) { renewTask = createTask(); } // acquired-permits can't be larger than the rate if (acquirePermit > this.permits) { acquiredPermits = this.permits; return false; } boolean canAcquire = acquirePermit < 0 || acquiredPermits < this.permits; if (canAcquire) { acquiredPermits += acquirePermit; } return canAcquire; }