├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ └── love │ │ └── wangqi │ │ ├── JedisPoolProxy.java │ │ ├── RedisPool.java │ │ ├── ScriptUtil.java │ │ ├── counter │ │ └── CounterRateLimiter.java │ │ └── tokenbucket │ │ ├── RateLimiter.java │ │ ├── RateLimiterBuilder.java │ │ ├── SmoothBursty.java │ │ ├── SmoothBurstyImpl.java │ │ └── SmoothRateLimiter.java └── resourses │ ├── counter_limit.lua │ └── smooth_ratelimiter.lua └── test └── java └── love └── wangqi ├── CounterRateLimiterTest.java └── RateLimiterTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | .idea/ 25 | target/ 26 | *.iml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # redis-limiter 2 | 3 | 基于redis的分布式限流方案 4 | 5 | 完整说明参考:[https://blog.wangqi.love/articles/Java/基于redis的分布式限流方案.html](https://blog.wangqi.love/articles/Java/基于redis的分布式限流方案.html) -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | love.wangqi 8 | redis-limiter 9 | 1.0-SNAPSHOT 10 | 11 | redis-limiter 12 | 13 | http://www.example.com 14 | 15 | 16 | UTF-8 17 | 1.8 18 | 1.8 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.11 26 | test 27 | 28 | 29 | redis.clients 30 | jedis 31 | 2.9.0 32 | 33 | 34 | ch.qos.logback 35 | logback-classic 36 | 1.2.3 37 | 38 | 39 | com.google.guava 40 | guava 41 | 25.1-jre 42 | 43 | 44 | cglib 45 | cglib 46 | 3.2.5 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | maven-clean-plugin 55 | 3.0.0 56 | 57 | 58 | 59 | maven-resources-plugin 60 | 3.0.2 61 | 62 | 63 | maven-compiler-plugin 64 | 3.7.0 65 | 66 | 67 | maven-surefire-plugin 68 | 2.20.1 69 | 70 | 71 | maven-jar-plugin 72 | 3.0.2 73 | 74 | 75 | maven-install-plugin 76 | 2.5.2 77 | 78 | 79 | maven-deploy-plugin 80 | 2.8.2 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /src/main/java/love/wangqi/JedisPoolProxy.java: -------------------------------------------------------------------------------- 1 | package love.wangqi; 2 | 3 | import net.sf.cglib.proxy.MethodInterceptor; 4 | import net.sf.cglib.proxy.MethodProxy; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import redis.clients.jedis.Jedis; 8 | import redis.clients.jedis.JedisPool; 9 | 10 | import java.lang.reflect.Method; 11 | 12 | /** 13 | * @author: wangqi 14 | * @description: 15 | * @date: Created in 2018/9/3 上午10:57 16 | */ 17 | public class JedisPoolProxy implements MethodInterceptor { 18 | private JedisPool jedisPool; 19 | 20 | private static final Logger log = LoggerFactory.getLogger(JedisPoolProxy.class); 21 | 22 | public JedisPoolProxy(JedisPool jedisPool) { 23 | this.jedisPool = jedisPool; 24 | } 25 | 26 | @Override 27 | public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { 28 | try (Jedis jedis = jedisPool.getResource()) { 29 | return method.invoke(jedis, args); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/love/wangqi/RedisPool.java: -------------------------------------------------------------------------------- 1 | package love.wangqi; 2 | 3 | import net.sf.cglib.proxy.Enhancer; 4 | import redis.clients.jedis.Jedis; 5 | import redis.clients.jedis.JedisPool; 6 | import redis.clients.jedis.JedisPoolConfig; 7 | 8 | /** 9 | * @author: wangqi 10 | * @description: 11 | * @date: Created in 2018/8/2 上午10:05 12 | */ 13 | public abstract class RedisPool { 14 | private static JedisPool jedisPool = null; 15 | private static String host = "localhost"; 16 | private static int port = 6379; 17 | private static String password = null; 18 | 19 | static { 20 | try { 21 | JedisPoolConfig config = new JedisPoolConfig(); 22 | jedisPool = new JedisPool(config, host, port, 3000, password); 23 | } catch (Exception e) { 24 | e.printStackTrace(); 25 | } 26 | } 27 | 28 | public static Jedis getJedis() { 29 | JedisPoolProxy proxy = new JedisPoolProxy(jedisPool); 30 | Enhancer enhancer = new Enhancer(); 31 | enhancer.setSuperclass(Jedis.class); 32 | enhancer.setCallback(proxy); 33 | return (Jedis) enhancer.create(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/love/wangqi/ScriptUtil.java: -------------------------------------------------------------------------------- 1 | package love.wangqi; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | 8 | /** 9 | * @author: wangqi 10 | * @description: 11 | * @date: Created in 2018/8/1 下午7:45 12 | */ 13 | public class ScriptUtil { 14 | public static String getScript(String path) { 15 | StringBuilder sb = new StringBuilder(); 16 | 17 | InputStream stream = ScriptUtil.class.getClassLoader().getResourceAsStream(path); 18 | BufferedReader br = new BufferedReader(new InputStreamReader(stream)); 19 | try { 20 | 21 | String str = ""; 22 | while ((str = br.readLine()) != null) { 23 | sb.append(str).append(System.lineSeparator()); 24 | } 25 | 26 | } catch (IOException e) { 27 | System.err.println(e.getStackTrace()); 28 | } 29 | return sb.toString(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/love/wangqi/counter/CounterRateLimiter.java: -------------------------------------------------------------------------------- 1 | package love.wangqi.counter; 2 | 3 | import love.wangqi.RedisPool; 4 | import love.wangqi.ScriptUtil; 5 | import redis.clients.jedis.Jedis; 6 | 7 | import java.util.Arrays; 8 | 9 | /** 10 | * @author: wangqi 11 | * @description: 12 | * @date: Created in 2018/8/1 下午7:40 13 | */ 14 | public class CounterRateLimiter extends RedisPool { 15 | private String script; 16 | private String key; 17 | private int maxPermits; 18 | private Long intervalMilliseconds; 19 | 20 | public CounterRateLimiter(String key, int maxPermits, Long intervalMilliseconds) throws Exception { 21 | script = ScriptUtil.getScript("counter_limit.lua"); 22 | this.key = key; 23 | this.maxPermits = maxPermits; 24 | this.intervalMilliseconds = intervalMilliseconds; 25 | } 26 | 27 | public Boolean acquire(int permits) throws Exception { 28 | Jedis redis = getJedis(); 29 | Object result = redis.eval(script, 30 | Arrays.asList(key, String.valueOf(maxPermits), String.valueOf(intervalMilliseconds)), 31 | Arrays.asList(String.valueOf(permits)) 32 | ); 33 | if (result != null && 0 != (Long) result) { 34 | return true; 35 | } else { 36 | return false; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/love/wangqi/tokenbucket/RateLimiter.java: -------------------------------------------------------------------------------- 1 | package love.wangqi.tokenbucket; 2 | 3 | import static com.google.common.base.Preconditions.checkArgument; 4 | import static com.google.common.base.Preconditions.checkNotNull; 5 | import static java.lang.Math.max; 6 | import static java.util.concurrent.TimeUnit.MICROSECONDS; 7 | import static java.util.concurrent.TimeUnit.SECONDS; 8 | 9 | import com.google.common.base.Stopwatch; 10 | import com.google.common.util.concurrent.Uninterruptibles; 11 | import redis.clients.jedis.Jedis; 12 | 13 | import java.util.Locale; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | /** 17 | * @author: wangqi 18 | * @description: 19 | * @date: Created in 2018/8/2 上午7:43 20 | */ 21 | public abstract class RateLimiter { 22 | private final SleepingStopwatch stopwatch; 23 | 24 | private volatile Object mutexDoNotUseDirectly; 25 | 26 | private Object mutex() { 27 | Object mutex = mutexDoNotUseDirectly; 28 | if (mutex == null) { 29 | synchronized (this) { 30 | mutex = mutexDoNotUseDirectly; 31 | if (mutex == null) { 32 | mutexDoNotUseDirectly = mutex = new Object(); 33 | } 34 | } 35 | } 36 | return mutex; 37 | } 38 | 39 | RateLimiter(SleepingStopwatch stopwatch) { 40 | this.stopwatch = checkNotNull(stopwatch); 41 | } 42 | 43 | public final void setRate(double permitsPerSecond) { 44 | checkArgument(permitsPerSecond > 0.0 && !Double.isNaN(permitsPerSecond), "rate must be positive"); 45 | synchronized (mutex()) { 46 | doSetRate(permitsPerSecond); 47 | } 48 | } 49 | 50 | abstract void doSetRate(double permitsPerSecond); 51 | 52 | public final double getRate() { 53 | synchronized (mutex()) { 54 | return doGetRate(); 55 | } 56 | } 57 | 58 | abstract double doGetRate(); 59 | 60 | public double acquire() { 61 | return acquire(1); 62 | } 63 | 64 | public double acquire(int permits) { 65 | checkPermits(permits); 66 | long microToWait = waitMicros(permits); 67 | stopwatch.sleepMicrosUninterruptibly(microToWait); 68 | return 1.0 * microToWait / SECONDS.toMicros(1L); 69 | } 70 | 71 | public boolean tryAcquire(long timeout, TimeUnit unit) { 72 | return tryAcquire(1, timeout, unit); 73 | } 74 | 75 | public boolean tryAcquire(int permits) { 76 | return tryAcquire(permits, 0, MICROSECONDS); 77 | } 78 | 79 | public boolean tryAcquire() { 80 | return tryAcquire(1, 0, MICROSECONDS); 81 | } 82 | 83 | public boolean tryAcquire(int permits, long timeout, TimeUnit unit) { 84 | long timeoutMicros = max(unit.toMicros(timeout), 0); 85 | checkPermits(permits); 86 | long microsToWait = queryWaitMicros(permits, timeoutMicros); 87 | if (microsToWait > timeoutMicros) { 88 | return false; 89 | } 90 | stopwatch.sleepMicrosUninterruptibly(microsToWait); 91 | return true; 92 | } 93 | 94 | final long waitMicros(int permits) { 95 | long waitMicros = queryWaitMicros(permits, null); 96 | return max(waitMicros, 0); 97 | } 98 | 99 | abstract long queryWaitMicros(int permits, Long timeoutMicros); 100 | 101 | @Override 102 | public String toString() { 103 | return String.format(Locale.ROOT, "RateLimiter[stableRate=%3.1fqps]", getRate()); 104 | } 105 | 106 | abstract static class SleepingStopwatch { 107 | protected SleepingStopwatch() {} 108 | 109 | protected abstract long readMicros(); 110 | 111 | protected abstract void sleepMicrosUninterruptibly(long micros); 112 | 113 | public static SleepingStopwatch createFromSystemTimer() { 114 | return new SleepingStopwatch() { 115 | final Stopwatch stopwatch = Stopwatch.createStarted(); 116 | 117 | @Override 118 | protected long readMicros() { 119 | return stopwatch.elapsed(MICROSECONDS); 120 | } 121 | 122 | @Override 123 | protected void sleepMicrosUninterruptibly(long micros) { 124 | if (micros > 0) { 125 | Uninterruptibles.sleepUninterruptibly(micros, MICROSECONDS); 126 | } 127 | } 128 | }; 129 | } 130 | } 131 | 132 | private static void checkPermits(int permits) { 133 | checkArgument(permits > 0, "Requested permits (%s) must be positive", permits); 134 | } 135 | 136 | protected abstract Jedis getJedis(); 137 | } 138 | -------------------------------------------------------------------------------- /src/main/java/love/wangqi/tokenbucket/RateLimiterBuilder.java: -------------------------------------------------------------------------------- 1 | package love.wangqi.tokenbucket; 2 | 3 | import love.wangqi.RedisPool; 4 | import redis.clients.jedis.Jedis; 5 | 6 | /** 7 | * @author: wangqi 8 | * @description: 9 | * @date: Created in 2018/9/3 上午10:45 10 | */ 11 | public class RateLimiterBuilder { 12 | Jedis jedis; 13 | 14 | public RateLimiterBuilder setJedis(Jedis jedis) { 15 | this.jedis = jedis; 16 | return this; 17 | } 18 | 19 | public RateLimiter create(String key, double permitsPerSecond) { 20 | return create(key, permitsPerSecond, RateLimiter.SleepingStopwatch.createFromSystemTimer()); 21 | } 22 | 23 | RateLimiter create(String key, double permitsPerSecond, RateLimiter.SleepingStopwatch stopwatch) { 24 | RateLimiter rateLimiter = new SmoothBurstyImpl(jedis, key, stopwatch, 1.0); 25 | rateLimiter.setRate(permitsPerSecond); 26 | return rateLimiter; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/love/wangqi/tokenbucket/SmoothBursty.java: -------------------------------------------------------------------------------- 1 | package love.wangqi.tokenbucket; 2 | 3 | import love.wangqi.ScriptUtil; 4 | 5 | /** 6 | * @author: wangqi 7 | * @description: 8 | * @date: Created in 2018/9/3 上午10:40 9 | */ 10 | public abstract class SmoothBursty extends SmoothRateLimiter { 11 | final double maxBurstSeconds; 12 | 13 | SmoothBursty(String key, SleepingStopwatch stopwatch, double maxBurstSeconds) { 14 | super(stopwatch); 15 | this.script = ScriptUtil.getScript("smooth_ratelimiter.lua"); 16 | this.key = key; 17 | this.maxBurstSeconds = maxBurstSeconds; 18 | } 19 | 20 | @Override 21 | void doSetRate(double permitsPerSecond, double stableIntervalMicros) { 22 | double oldMaxPermits = this.maxPermits; 23 | this.permitsPerSecond = permitsPerSecond; 24 | maxPermits = maxBurstSeconds * permitsPerSecond; 25 | if (oldMaxPermits == Double.POSITIVE_INFINITY) { 26 | storedPermits = maxPermits; 27 | } else { 28 | storedPermits = 29 | (oldMaxPermits == 0.0) 30 | ? 0.0 : storedPermits * maxPermits / oldMaxPermits; 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/java/love/wangqi/tokenbucket/SmoothBurstyImpl.java: -------------------------------------------------------------------------------- 1 | package love.wangqi.tokenbucket; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | /** 6 | * @author: wangqi 7 | * @description: 8 | * @date: Created in 2018/9/3 上午10:42 9 | */ 10 | public class SmoothBurstyImpl extends SmoothBursty { 11 | private Jedis jedis; 12 | 13 | SmoothBurstyImpl(Jedis jedis, String key, RateLimiter.SleepingStopwatch stopwatch, double maxBurstSeconds) { 14 | super(key, stopwatch, maxBurstSeconds); 15 | this.jedis = jedis; 16 | } 17 | 18 | @Override 19 | protected Jedis getJedis() { 20 | return jedis; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/love/wangqi/tokenbucket/SmoothRateLimiter.java: -------------------------------------------------------------------------------- 1 | package love.wangqi.tokenbucket; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static java.util.concurrent.TimeUnit.SECONDS; 10 | 11 | /** 12 | * @author: wangqi 13 | * @description: 14 | * @date: Created in 2018/8/2 上午7:34 15 | */ 16 | public abstract class SmoothRateLimiter extends RateLimiter { 17 | 18 | String key; 19 | 20 | String script; 21 | 22 | double storedPermits = 0; 23 | 24 | double permitsPerSecond = 1; 25 | 26 | double maxPermits = 0; 27 | 28 | double stableIntervalMicros = 0; 29 | 30 | 31 | protected SmoothRateLimiter(SleepingStopwatch stopwatch) { 32 | super(stopwatch); 33 | } 34 | 35 | @Override 36 | void doSetRate(double permitsPerSecond) { 37 | queryWaitMicros(0, null); 38 | double stableIntervalMicros = SECONDS.toMicros(1L) / permitsPerSecond; 39 | this.stableIntervalMicros = stableIntervalMicros; 40 | doSetRate(permitsPerSecond, stableIntervalMicros); 41 | } 42 | 43 | abstract void doSetRate(double permitsPerSecond, double stableIntervalMicros); 44 | 45 | @Override 46 | double doGetRate() { 47 | return SECONDS.toMicros(1L) / stableIntervalMicros; 48 | } 49 | 50 | @Override 51 | long queryWaitMicros(int permits, Long timeoutMicros) { 52 | List keys = Arrays.asList(key, String.valueOf(maxPermits), String.valueOf(permitsPerSecond)); 53 | List args = new ArrayList<>(); 54 | args.add(String.valueOf(permits)); 55 | if (timeoutMicros != null) { 56 | args.add(String.valueOf(timeoutMicros)); 57 | } 58 | try { 59 | Jedis redis = getJedis(); 60 | Object result = redis.eval(script, keys, args); 61 | return (long) result; 62 | } catch (Exception e) { 63 | e.printStackTrace(); 64 | } 65 | return 0; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/resourses/counter_limit.lua: -------------------------------------------------------------------------------- 1 | -- 资源唯一标识 2 | local key = KEYS[1] 3 | -- 时间窗口内最大并发数 4 | local max_permits = tonumber(KEYS[2]) 5 | -- 窗口的间隔时间 6 | local interval_milliseconds = tonumber(KEYS[3]) 7 | -- 获取的并发数 8 | local permits = tonumber(ARGV[1]) 9 | 10 | local current_permits = tonumber(redis.call("get", key) or 0) 11 | 12 | -- 如果超过了最大并发数,返回false 13 | if (current_permits + permits > max_permits) then 14 | return false 15 | else 16 | -- 增加并发计数 17 | redis.call("incrby", key, permits) 18 | -- 如果key中保存的并发计数为0,说明当前是一个新的时间窗口,它的过期时间设置为窗口的过期时间 19 | if (current_permits == 0) then 20 | redis.call("pexpire", key, interval_milliseconds) 21 | end 22 | return true 23 | end 24 | -------------------------------------------------------------------------------- /src/main/resourses/smooth_ratelimiter.lua: -------------------------------------------------------------------------------- 1 | -- key 2 | local key = KEYS[1] 3 | -- 最大存储的令牌数 4 | local max_permits = tonumber(KEYS[2]) 5 | -- 每秒钟产生的令牌数 6 | local permits_per_second = tonumber(KEYS[3]) 7 | -- 请求的令牌数 8 | local required_permits = tonumber(ARGV[1]) 9 | 10 | -- 下次请求可以获取令牌的起始时间 11 | local next_free_ticket_micros = tonumber(redis.call('hget', key, 'next_free_ticket_micros') or 0) 12 | 13 | -- 当前时间 14 | local time = redis.call('time') 15 | local now_micros = tonumber(time[1]) * 1000000 + tonumber(time[2]) 16 | 17 | -- 查询获取令牌是否超时 18 | if (ARGV[2] ~= nil) then 19 | -- 获取令牌的超时时间 20 | local timeout_micros = tonumber(ARGV[2]) 21 | local micros_to_wait = next_free_ticket_micros - now_micros 22 | -- 不能获取到令牌,直接返回 23 | if (micros_to_wait > timeout_micros) then 24 | return micros_to_wait 25 | end 26 | end 27 | 28 | -- 当前存储的令牌数 29 | local stored_permits = tonumber(redis.call('hget', key, 'stored_permits') or 0) 30 | -- 添加令牌的时间间隔 31 | local stable_interval_micros = 1000000 / permits_per_second 32 | 33 | -- 补充令牌 34 | if (now_micros > next_free_ticket_micros) then 35 | local new_permits = (now_micros - next_free_ticket_micros) / stable_interval_micros 36 | stored_permits = math.min(max_permits, stored_permits + new_permits) 37 | next_free_ticket_micros = now_micros 38 | end 39 | 40 | -- 消耗令牌 41 | local stored_permits_to_spend = math.min(required_permits, stored_permits) 42 | local fresh_permits = required_permits - stored_permits_to_spend 43 | local wait_micros = fresh_permits * stable_interval_micros 44 | 45 | redis.replicate_commands() 46 | redis.call('hset', key, 'stored_permits', stored_permits - stored_permits_to_spend) 47 | redis.call('hset', key, 'next_free_ticket_micros', next_free_ticket_micros + wait_micros) 48 | redis.call('expire', key, 30) 49 | 50 | -- 返回需要等待的时间长度 51 | return next_free_ticket_micros - now_micros 52 | -------------------------------------------------------------------------------- /src/test/java/love/wangqi/CounterRateLimiterTest.java: -------------------------------------------------------------------------------- 1 | package love.wangqi; 2 | 3 | import love.wangqi.counter.CounterRateLimiter; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * @author: wangqi 12 | * @description: 13 | * @date: Created in 2018/8/1 下午7:50 14 | */ 15 | public class CounterRateLimiterTest { 16 | private static final Logger logger = LoggerFactory.getLogger(CounterRateLimiterTest.class); 17 | 18 | private CounterRateLimiter counterRateLimiter; 19 | 20 | public CounterRateLimiterTest() throws Exception { 21 | counterRateLimiter = new CounterRateLimiter("limiter", 1, 1 * 1000L); 22 | } 23 | 24 | public void doSomething() throws Exception { 25 | while (true) { 26 | if (counterRateLimiter.acquire(1)) { 27 | logger.info("do something"); 28 | break; 29 | } 30 | } 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | CounterRateLimiterTest action = new CounterRateLimiterTest(); 35 | 36 | List threadList = new ArrayList<>(); 37 | for (int i = 0; i < 100; i++) { 38 | threadList.add(new Thread(new Runnable() { 39 | @Override 40 | public void run() { 41 | try { 42 | action.doSomething(); 43 | } catch (Exception e) { 44 | e.printStackTrace(); 45 | } 46 | } 47 | })); 48 | } 49 | for (Thread thread : threadList) { 50 | thread.start(); 51 | } 52 | for (Thread thread : threadList) { 53 | thread.join(); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/love/wangqi/RateLimiterTest.java: -------------------------------------------------------------------------------- 1 | package love.wangqi; 2 | 3 | import love.wangqi.tokenbucket.RateLimiterBuilder; 4 | import love.wangqi.tokenbucket.RateLimiter; 5 | import org.junit.Test; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Arrays; 11 | import java.util.List; 12 | 13 | /** 14 | * @author: wangqi 15 | * @description: 16 | * @date: Created in 2018/8/2 上午8:21 17 | */ 18 | public class RateLimiterTest { 19 | final static Logger logger = LoggerFactory.getLogger(RateLimiterTest.class); 20 | 21 | @Test 22 | public void test01() throws InterruptedException { 23 | RateLimiterBuilder rateLimiterBuilder = new RateLimiterBuilder().setJedis(RedisPool.getJedis()); 24 | RateLimiter rateLimiter = rateLimiterBuilder.create("smooth_ratelimiter", 10.0); 25 | 26 | class Statistics { 27 | long start = 0; 28 | long count = 0; 29 | 30 | public synchronized void success() { 31 | if (start == 0) { 32 | start = System.currentTimeMillis(); 33 | } 34 | if (System.currentTimeMillis() - start <= 1000) { 35 | count++; 36 | } else { 37 | System.out.println("count " + count); 38 | start = System.currentTimeMillis(); 39 | count = 0; 40 | } 41 | } 42 | } 43 | 44 | Statistics statistics = new Statistics(); 45 | 46 | class MyRun implements Runnable { 47 | @Override 48 | public void run() { 49 | boolean acquire; 50 | do { 51 | acquire = rateLimiter.tryAcquire(); 52 | if (acquire) { 53 | logger.info("running... wait " + acquire); 54 | statistics.success(); 55 | } 56 | } while (!acquire); 57 | } 58 | } 59 | 60 | List threadList = new ArrayList<>(); 61 | for (int i = 0; i < 100; i++) { 62 | threadList.add(new Thread(new MyRun())); 63 | } 64 | for (Thread thread : threadList) { 65 | thread.start(); 66 | } 67 | for (Thread thread : threadList) { 68 | thread.join(); 69 | } 70 | 71 | Thread.sleep(3000); 72 | System.out.println("====================="); 73 | threadList.clear(); 74 | for (int i = 0; i < 100; i++) { 75 | threadList.add(new Thread(new MyRun())); 76 | } 77 | for (Thread thread : threadList) { 78 | thread.start(); 79 | } 80 | for (Thread thread : threadList) { 81 | thread.join(); 82 | } 83 | } 84 | 85 | @Test 86 | public void test02() throws InterruptedException { 87 | RateLimiterBuilder flowLimiter = new RateLimiterBuilder(); 88 | RateLimiter rateLimiter = flowLimiter.create("smooth_ratelimiter", 1.0); 89 | class MyRun implements Runnable { 90 | @Override 91 | public void run() { 92 | logger.info("running... wait " + rateLimiter.acquire()); 93 | } 94 | } 95 | 96 | List threadList = new ArrayList<>(); 97 | for (int i = 0; i < 100; i++) { 98 | threadList.add(new Thread(new MyRun())); 99 | } 100 | for (Thread thread : threadList) { 101 | thread.start(); 102 | } 103 | for (Thread thread : threadList) { 104 | thread.join(); 105 | } 106 | } 107 | 108 | @Test 109 | public void test03() { 110 | RateLimiterBuilder flowLimiter = new RateLimiterBuilder(); 111 | RateLimiter rateLimiter = flowLimiter.create("smooth_ratelimiter", 1.0); 112 | Arrays.asList(6, 2, 6).forEach(num -> System.out.println(System.currentTimeMillis() + " wait " + rateLimiter.acquire(num))); 113 | } 114 | } 115 | --------------------------------------------------------------------------------