{"id":1114,"date":"2025-07-07T10:12:50","date_gmt":"2025-07-07T02:12:50","guid":{"rendered":"https:\/\/beijian99.top\/?p=1114"},"modified":"2025-07-07T10:12:50","modified_gmt":"2025-07-07T02:12:50","slug":"%e9%99%90%e6%b5%81%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/beijian99.top\/?p=1114","title":{"rendered":"\u9650\u6d41\u7b97\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u9650\u6d41\uff1f<\/h2>\n\n\n\n<p>\u9650\u6d41\uff08Rate Limiting\uff09\u662f\u4e00\u79cd\u63a7\u5236\u670d\u52a1\u8bf7\u6c42\u901f\u7387\u7684\u673a\u5236\uff0c\u7528\u4e8e\u4fdd\u62a4\u7cfb\u7edf\u514d\u53d7\u8fc7\u8f7d\u8bf7\u6c42\u7684\u5f71\u54cd\u3002\u901a\u8fc7\u9650\u5236\u5355\u4f4d\u65f6\u95f4\u5185\u53ef\u4ee5\u5904\u7406\u7684\u8bf7\u6c42\u6570\u91cf\uff0c\u9650\u6d41\u53ef\u4ee5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u9632\u6b62\u8d44\u6e90\u88ab\u8017\u5c3d<\/li>\n\n\n\n<li>\u907f\u514d\u670d\u52a1\u96ea\u5d29<\/li>\n\n\n\n<li>\u4fdd\u8bc1\u670d\u52a1\u8d28\u91cf<\/li>\n\n\n\n<li>\u9632\u6b62\u6076\u610f\u653b\u51fb<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u89c1\u9650\u6d41\u7b97\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u8ba1\u6570\u5668\u7b97\u6cd5\uff08\u56fa\u5b9a\u7a97\u53e3\uff09<\/h3>\n\n\n\n<p>\u8ba1\u6570\u5668\u7b97\u6cd5\u662f\u6700\u7b80\u5355\u7684\u9650\u6d41\u7b97\u6cd5\uff0c\u5b83\u7ef4\u62a4\u4e00\u4e2a\u8ba1\u6570\u5668\u8bb0\u5f55\u5355\u4f4d\u65f6\u95f4\u5185\u7684\u8bf7\u6c42\u6570\u91cf\uff0c\u5f53\u8d85\u8fc7\u9608\u503c\u65f6\u62d2\u7edd\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p>\u200b<strong>\u200b\u7279\u70b9\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9e\u73b0\u7b80\u5355<\/li>\n\n\n\n<li>\u5b58\u5728\u4e34\u754c\u95ee\u9898\uff08\u7a97\u53e3\u5207\u6362\u65f6\u53ef\u80fd\u77ac\u95f4\u901a\u8fc7\u4e24\u500d\u9608\u503c\u8bf7\u6c42\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u200b<strong>\u200bGolang \u5b9e\u73b0\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\npackage main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;sync&quot;\n\t&quot;time&quot;\n)\n\ntype CounterLimiter struct {\n\trate  int           \/\/ \u9650\u5236\u6570\u91cf\n\tbegin time.Time     \/\/ \u7a97\u53e3\u5f00\u59cb\u65f6\u95f4\n\tcycle time.Duration \/\/ \u7a97\u53e3\u5468\u671f\n\tcount int           \/\/ \u8ba1\u6570\u5668\n\tmu    sync.Mutex    \/\/ \u9501\n}\n\nfunc NewCounterLimiter(rate int, cycle time.Duration) *CounterLimiter {\n\treturn &amp;CounterLimiter{\n\t\trate:  rate,\n\t\tbegin: time.Now(),\n\t\tcycle: cycle,\n\t\tcount: 0,\n\t}\n}\n\nfunc (l *CounterLimiter) Allow() bool {\n\tl.mu.Lock()\n\tdefer l.mu.Unlock()\n\n\t\/\/ \u5982\u679c\u5f53\u524d\u65f6\u95f4\u8d85\u8fc7\u7a97\u53e3\u5468\u671f\uff0c\u91cd\u7f6e\u7a97\u53e3\n\tif time.Now().After(l.begin.Add(l.cycle)) {\n\t\tl.begin = time.Now()\n\t\tl.count = 0\n\t}\n\n\t\/\/ \u5224\u65ad\u8bf7\u6c42\u6570\u662f\u5426\u8d85\u8fc7\u9650\u5236\n\tif l.count &gt;= l.rate {\n\t\treturn false\n\t}\n\n\tl.count++\n\treturn true\n}\n\nfunc main() {\n\tlimiter := NewCounterLimiter(100, time.Minute) \/\/ \u6bcf\u5206\u949f100\u4e2a\u8bf7\u6c42\n\n\tfor i := 0; i &lt; 150; i++ {\n\t\tif limiter.Allow() {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u901a\u8fc7\\n&quot;, i+1)\n\t\t} else {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u88ab\u9650\u6d41\\n&quot;, i+1)\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. \u6ed1\u52a8\u7a97\u53e3\u7b97\u6cd5<\/h3>\n\n\n\n<p>\u6ed1\u52a8\u7a97\u53e3\u7b97\u6cd5\u662f\u5bf9\u8ba1\u6570\u5668\u7b97\u6cd5\u7684\u6539\u8fdb\uff0c\u5b83\u5c06\u65f6\u95f4\u7a97\u53e3\u5212\u5206\u4e3a\u591a\u4e2a\u5c0f\u7a97\u53e3\uff0c\u901a\u8fc7\u7edf\u8ba1\u8fd9\u4e9b\u5c0f\u7a97\u53e3\u7684\u8bf7\u6c42\u6570\u6765\u907f\u514d\u4e34\u754c\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u200b<strong>\u200b\u7279\u70b9\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u89e3\u51b3\u4e86\u8ba1\u6570\u5668\u7b97\u6cd5\u7684\u4e34\u754c\u95ee\u9898<\/li>\n\n\n\n<li>\u6bd4\u8ba1\u6570\u5668\u7b97\u6cd5\u66f4\u7cbe\u786e<\/li>\n\n\n\n<li>\u5b9e\u73b0\u7a0d\u590d\u6742<\/li>\n<\/ul>\n\n\n\n<p>\u200b<strong>\u200bGolang \u5b9e\u73b0\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\npackage main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;sync&quot;\n\t&quot;time&quot;\n)\n\ntype SlidingWindowLimiter struct {\n\trate        int           \/\/ \u9650\u5236\u6570\u91cf\n\twindowSize  time.Duration \/\/ \u7a97\u53e3\u5927\u5c0f\n\tsegmentSize time.Duration \/\/ \u5c0f\u7a97\u53e3\u5927\u5c0f\n\tsegments    &#x5B;]int         \/\/ \u5c0f\u7a97\u53e3\u8ba1\u6570\u5668\n\tmu          sync.Mutex    \/\/ \u9501\n\tlastTime    time.Time     \/\/ \u4e0a\u6b21\u8bf7\u6c42\u65f6\u95f4\n}\n\nfunc NewSlidingWindowLimiter(rate int, windowSize, segmentSize time.Duration) *SlidingWindowLimiter {\n\tsegments := make(&#x5B;]int, int(windowSize\/segmentSize))\n\treturn &amp;SlidingWindowLimiter{\n\t\trate:        rate,\n\t\twindowSize:  windowSize,\n\t\tsegmentSize: segmentSize,\n\t\tsegments:    segments,\n\t\tlastTime:    time.Now(),\n\t}\n}\n\nfunc (l *SlidingWindowLimiter) Allow() bool {\n\tl.mu.Lock()\n\tdefer l.mu.Unlock()\n\n\tnow := time.Now()\n\telapsed := now.Sub(l.lastTime)\n\tl.lastTime = now\n\n\t\/\/ \u79fb\u52a8\u7a97\u53e3\n\tsteps := int(elapsed \/ l.segmentSize)\n\tif steps &gt; len(l.segments) {\n\t\tsteps = len(l.segments)\n\t}\n\tfor i := 0; i &lt; steps; i++ {\n\t\tl.segments = append(l.segments&#x5B;1:], 0)\n\t}\n\n\t\/\/ \u8ba1\u7b97\u5f53\u524d\u7a97\u53e3\u603b\u8bf7\u6c42\u6570\n\ttotal := 0\n\tfor _, count := range l.segments {\n\t\ttotal += count\n\t}\n\n\tif total &gt;= l.rate {\n\t\treturn false\n\t}\n\n\tl.segments&#x5B;len(l.segments)-1]++\n\treturn true\n}\n\nfunc main() {\n\tlimiter := NewSlidingWindowLimiter(100, time.Minute, 10*time.Second) \/\/ \u6bcf\u5206\u949f100\u4e2a\u8bf7\u6c42\uff0c10\u79d2\u4e00\u4e2a\u5c0f\u7a97\u53e3\n\n\tfor i := 0; i &lt; 150; i++ {\n\t\tif limiter.Allow() {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u901a\u8fc7\\n&quot;, i+1)\n\t\t} else {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u88ab\u9650\u6d41\\n&quot;, i+1)\n\t\t}\n\t\ttime.Sleep(500 * time.Millisecond)\n\t}\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. \u6f0f\u6876\u7b97\u6cd5<\/h3>\n\n\n\n<p>\u6f0f\u6876\u7b97\u6cd5\u6a21\u62df\u4e00\u4e2a\u56fa\u5b9a\u5bb9\u91cf\u7684\u6876\uff0c\u8bf7\u6c42\u4ee5\u4efb\u610f\u901f\u7387\u8fdb\u5165\u6876\u4e2d\uff0c\u4f46\u4ee5\u56fa\u5b9a\u901f\u7387\u6d41\u51fa\u3002\u5f53\u6876\u6ee1\u65f6\uff0c\u65b0\u8bf7\u6c42\u4f1a\u88ab\u62d2\u7edd\u3002<\/p>\n\n\n\n<p>\u200b<strong>\u200b\u7279\u70b9\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5e73\u6ed1\u6d41\u91cf\u8f93\u51fa<\/li>\n\n\n\n<li>\u65e0\u6cd5\u5e94\u5bf9\u7a81\u53d1\u6d41\u91cf<\/li>\n\n\n\n<li>\u5b9e\u73b0\u7b80\u5355<\/li>\n<\/ul>\n\n\n\n<p>\u200b<strong>\u200bGolang \u5b9e\u73b0\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\npackage main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;sync&quot;\n\t&quot;time&quot;\n)\n\ntype LeakyBucketLimiter struct {\n\trate       float64     \/\/ \u6d41\u51fa\u901f\u7387\uff08\u8bf7\u6c42\/\u79d2\uff09\n\tcapacity   float64     \/\/ \u6876\u5bb9\u91cf\n\twater      float64     \/\/ \u5f53\u524d\u6c34\u91cf\n\tlastLeakMs int64       \/\/ \u4e0a\u6b21\u6f0f\u6c34\u65f6\u95f4\uff08\u6beb\u79d2\uff09\n\tmu         sync.Mutex  \/\/ \u9501\n}\n\nfunc NewLeakyBucketLimiter(rate, capacity float64) *LeakyBucketLimiter {\n\treturn &amp;LeakyBucketLimiter{\n\t\trate:       rate,\n\t\tcapacity:   capacity,\n\t\tlastLeakMs: time.Now().UnixNano() \/ 1e6,\n\t}\n}\n\nfunc (l *LeakyBucketLimiter) Allow() bool {\n\tl.mu.Lock()\n\tdefer l.mu.Unlock()\n\n\tnowMs := time.Now().UnixNano() \/ 1e6\n\telapsedMs := nowMs - l.lastLeakMs\n\tl.lastLeakMs = nowMs\n\n\t\/\/ \u6f0f\u6c34\n\tl.water -= float64(elapsedMs) * l.rate \/ 1000\n\tif l.water &lt; 0 {\n\t\tl.water = 0\n\t}\n\n\t\/\/ \u5224\u65ad\u6876\u662f\u5426\u5df2\u6ee1\n\tif l.water &gt;= l.capacity {\n\t\treturn false\n\t}\n\n\tl.water++\n\treturn true\n}\n\nfunc main() {\n\tlimiter := NewLeakyBucketLimiter(10, 20) \/\/ \u6bcf\u79d210\u4e2a\u8bf7\u6c42\uff0c\u6876\u5bb9\u91cf20\n\n\tfor i := 0; i &lt; 50; i++ {\n\t\tif limiter.Allow() {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u901a\u8fc7\\n&quot;, i+1)\n\t\t} else {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u88ab\u9650\u6d41\\n&quot;, i+1)\n\t\t}\n\t\ttime.Sleep(50 * time.Millisecond)\n\t}\n}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"540\" src=\"https:\/\/beijian99.top\/wp-content\/uploads\/2025\/07\/image-4-1024x540.png\" alt=\"\" class=\"wp-image-1116\" srcset=\"https:\/\/beijian99.top\/wp-content\/uploads\/2025\/07\/image-4-1024x540.png 1024w, https:\/\/beijian99.top\/wp-content\/uploads\/2025\/07\/image-4-300x158.png 300w, https:\/\/beijian99.top\/wp-content\/uploads\/2025\/07\/image-4-768x405.png 768w, https:\/\/beijian99.top\/wp-content\/uploads\/2025\/07\/image-4.png 1426w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u4ee4\u724c\u6876\u7b97\u6cd5<\/h3>\n\n\n\n<p>\u4ee4\u724c\u6876\u7b97\u6cd5\u4ee5\u56fa\u5b9a\u901f\u7387\u5411\u6876\u4e2d\u6dfb\u52a0\u4ee4\u724c\uff0c\u8bf7\u6c42\u9700\u8981\u83b7\u53d6\u4ee4\u724c\u624d\u80fd\u88ab\u5904\u7406\u3002\u5f53\u6876\u4e2d\u6ca1\u6709\u4ee4\u724c\u65f6\uff0c\u8bf7\u6c42\u4f1a\u88ab\u62d2\u7edd\u3002<\/p>\n\n\n\n<p>\u200b<strong>\u200b\u7279\u70b9\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u5e94\u5bf9\u7a81\u53d1\u6d41\u91cf<\/li>\n\n\n\n<li>\u5b9e\u73b0\u76f8\u5bf9\u590d\u6742<\/li>\n\n\n\n<li>\u662f\u4e1a\u754c\u6700\u5e38\u7528\u7684\u9650\u6d41\u7b97\u6cd5<\/li>\n<\/ul>\n\n\n\n<p>\u200b<strong>\u200bGolang \u5b9e\u73b0\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\npackage main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;sync&quot;\n\t&quot;time&quot;\n)\n\ntype TokenBucketLimiter struct {\n\trate         float64     \/\/ \u4ee4\u724c\u751f\u6210\u901f\u7387\uff08\u4ee4\u724c\/\u79d2\uff09\n\tcapacity     float64     \/\/ \u6876\u5bb9\u91cf\n\ttokens       float64     \/\/ \u5f53\u524d\u4ee4\u724c\u6570\n\tlastTokenMs  int64       \/\/ \u4e0a\u6b21\u751f\u6210\u4ee4\u724c\u65f6\u95f4\uff08\u6beb\u79d2\uff09\n\tmu           sync.Mutex  \/\/ \u9501\n}\n\nfunc NewTokenBucketLimiter(rate, capacity float64) *TokenBucketLimiter {\n\treturn &amp;TokenBucketLimiter{\n\t\trate:        rate,\n\t\tcapacity:    capacity,\n\t\tlastTokenMs: time.Now().UnixNano() \/ 1e6,\n\t}\n}\n\nfunc (l *TokenBucketLimiter) Allow() bool {\n\tl.mu.Lock()\n\tdefer l.mu.Unlock()\n\n\tnowMs := time.Now().UnixNano() \/ 1e6\n\telapsedMs := nowMs - l.lastTokenMs\n\tl.lastTokenMs = nowMs\n\n\t\/\/ \u751f\u6210\u4ee4\u724c\n\tl.tokens += float64(elapsedMs) * l.rate \/ 1000\n\tif l.tokens &gt; l.capacity {\n\t\tl.tokens = l.capacity\n\t}\n\n\t\/\/ \u5224\u65ad\u662f\u5426\u6709\u4ee4\u724c\n\tif l.tokens &lt; 1 {\n\t\treturn false\n\t}\n\n\tl.tokens--\n\treturn true\n}\n\nfunc main() {\n\tlimiter := NewTokenBucketLimiter(10, 20) \/\/ \u6bcf\u79d210\u4e2a\u4ee4\u724c\uff0c\u6876\u5bb9\u91cf20\n\n\tfor i := 0; i &lt; 50; i++ {\n\t\tif limiter.Allow() {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u901a\u8fc7\\n&quot;, i+1)\n\t\t} else {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u88ab\u9650\u6d41\\n&quot;, i+1)\n\t\t}\n\t\ttime.Sleep(50 * time.Millisecond)\n\t}\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u7b97\u6cd5\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7b97\u6cd5<\/th><th>\u5b9e\u73b0\u96be\u5ea6<\/th><th>\u5e73\u6ed1\u5ea6<\/th><th>\u7a81\u53d1\u6d41\u91cf\u5904\u7406<\/th><th>\u8d44\u6e90\u6d88\u8017<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td>\u8ba1\u6570\u5668<\/td><td>\u7b80\u5355<\/td><td>\u4f4e<\/td><td>\u4e0d\u652f\u6301<\/td><td>\u4f4e<\/td><td>\u7b80\u5355\u9650\u6d41<\/td><\/tr><tr><td>\u6ed1\u52a8\u7a97\u53e3<\/td><td>\u4e2d\u7b49<\/td><td>\u4e2d<\/td><td>\u90e8\u5206\u652f\u6301<\/td><td>\u4e2d<\/td><td>\u9700\u8981\u66f4\u7cbe\u786e\u9650\u6d41<\/td><\/tr><tr><td>\u6f0f\u6876<\/td><td>\u4e2d\u7b49<\/td><td>\u9ad8<\/td><td>\u4e0d\u652f\u6301<\/td><td>\u4f4e<\/td><td>\u9700\u8981\u5e73\u6ed1\u8f93\u51fa<\/td><\/tr><tr><td>\u4ee4\u724c\u6876<\/td><td>\u590d\u6742<\/td><td>\u9ad8<\/td><td>\u652f\u6301<\/td><td>\u4e2d<\/td><td>\u4e3b\u6d41\u65b9\u6848\uff0c\u5141\u8bb8\u7a81\u53d1\u6d41\u91cf<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Golang \u6807\u51c6\u5e93\u5b9e\u73b0<\/h2>\n\n\n\n<p>Golang \u7684 <code>golang.org\/x\/time\/rate<\/code> \u5305\u63d0\u4f9b\u4e86\u57fa\u4e8e\u4ee4\u724c\u6876\u7b97\u6cd5\u7684\u9650\u6d41\u5668\u5b9e\u73b0\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\npackage main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;golang.org\/x\/time\/rate&quot;\n\t&quot;time&quot;\n)\n\nfunc main() {\n\t\/\/ \u6bcf\u79d210\u4e2a\u4ee4\u724c\uff0c\u6876\u5bb9\u91cf\u4e3a10\n\tlimiter := rate.NewLimiter(10, 10)\n\n\tfor i := 0; i &lt; 20; i++ {\n\t\tif limiter.Allow() {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u901a\u8fc7\\n&quot;, i+1)\n\t\t} else {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u88ab\u9650\u6d41\\n&quot;, i+1)\n\t\t}\n\t\ttime.Sleep(50 * time.Millisecond)\n\t}\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u5206\u5e03\u5f0f\u9650\u6d41<\/h2>\n\n\n\n<p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5355\u673a\u9650\u6d41\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff0c\u9700\u8981\u4f7f\u7528\u5206\u5e03\u5f0f\u9650\u6d41\u65b9\u6848\uff0c\u5e38\u89c1\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u200b<strong>\u200bRedis + Lua \u811a\u672c\u200b<\/strong>\u200b\uff1a\u5229\u7528 Redis \u7684\u539f\u5b50\u6027\u548c\u9ad8\u6027\u80fd<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u5206\u5e03\u5f0f\u4ee4\u724c\u6876\u200b<\/strong>\u200b\uff1a\u5982\u4f7f\u7528 Redis \u5b58\u50a8\u4ee4\u724c\u6570\u91cf<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u4e2d\u95f4\u4ef6\u200b<\/strong>\u200b\uff1a\u5982 Nginx\u3001Envoy \u63d0\u4f9b\u7684\u9650\u6d41\u529f\u80fd<\/li>\n<\/ol>\n\n\n\n<p>\u200b<strong>\u200bRedis + Lua \u793a\u4f8b\u200b<\/strong>\u200b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\npackage main\n\nimport (\n\t&quot;context&quot;\n\t&quot;fmt&quot;\n\t&quot;github.com\/go-redis\/redis\/v8&quot;\n\t&quot;time&quot;\n)\n\nvar ctx = context.Background()\nvar rdb = redis.NewClient(&amp;redis.Options{\n\tAddr: &quot;localhost:6379&quot;,\n})\n\nfunc isAllowed(key string, limit int, window time.Duration) bool {\n\t\/\/ \u4f7f\u7528Lua\u811a\u672c\u4fdd\u8bc1\u539f\u5b50\u6027\n\tscript := `\n\tlocal key = KEYS&#x5B;1]\n\tlocal limit = tonumber(ARGV&#x5B;1])\n\tlocal window = tonumber(ARGV&#x5B;2])\n\tlocal current = redis.call(&#039;GET&#039;, key)\n\tif current == false then\n\t\tredis.call(&#039;SET&#039;, key, 1, &#039;EX&#039;, window)\n\t\treturn 1\n\tend\n\tif tonumber(current) &gt;= limit then\n\t\treturn 0\n\telse\n\t\tredis.call(&#039;INCR&#039;, key)\n\t\treturn 1\n\tend\n\t`\n\tresult, err := rdb.Eval(ctx, script, &#x5B;]string{key}, limit, window.Seconds()).Result()\n\tif err != nil {\n\t\tfmt.Println(&quot;Redis error:&quot;, err)\n\t\treturn false\n\t}\n\treturn result.(int64) == 1\n}\n\nfunc main() {\n\tkey := &quot;user_123&quot; \/\/ \u7528\u6237ID\u6216\u5176\u4ed6\u6807\u8bc6\n\tlimit := 100      \/\/ \u9650\u5236\u6570\u91cf\n\twindow := time.Minute \/\/ \u65f6\u95f4\u7a97\u53e3\n\n\tfor i := 0; i &lt; 150; i++ {\n\t\tif isAllowed(key, limit, window) {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u901a\u8fc7\\n&quot;, i+1)\n\t\t} else {\n\t\t\tfmt.Printf(&quot;\u8bf7\u6c42 %d \u88ab\u9650\u6d41\\n&quot;, i+1)\n\t\t}\n\t\ttime.Sleep(500 * time.Millisecond)\n\t}\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u200b<\/strong>\u200b\uff1a\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u9650\u6d41\u7b97\u6cd5<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u591a\u7ea7\u9650\u6d41\u200b<\/strong>\u200b\uff1a\u53ef\u4ee5\u5728\u7f51\u5173\u3001\u670d\u52a1\u3001\u65b9\u6cd5\u7b49\u591a\u4e2a\u5c42\u6b21\u5b9e\u65bd\u9650\u6d41<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u52a8\u6001\u8c03\u6574\u200b<\/strong>\u200b\uff1a\u6839\u636e\u7cfb\u7edf\u8d1f\u8f7d\u52a8\u6001\u8c03\u6574\u9650\u6d41\u9608\u503c<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u533a\u5206\u7528\u6237\u200b<\/strong>\u200b\uff1a\u5bf9\u91cd\u8981\u7528\u6237\u548c\u666e\u901a\u7528\u6237\u5b9e\u65bd\u4e0d\u540c\u7684\u9650\u6d41\u7b56\u7565<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u76d1\u63a7\u4e0e\u544a\u8b66\u200b<\/strong>\u200b\uff1a\u76d1\u63a7\u9650\u6d41\u60c5\u51b5\uff0c\u53ca\u65f6\u53d1\u73b0\u5f02\u5e38<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u4f18\u96c5\u964d\u7ea7\u200b<\/strong>\u200b\uff1a\u88ab\u9650\u6d41\u65f6\u63d0\u4f9b\u53cb\u597d\u7684\u54cd\u5e94\uff0c\u5982\u6392\u961f\u9875\u9762\u6216\u7f13\u5b58\u5185\u5bb9<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u9650\u6d41\uff1f \u9650\u6d41\uff08Rate Limiting\uff09\u662f\u4e00\u79cd\u63a7\u5236\u670d\u52a1\u8bf7\u6c42\u901f\u7387\u7684\u673a\u5236\uff0c\u7528\u4e8e\u4fdd\u62a4\u7cfb\u7edf\u514d\u53d7\u8fc7\u8f7d\u8bf7\u6c42\u7684\u5f71\u54cd\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1115,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1114","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm"],"_links":{"self":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/1114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1114"}],"version-history":[{"count":1,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/1114\/revisions"}],"predecessor-version":[{"id":1117,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/1114\/revisions\/1117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/media\/1115"}],"wp:attachment":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}