/** * Acquires permits from this {@link RateLimiter} if it can be acquired immediately without delay. * * <p> * This method is equivalent to {@code tryAcquire(1)}. * * @param permits * the number of permits to acquire * @return {@code true} if the permits were acquired, {@code false} otherwise */ public synchronized boolean tryAcquire() { return tryAcquire(1); }
/** * Acquires the given number of permits from this {@code RateLimiter}, blocking until the request be granted. * * This method is equivalent to {@code acquire(1)}. * * @param permits * the number of permits to acquire */ public synchronized void acquire() throws InterruptedException { acquire(1); }
/** * 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; }
this.dispatchRateLimiterOnMessage = new RateLimiter(brokerService.pulsar().getExecutor(), msgRate, ratePeriod, TimeUnit.SECONDS); } else { this.dispatchRateLimiterOnMessage.setRate(msgRate, dispatchRate.ratePeriodInSecond, TimeUnit.SECONDS); this.dispatchRateLimiterOnMessage.close(); this.dispatchRateLimiterOnMessage = null; this.dispatchRateLimiterOnByte = new RateLimiter(brokerService.pulsar().getExecutor(), byteRate, ratePeriod, TimeUnit.SECONDS); } else { this.dispatchRateLimiterOnByte.setRate(byteRate, dispatchRate.ratePeriodInSecond, TimeUnit.SECONDS); this.dispatchRateLimiterOnByte.close(); this.dispatchRateLimiterOnByte = null;
@Override public void run() { renew(); } }, this.rateTime, this.rateTime, this.timeUnit);
/** * Resets new rate with new permits and rate-time. * * @param permits * @param rateTime * @param timeUnit */ public synchronized void setRate(long permits, long rateTime, TimeUnit timeUnit) { if(renewTask != null) { renewTask.cancel(false); } this.permits = permits; this.rateTime = rateTime; this.timeUnit = timeUnit; this.renewTask = createTask(); }
/** * Get configured byte dispatch-throttling rate. Returns -1 if not configured * * @return */ public long getDispatchRateOnByte() { return dispatchRateLimiterOnByte != null ? dispatchRateLimiterOnByte.getRate() : -1; }
/** * returns available msg-permit if msg-dispatch-throttling is enabled else it returns -1 * * @return */ public long getAvailableDispatchRateLimitOnMsg() { return dispatchRateLimiterOnMessage == null ? -1 : dispatchRateLimiterOnMessage.getAvailablePermits(); }
@Override public void run() { renew(); } }, this.rateTime, this.rateTime, this.timeUnit);
/** * Resets new rate with new permits and rate-time. * * @param permits * @param rateTime * @param timeUnit */ public synchronized void setRate(long permits, long rateTime, TimeUnit timeUnit) { if(renewTask != null) { renewTask.cancel(false); } this.permits = permits; this.rateTime = rateTime; this.timeUnit = timeUnit; this.renewTask = createTask(); }
/** * Get configured msg dispatch-throttling rate. Returns -1 if not configured * * @return */ public long getDispatchRateOnMsg() { return dispatchRateLimiterOnMessage != null ? dispatchRateLimiterOnMessage.getRate() : -1; }
/** * checks if dispatch-rate limit is configured and if it's configured then check if permits are available or not. * * @return */ public boolean hasMessageDispatchPermit() { return (dispatchRateLimiterOnMessage == null || dispatchRateLimiterOnMessage.getAvailablePermits() > 0) && (dispatchRateLimiterOnByte == null || dispatchRateLimiterOnByte.getAvailablePermits() > 0); }
/** * 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. * * <p> * This method is equivalent to {@code tryAcquire(1)}. * * @param permits * the number of permits to acquire * @return {@code true} if the permits were acquired, {@code false} otherwise */ public synchronized boolean tryAcquire() { return tryAcquire(1); }
/** * Acquires the given number of permits from this {@code RateLimiter}, blocking until the request be granted. * * This method is equivalent to {@code acquire(1)}. * * @param permits * the number of permits to acquire */ public synchronized void acquire() throws InterruptedException { acquire(1); }
/** * 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; }
/** * It acquires msg and bytes permits from rate-limiter and returns if acquired permits succeed. * * @param msgPermits * @param bytePermits * @return */ public boolean tryDispatchPermit(long msgPermits, long bytePermits) { boolean acquiredMsgPermit = msgPermits <= 0 || dispatchRateLimiterOnMessage == null // acquiring permits must be < configured msg-rate; || dispatchRateLimiterOnMessage.tryAcquire(msgPermits); boolean acquiredBytePermit = bytePermits <= 0 || dispatchRateLimiterOnByte == null // acquiring permits must be < configured msg-rate; || dispatchRateLimiterOnByte.tryAcquire(bytePermits); return acquiredMsgPermit && acquiredBytePermit; }
/** * 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); }