{"id":1980,"date":"2021-03-23T14:57:33","date_gmt":"2021-03-23T06:57:33","guid":{"rendered":"\/?p=1980"},"modified":"2021-03-23T14:57:34","modified_gmt":"2021-03-23T06:57:34","slug":"33-%e9%ab%98%e7%ba%bf%e7%a8%8b%e5%b9%b6%e5%8f%91%e4%b8%8b%e7%9a%84%e9%99%90%e6%b5%81%e5%99%a8guava-ratelimter","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=1980","title":{"rendered":"33.\u9ad8\u7ebf\u7a0b\u5e76\u53d1\u4e0b\u7684\u9650\u6d41\u5668(Guava RateLimter)"},"content":{"rendered":"<p>\u4f7f\u7528Guava RateLimterr\u5982\u4f55\u89e3\u51b3\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u9650\u6d41\u95ee\u9898\u7684,\u5047\u8bbe\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6c60,\u6bcf\u79d2\u53ea\u80fd\u5904\u7406\u4e24\u4e2a\u4efb\u52a1<\/p>\n<p>\u90a3\u4e48\u5c31\u9700\u8981\u9650\u6d41\u5668\u4e0a\u573a\u4e86,\u63a5\u4e0b\u9762\u662f\u4e00\u4e2a\u5173\u4e8e\u9650\u6d41\u5668\u7684\u57fa\u672c\u4f7f\u7528\u7684\u4ee3\u7801<\/p>\n<p>public static void main(String[] args) {<\/p>\n<p>\/\/\u9650\u6d41\u5668\u6d41\u901f\uff1a2\u4e2a\u8bf7\u6c42\/\u79d2<\/p>\n<p>RateLimiter limiter = RateLimiter.create(2.0);<\/p>\n<p>\/\/\u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b\u6c60<\/p>\n<p>ExecutorService es = Executors.newFixedThreadPool(1);<\/p>\n<p>\/\/\u8bb0\u5f55\u4e0a\u4e00\u6b21\u6267\u884c\u65f6\u95f4<\/p>\n<p>prev = System.nanoTime();<\/p>\n<p>\/\/\u6d4b\u8bd5\u6267\u884c20\u6b21<\/p>\n<p>for (int i=0; i&lt;20; i++){<\/p>\n<p>\/\/\u9650\u6d41\u5668\u9650\u6d41<\/p>\n<p>limiter.acquire();<\/p>\n<p>\/\/\u63d0\u4ea4\u4efb\u52a1\u5f02\u6b65\u6267\u884c<\/p>\n<p>es.execute(()-&gt;{<\/p>\n<p>long cur=System.nanoTime();<\/p>\n<p>\/\/\u6253\u5370\u65f6\u95f4\u95f4\u9694\uff1a\u6beb\u79d2<\/p>\n<p>System.out.println((cur-prev)\/1000_000);<\/p>\n<p>prev = cur;<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>\/* \u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p>&#8230;<\/p>\n<p>500<\/p>\n<p>499<\/p>\n<p>499<\/p>\n<p>500<\/p>\n<p>499*\/<\/p>\n<p>}<\/p>\n<p>\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u9650\u6d41\u5668,\u8ba9\u5176\u6bcf\u79d2\u53ef\u4ee5\u5904\u7406\u4e24\u4e2a\u8bf7\u6c42,\u7136\u540e\u5728\u771f\u6b63\u6267\u884c\u4efb\u52a1\u4e4b\u524d,\u8c03\u7528acquire()\u65b9\u6cd5\u53ef\u4ee5\u8d77\u5230\u9650\u6d41\u7684\u4f5c\u7528,\u800c\u4e14\u7ebf\u7a0b\u7684\u65f6\u95f4\u95f4\u9694\u57fa\u672c\u5728500\u6beb\u79d2\u4e4b\u95f4<\/p>\n<p>\u90a3\u4e48\u5173\u4e8e\u9650\u6d41\u5668\u7684\u5b9e\u73b0,\u4f7f\u7528\u4e86\u4ee4\u724c\u6876\u7b97\u6cd5,\u8fd9\u4e2a\u7b97\u6cd5\u7684\u6838\u5fc3\u5728\u4e8e,\u901a\u8fc7\u9650\u6d41\u5668,\u9700\u8981\u62ff\u5230\u9650\u6d41\u5668\u63d0\u4f9b\u7684\u4ee4\u724c,\u8fd9\u4e2a\u9650\u6d41\u5668\u7684\u6838\u5fc3\u601d\u60f3\u5c31\u5728\u4e8e\u4ee4\u724c\u7684\u53d1\u653e\u901f\u5ea6,\u5982\u679c\u76f4\u63a5\u4f7f\u7528\u4e00\u4e2a\u5b9a\u65f6\u7ebf\u7a0b\u6c60,\u90a3\u4e48\u4f1a\u6709\u4e00\u4e2a\u95ee\u9898,\u5c31\u662f\u5728\u670d\u52a1\u5668\u9ad8\u538b\u60c5\u51b5\u4e0b,\u5b9a\u65f6\u4efb\u52a1\u53ef\u80fd\u51fa\u73b0\u4e0d\u51c6\u786e\u7684\u95ee\u9898<\/p>\n<p>\u5047\u8bbe\u4ee4\u724c\u4ee5\u6bcf500\u6beb\u79d2\u6dfb\u52a0\u5230\u4ee4\u724c\u6876\u4e2d,<\/p>\n<p>\u5982\u679c\u4ee4\u724c\u6876\u5bb9\u91cf\u4e3aa,\u90a3\u4e48\u5982\u679c\u518d\u5f80\u5176\u4e2d\u6dfb\u52a0\u7684\u4ee4\u724c\u90fd\u4f1a\u88ab\u4e22\u5f03<\/p>\n<p>\u8bf7\u6c42\u80fd\u591f\u901a\u8fc7\u9650\u6d41\u5668\u7684\u524d\u63d0\u6761\u4ef6\u662f\u62ff\u5230\u4e86\u4ee4\u724c<\/p>\n<p>\u5982\u4f55\u53bb\u5b9e\u73b0\u4ee4\u724c\u6876\u7b97\u6cd5,\u5f88\u7b80\u5355\u7684\u4e00\u4e2a\u7b97\u6cd5\u601d\u8def,\u5c31\u662f\u53bb\u8ba1\u7b97\u4e0b\u4e2a\u4ee4\u724c\u53d1\u653e\u7684\u4e8b\u4ef6,\u5982\u679c\u5bb9\u91cf\u4e3a1,\u901f\u7387\u4e3a1\u79d2\u4e00\u4e2a\u4ee4\u724c,\u90a3\u4e48\u53d1\u653e\u7684\u65b9\u5f0f\u5c31\u662f<\/p>\n<p>\u5982\u679c\u53d1\u653e\u7684\u65f6\u95f4\u662f\u7b2c\u4e09\u79d2,\u800c\u5728\u7b2c\u4e8c\u79d2\u6709\u4e00\u4e2a\u8bf7\u6c42\u5230\u6765,\u90a3\u4e48\u4ed6\u4f1a\u9884\u5b9a\u7b2c\u4e09\u79d2\u7684\u4ee4\u724c,\u5982\u679c\u540c\u65f6\u6709\u7b2c\u4e8c\u4e2a\u8bf7\u6c42\u5230\u6765,\u90a3\u4e48\u4ed6\u4f1a\u88ab\u63a8\u8fdf\u5230\u7b2c\u56db\u79d2\u83b7\u53d6\u4ee4\u724c,\u4e14\u5c06\u90a3\u4e2a\u4ee4\u724c\u9884\u5b9a\u4e0b<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"378\" height=\"185\" class=\"wp-image-1981\" src=\"\/wp-content\/uploads\/2021\/03\/unnamed-file-1091.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-1091.png 378w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-1091-300x147.png 300w\" sizes=\"(max-width: 378px) 100vw, 378px\" \/><\/p>\n<p>\u6700\u540e\u4e00\u4e2a\u7a7a\u95f2\u7684\u4ee4\u724c\u7684\u51fa\u73b0\u65f6\u95f4\u88ab\u63a8\u8fdf\u5230\u4e86\u7b2c\u4e94\u79d2<\/p>\n<p>\u53ea\u8981\u5229\u7528\u4e00\u4e2a\u52a8\u6001\u7684\u8ba1\u7b97\u4e0b\u4e00\u4e2a\u4ee4\u724c\u4ea7\u751f\u7684\u4e8b\u4ef6,\u5c31\u80fd\u5b8c\u6210\u8fd9\u4e2a\u9650\u6d41\u7684\u529f\u80fd,\u63a5\u4e0b\u6765\u5c31\u662f\u8fd9\u4e2a\u7b97\u6cd5\u7684\u7b80\u5355\u5b9e\u73b0<\/p>\n<p>class SimpleLimiter {<\/p>\n<p>\/\/\u4e0b\u4e00\u4ee4\u724c\u4ea7\u751f\u65f6\u95f4<\/p>\n<p>long next = System.nanoTime();<\/p>\n<p>\/\/\u53d1\u653e\u4ee4\u724c\u95f4\u9694\uff1a\u7eb3\u79d2<\/p>\n<p>long interval = 1000_000_000;<\/p>\n<p>\/\/\u9884\u5360\u4ee4\u724c\uff0c\u8fd4\u56de\u80fd\u591f\u83b7\u53d6\u4ee4\u724c\u7684\u65f6\u95f4<\/p>\n<p>synchronized long reserve(long now) {<\/p>\n<p>\/\/\u8bf7\u6c42\u65f6\u95f4\u5728\u4e0b\u4e00\u4ee4\u724c\u4ea7\u751f\u65f6\u95f4\u4e4b\u540e<\/p>\n<p>\/\/\u91cd\u65b0\u8ba1\u7b97\u4e0b\u4e00\u4ee4\u724c\u4ea7\u751f\u65f6\u95f4<\/p>\n<p>if (now &gt; next) {<\/p>\n<p>\/\/\u5c06\u4e0b\u4e00\u4ee4\u724c\u4ea7\u751f\u65f6\u95f4\u91cd\u7f6e\u4e3a\u5f53\u524d\u65f6\u95f4<\/p>\n<p>next = now;<\/p>\n<p>}<\/p>\n<p>\/\/\u80fd\u591f\u83b7\u53d6\u4ee4\u724c\u7684\u65f6\u95f4<\/p>\n<p>long at = next;<\/p>\n<p>\/\/\u8bbe\u7f6e\u4e0b\u4e00\u4ee4\u724c\u4ea7\u751f\u65f6\u95f4<\/p>\n<p>next += interval;<\/p>\n<p>\/\/\u8fd4\u56de\u7ebf\u7a0b\u9700\u8981\u7b49\u5f85\u7684\u65f6\u95f4<\/p>\n<p>return Math.max(at, 0L);<\/p>\n<p>}<\/p>\n<p>\/\/\u7533\u8bf7\u4ee4\u724c<\/p>\n<p>void acquire() {<\/p>\n<p>\/\/\u7533\u8bf7\u4ee4\u724c\u65f6\u7684\u65f6\u95f4<\/p>\n<p>long now = System.nanoTime();<\/p>\n<p>\/\/\u9884\u5360\u4ee4\u724c<\/p>\n<p>long at = reserve(now);<\/p>\n<p>long waitTime = max(at &#8211; now, 0);<\/p>\n<p>\/\/\u6309\u7167\u6761\u4ef6\u7b49\u5f85<\/p>\n<p>if (waitTime &gt; 0) {<\/p>\n<p>try {<\/p>\n<p>TimeUnit.NANOSECONDS.sleep(waitTime);<\/p>\n<p>} catch (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u518d\u5f80\u590d\u6742\u4e86\u8bf4,\u5982\u679c\u6876\u7684\u5bb9\u91cf\u5927\u4e8e1,\u90a3\u4e48\u5982\u4f55\u5904\u7406,\u5f88\u7b80\u5355,\u53ea\u662f\u589e\u52a0\u4e00\u4e2a\u8ba1\u7b97\u6876\u4e2d\u4ee4\u724c\u6570\u91cf\u7684\u65b9\u6cd5,\u5728\u8bf7\u6c42\u4ee4\u724c\u7684\u65f6\u5019,\u8fdb\u884c\u4e00\u4e2a\u67e5\u8be2\u6570\u91cf,\u5982\u679c\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5230,\u90a3\u4e48\u5c31\u65e0\u987b\u589e\u52a0\u4e00\u4e2ainterval\u4e86<\/p>\n<p>class SimpleLimiter {<\/p>\n<p>\/\/\u5f53\u524d\u4ee4\u724c\u6876\u4e2d\u7684\u4ee4\u724c\u6570\u91cf<\/p>\n<p>long storedPermits = 0;<\/p>\n<p>\/\/\u4ee4\u724c\u6876\u7684\u5bb9\u91cf<\/p>\n<p>long maxPermits = 3;<\/p>\n<p>\/\/\u4e0b\u4e00\u4ee4\u724c\u4ea7\u751f\u65f6\u95f4<\/p>\n<p>long next = System.nanoTime();<\/p>\n<p>\/\/\u53d1\u653e\u4ee4\u724c\u95f4\u9694\uff1a\u7eb3\u79d2<\/p>\n<p>long interval = 1000_000_000;<\/p>\n<p>\/\/\u8bf7\u6c42\u65f6\u95f4\u5728\u4e0b\u4e00\u4ee4\u724c\u4ea7\u751f\u65f6\u95f4\u4e4b\u540e,\u5219<\/p>\n<p>\/\/ 1.\u91cd\u65b0\u8ba1\u7b97\u4ee4\u724c\u6876\u4e2d\u7684\u4ee4\u724c\u6570<\/p>\n<p>\/\/ 2.\u5c06\u4e0b\u4e00\u4e2a\u4ee4\u724c\u53d1\u653e\u65f6\u95f4\u91cd\u7f6e\u4e3a\u5f53\u524d\u65f6\u95f4<\/p>\n<p>void resync(long now) {<\/p>\n<p>if (now &gt; next) {<\/p>\n<p>\/\/\u65b0\u4ea7\u751f\u7684\u4ee4\u724c\u6570<\/p>\n<p>long newPermits = (now &#8211; next) \/ interval;<\/p>\n<p>\/\/\u65b0\u4ee4\u724c\u589e\u52a0\u5230\u4ee4\u724c\u6876<\/p>\n<p>storedPermits = min(maxPermits, storedPermits + newPermits);<\/p>\n<p>\/\/\u5c06\u4e0b\u4e00\u4e2a\u4ee4\u724c\u53d1\u653e\u65f6\u95f4\u91cd\u7f6e\u4e3a\u5f53\u524d\u65f6\u95f4<\/p>\n<p>next = now;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/\u9884\u5360\u4ee4\u724c\uff0c\u8fd4\u56de\u80fd\u591f\u83b7\u53d6\u4ee4\u724c\u7684\u65f6\u95f4<\/p>\n<p>synchronized long reserve(long now) {<\/p>\n<p>resync(now);<\/p>\n<p>\/\/\u80fd\u591f\u83b7\u53d6\u4ee4\u724c\u7684\u65f6\u95f4<\/p>\n<p>long at = next;<\/p>\n<p>\/\/\u4ee4\u724c\u6876\u4e2d\u80fd\u63d0\u4f9b\u7684\u4ee4\u724c<\/p>\n<p>long fb = min(1, storedPermits);<\/p>\n<p>\/\/\u4ee4\u724c\u51c0\u9700\u6c42\uff1a\u9996\u5148\u51cf\u6389\u4ee4\u724c\u6876\u4e2d\u7684\u4ee4\u724c<\/p>\n<p>long nr = 1 &#8211; fb;<\/p>\n<p>\/\/\u91cd\u65b0\u8ba1\u7b97\u4e0b\u4e00\u4ee4\u724c\u4ea7\u751f\u65f6\u95f4<\/p>\n<p>next = next + nr * interval;<\/p>\n<p>\/\/\u91cd\u65b0\u8ba1\u7b97\u4ee4\u724c\u6876\u4e2d\u7684\u4ee4\u724c<\/p>\n<p>this.storedPermits -= fb;<\/p>\n<p>return at;<\/p>\n<p>}<\/p>\n<p>\/\/\u7533\u8bf7\u4ee4\u724c<\/p>\n<p>void acquire() {<\/p>\n<p>\/\/\u7533\u8bf7\u4ee4\u724c\u65f6\u7684\u65f6\u95f4<\/p>\n<p>long now = System.nanoTime();<\/p>\n<p>\/\/\u9884\u5360\u4ee4\u724c<\/p>\n<p>long at = reserve(now);<\/p>\n<p>long waitTime = max(at &#8211; now, 0);<\/p>\n<p>\/\/\u6309\u7167\u6761\u4ef6\u7b49\u5f85<\/p>\n<p>if (waitTime &gt; 0) {<\/p>\n<p>try {<\/p>\n<p>TimeUnit.NANOSECONDS<\/p>\n<p>.sleep(waitTime);<\/p>\n<p>} catch (InterruptedException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u7ecf\u5178\u7684\u9650\u6d41\u7b97\u6cd5,\u53ef\u4ee5\u5206\u4e3a\u4ee4\u724c\u6876\u7b97\u6cd5\u548c\u6f0f\u6876\u7b97\u6cd5,\u8fd9\u5c31\u662f\u4e00\u4e2a\u786c\u5e01\u7684\u6b63\u53cd\u9762,\u4ee4\u724c\u6876\u7b97\u6cd5\u8981\u6c42\u5fc5\u987b\u62ff\u5230\u4ee4\u724c\u624d\u80fd\u901a\u8fc7\u9650\u6d41\u5668,\u800c\u6f0f\u6876\u7b97\u6cd5\u8fd9\u662f\u5c06\u8bf7\u6c42\u5f52\u4e3a\u4e00\u4e2a\u6876\u4e2d,\u6876\u4e2d\u6309\u7167\u4e00\u5b9a\u7684\u901f\u7387\u6765\u5904\u7406\u8bf7\u6c42,\u53ea\u6709\u6876\u4e2d\u6709\u7a7a\u4f59,\u624d\u80fd\u901a\u8fc7\u9650\u6d41\u5668<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Guava RateLim [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/1980"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1980"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/1980\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1980"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}