View Javadoc
1   package org.oxerr.rescu.ext.ratelimiter.redisson;
2   
3   import org.oxerr.rescu.ext.ratelimiter.RateLimiter;
4   import org.redisson.api.RRateLimiter;
5   import org.redisson.api.RateIntervalUnit;
6   import org.redisson.api.RateType;
7   import org.redisson.api.RedissonClient;
8   
9   public class RedissonRateLimiter implements RateLimiter {
10  
11  	private final RRateLimiter rateLimiter;
12  
13  	public RedissonRateLimiter(RRateLimiter rateLimiter) {
14  		this.rateLimiter = rateLimiter;
15  	}
16  
17  	public RedissonRateLimiter(RedissonClient redisson, String name) {
18  		this.rateLimiter = redisson.getRateLimiter(name);
19  	}
20  
21  	/**
22  	 * Updates RateLimiter's state and stores config to Redis server.
23  	 *
24  	 * @param mode             - rate mode
25  	 * @param rate             - rate
26  	 * @param rateInterval     - rate time interval
27  	 * @param rateIntervalUnit - rate time interval unit
28  	 */
29  	public void setRate(RateType mode, long rate, long rateInterval, RateIntervalUnit rateIntervalUnit) {
30  		this.rateLimiter.setRate(mode, rate, rateInterval, rateIntervalUnit);
31  	}
32  
33  	@Override
34  	public void acquire() {
35  		this.rateLimiter.acquire();
36  	}
37  
38  	public RRateLimiter getRateLimiter() {
39  		return this.rateLimiter;
40  	}
41  
42  }