Search in sources :

Example 1 with RateLimiter

use of org.apache.pulsar.common.util.RateLimiter in project incubator-pulsar by apache.

the class DispatchRateLimiter method updateDispatchRate.

/**
 * Update dispatch rate by updating msg and byte rate-limiter. If dispatch-rate is configured < 0 then it closes
 * the rate-limiter and disables appropriate rate-limiter.
 *
 * @param dispatchRate
 */
public synchronized void updateDispatchRate(DispatchRate dispatchRate) {
    // synchronized to prevent race condition from concurrent zk-watch
    log.info("[{}] setting message-dispatch-rate {}", topicName, dispatchRate);
    long msgRate = dispatchRate.dispatchThrottlingRatePerTopicInMsg;
    long byteRate = dispatchRate.dispatchThrottlingRatePerTopicInByte;
    long ratePerid = dispatchRate.ratePeriodInSecond;
    // update msg-rateLimiter
    if (msgRate > 0) {
        if (this.dispatchRateLimiterOnMessage == null) {
            this.dispatchRateLimiterOnMessage = new RateLimiter(brokerService.pulsar().getExecutor(), msgRate, ratePerid, TimeUnit.SECONDS);
        } else {
            this.dispatchRateLimiterOnMessage.setRate(msgRate, dispatchRate.ratePeriodInSecond, TimeUnit.SECONDS);
        }
    } else {
        // message-rate should be disable and close
        if (this.dispatchRateLimiterOnMessage != null) {
            this.dispatchRateLimiterOnMessage.close();
            this.dispatchRateLimiterOnMessage = null;
        }
    }
    // update byte-rateLimiter
    if (byteRate > 0) {
        if (this.dispatchRateLimiterOnByte == null) {
            this.dispatchRateLimiterOnByte = new RateLimiter(brokerService.pulsar().getExecutor(), byteRate, ratePerid, TimeUnit.SECONDS);
        } else {
            this.dispatchRateLimiterOnByte.setRate(byteRate, dispatchRate.ratePeriodInSecond, TimeUnit.SECONDS);
        }
    } else {
        // message-rate should be disable and close
        if (this.dispatchRateLimiterOnByte != null) {
            this.dispatchRateLimiterOnByte.close();
            this.dispatchRateLimiterOnByte = null;
        }
    }
}
Also used : RateLimiter(org.apache.pulsar.common.util.RateLimiter)

Aggregations

RateLimiter (org.apache.pulsar.common.util.RateLimiter)1