{"id":287,"date":"2022-06-16T15:49:22","date_gmt":"2022-06-16T07:49:22","guid":{"rendered":"http:\/\/blog.kijk.top\/?p=287"},"modified":"2022-06-16T15:49:22","modified_gmt":"2022-06-16T07:49:22","slug":"golang%e9%99%90%e9%80%9f%e7%ae%97%e6%b3%95%e5%92%8c%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"http:\/\/blog.kijk.top\/?p=287","title":{"rendered":"Golang\u9650\u901f\u7b97\u6cd5\u548c\u5e94\u7528"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\u4e8e\uff1ahttps:\/\/wangbjun.site\/2020\/coding\/golang\/limiter.html<\/p>\n\n\n\n<p>\u4eca\u5929\u6765\u8bf4\u8bf4\u9650\u901f\u7b97\u6cd5\uff0c\u5176\u5b9e\u8fd9\u4e2a\u5012\u4e0d\u662f\u4ec0\u4e48\u795e\u79d8\u7684\u4e8b\u60c5\uff0c\u672c\u6587\u4e3b\u8981\u505a\u4e00\u4e2a\u603b\u7ed3\uff0c\u5e76\u4e14\u4eceGolang\u8fd9\u4e48\u8bed\u8a00\u7684\u89d2\u5ea6\u505a\u4e00\u4e2a\u5b9e\u8df5\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u4f5c\u4e3a\u4e00\u4e2a\u5b8c\u5168\u4e0d\u61c2\u7b97\u6cd5\u7684\u4eba\uff0c\u8ba9\u4f60\u53bb\u5b9e\u73b0\u4e00\u4e2a\u9650\u901f\u529f\u80fd\uff081\u79d2\u5185\u6700\u591a100\u6b21\uff09\uff0c\u4f60\u53ef\u80fd\u4f1a\u60f3\u5230\u7684\u6700\u7b80\u5355\u65b9\u5f0f\u5c31\u662f\u8bb0\u5f55\u4e00\u4e2a\u5f00\u59cb\u65f6\u95f4\uff0c\u7136\u540e\u5f00\u59cb\u8ba1\u6570\uff0c\u5f53\u8ba1\u6570\u8fbe\u5230100\u4e4b\u540e\u9650\u5236\u8c03\u7528\uff0c\u7b49\u5f85\u65f6\u95f4\u95f4\u9694\u8fbe\u52301\u79d2\u7684\u65f6\u5019\u91cd\u7f6e\u8ba1\u6570\u5668\uff0c\u7136\u540e\u91cd\u65b0\u8ba1\u6570\uff0c\u5982\u6b64\u5f80\u590d\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u7b97\u6cd5\u4e5f\u662f\u65e5\u5e38\u751f\u6d3b\u4e2d\u5e94\u7528\u5e7f\u6cdb\uff0c\u6bd4\u5982\u8bf4\u5750\u5730\u94c1\uff0c\u5728\u5317\u4eac\uff0c\u5f88\u591a\u5730\u94c1\u7ad9\u5728\u65e9\u665a\u9ad8\u5cf0\u65f6\u671f\u90fd\u4f1a\u9650\u6d41\uff0c\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u5730\u94c1\u7ad9\u95e8\u53e3\u6709\u5b89\u68c0\u5458\uff0c\u6bcf\u8fc7\u4e00\u6bb5\u65f6\u95f4\u653e\u51e0\u4e2a\u4eba\u8fdb\u53bb\uff0c\u867d\u7136\u5b89\u68c0\u5458\u4e5f\u4e0d\u4f1a\u6390\u7740\u8868\u8ba1\u65f6\uff0c\u4f46\u662f\u57fa\u672c\u4e0a\u662f\u8fd9\u4e2a\u89c4\u5f8b\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u88ab\u79f0\u4e3a <code>\u8ba1\u6570\u5668\u7b97\u6cd5<\/code>\uff0c\u6709\u4e9b\u6587\u7ae0\u4e5f\u79f0\u4e4b\u4e3a <code>\u56fa\u5b9a\u65f6\u95f4\u7a97\u53e3\u8ba1\u6570\u6cd5<\/code>\uff0c\u56e0\u4e3a\u4e0e\u4e4b\u5bf9\u5e94\u7684\u8fd8\u6709\u4e00\u4e2a\u65b9\u6cd5\u53eb\u4f5c <code>\u6ed1\u52a8\u65f6\u95f4\u7a97\u53e3\u8ba1\u6570\u6cd5<\/code>\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>1.\u8ba1\u6570\u5668\u6cd5<\/p><\/blockquote>\n\n\n\n<p>\u8fd9\u79cd\u7b97\u6cd5\u5b9e\u73b0\u8d77\u6765\u5e76\u4e0d\u96be\uff0c\u4e3b\u8981\u8981\u70b9\u5728\u4e8e\u91cd\u7f6e\u8ba1\u6570\u5668\u548c\u5f00\u59cb\u65f6\u95f4\uff0c\u6211\u8fd9\u91cc\u7528Go\uff0c\u4ee5\u4e00\u4e2a\u65b9\u6cd5\u7684\u8c03\u7528\u4e3a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    var count = 1\r\n    var sTime = time.Now()\r\n\r\n    for {\r\n        if time.Now().Sub(sTime).Seconds() > 1  {\r\n            count = 1\r\n            sTime = time.Now()\r\n        }\r\n        if count > 100  {\r\n            time.Sleep(time.Millisecond * 10)\r\n            continue\r\n        }\r\n        doSomething(count)\r\n        count++\r\n    }\r\n}\r\n\r\nfunc doSomething(n int) {\r\n    fmt.Printf(\"do something #%d\\n\", n)\r\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u7b97\u6cd5\u6700\u5927\u95ee\u9898\u5728\u4e8e\u4e34\u754c\u70b9\u95ee\u9898\uff0c\u6bd4\u5982\u8bf4\u5728\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u91cc\u9762\uff0c\u5047\u8bbe\u6211\u4eec\u5728\u524d\u97621\u79d2\u949f\u4e2d\u6700\u540e999ms\u7684\u65f6\u5019\u6253\u5370\u4e86100\u4e2a\uff0c\u53c8\u5728\u7b2c\u4e8c\u4e2a1\u79d2\u7684\u524d1ms\u5185\u53c8\u6253\u5370\u4e86100\u4e2a\uff0c\u90a3\u4e48\u603b\u5f97\u7b97\u8d77\u6765\uff0c\u5176\u57282ms\u5185\u5c31\u6253\u5370\u4e86200\u4e2a\uff0c\u8d85\u51fa\u4e86\u6211\u4eec\u9650\u5236\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\u5c31\u662f1ms\u5c31\u4f1a\u6253\u5370\u5b8c100\u4e2a\uff0c\u540e\u9762999ms\u90fd\u4f1a\u5728\u7b49\u5f85\u4e2d\uff0c\u5047\u8bbe\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6bd4\u5982\u8bf4\u63a5\u53e3\u9650\u6d41\uff0c\u5047\u5982\u8bf4\u67d0\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42\u901f\u5ea6\u6bd4\u8f83\u5feb\uff0c\u90a3\u4e48\u5c31\u4f1a\u77ac\u95f4\u6d88\u8017\u6240\u6709\u914d\u989d\uff0c\u5bfc\u81f4\u5176\u5b83\u7528\u6237\u65e0\u6cd5\u6b63\u5e38\u8bbf\u95ee\uff0c\u8fd9\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u8fd9\u4e0d\u610f\u5473\u7740\u8fd9\u4e2a\u7b97\u6cd5\u6ca1\u7528\uff0c\u5f88\u591a\u65f6\u5019\u6211\u4eec\u7684\u9700\u6c42\u5c31\u662f\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u63a7\u5236\u5e76\u53d1\u6570\uff0c\u5982\u679c\u4e0d\u8003\u8651\u4e0a\u8ff0\u60c5\u51b5\u7684\u8bdd\u662f\u5b8c\u5168\u53ef\u7528\u7684\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>2.\u6ed1\u52a8\u7a97\u53e3\u8ba1\u6570\u6cd5<\/p><\/blockquote>\n\n\n\n<p>\u8fd9\u4e2a\u7b97\u6cd5\u5b9e\u9645\u4e0a\u662f\u524d\u9762\u8fd9\u79cd\u7b97\u6cd5\u7684\u6539\u8fdb\u7248\uff0c\u7b2c\u4e00\u79cd\u65b9\u6cd5\u7684\u95ee\u9898\u5728\u4e8e\u7edf\u8ba1\u7684\u65f6\u95f4\u592a\u7c97\u4e86\uff0c\u4e0d\u591f\u7cbe\u7ec6\uff0c\u5047\u8bbe\u6211\u4eec\u628a1s\u7ec6\u5206\u4e3a1000ms\uff0c\u90a3\u4e48\u4e5f\u5c31\u662f10ms\u4e00\u4e2a\u8bf7\u6c42\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u53ea\u9700\u8981\u7edf\u8ba1\u6700\u8fd1100\u4e2a10ms\u6bb5\u5185\u7684\u8bf7\u6c42\u6570\u5c31\u53ef\u4ee5\u4e86\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u56fa\u5b9a\u7684\u5f00\u59cb\u65f6\u95f4\u3002\u8fd9\u4e2a\u7b97\u6cd5\u6709\u4e00\u4e2a\u975e\u5e38\u77e5\u540d\u7684\u5e94\u7528\u5c31\u662f\u5728TCP\u7f51\u7edc\u4e2d\u6d41\u91cf\u63a7\u5236\u4e2d\u7528\u4e8e\u8ba1\u7b97\u7a97\u53e3\u5927\u5c0f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"452\" src=\"http:\/\/blog.kijk.top\/wp-content\/uploads\/2022\/06\/2020-06-16_22-52-1024x452.png\" alt=\"\" class=\"wp-image-288\" srcset=\"http:\/\/blog.kijk.top\/wp-content\/uploads\/2022\/06\/2020-06-16_22-52-1024x452.png 1024w, http:\/\/blog.kijk.top\/wp-content\/uploads\/2022\/06\/2020-06-16_22-52-300x132.png 300w, http:\/\/blog.kijk.top\/wp-content\/uploads\/2022\/06\/2020-06-16_22-52-768x339.png 768w, http:\/\/blog.kijk.top\/wp-content\/uploads\/2022\/06\/2020-06-16_22-52-1536x678.png 1536w, http:\/\/blog.kijk.top\/wp-content\/uploads\/2022\/06\/2020-06-16_22-52.png 1984w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u6ed1\u52a8\u7a97\u53e3\u7684\u6838\u5fc3\u601d\u60f3\u662f\u628a\u65f6\u95f4\u6bb5\u5207\u5272\u6210\u66f4\u5c0f\u7684\u533a\u5757\uff0c\u6bcf\u4e2a\u533a\u5757\u5355\u72ec\u8ba1\u6570\uff0c\u6700\u540e\u7edf\u8ba1\u8fd9\u4e2a\u65f6\u95f4\u6bb5\u5185\u5404\u4e2a\u5c0f\u533a\u5757\u7684\u603b\u8ba1\u6570\uff0c\u5176\u91cd\u70b9\u5728\u4e8e\u8fd9\u4e2a\u65f6\u95f4\u6bb5\u5e76\u4e0d\u662f\u4e00\u4e2a\u56fa\u5b9a\u7684\u65f6\u95f4\u6bb5\uff0c\u800c\u662f\u5728\u52a8\u6001\u53d8\u5316\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u4fbf\u4e8e\u6f14\u793a\uff0c\u8fd9\u91cc\u5047\u8bbe\u9650\u6d41\u6761\u4ef6\u4e3a10s\u5185\u6700\u591a500\u4e2a\u8bf7\u6c42\uff0c\u6211\u4eec\u4ee51s\u4e3a\u533a\u5757\u5212\u5206\u4e3a10\u4e2a\u533a\u5757\uff0c\u6bcf\u4e2a\u533a\u5757\u5355\u72ec\u8ba1\u6570\uff0c\u8fd9\u4e2a\u7b97\u6cd5\u5b9e\u73b0\u7684\u6838\u5fc3\u5728\u4e8e\u7ef4\u6301\u4e00\u4e2a\u961f\u5217\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86Go\u81ea\u5e26\u7684list\u5b9e\u73b0\uff0c\u53c2\u8003\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n    \"container\/list\"\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\ntype part struct {\r\n    Time int64\r\n    Count int\r\n}\r\n\r\nfunc main() {\r\n    var listPart = list.New()\r\n    for {\r\n        var nowSecond = time.Now().Unix()\r\n        if listPart.Len() == 0 {\r\n            for i := 0; i &lt; 10; i++ {\r\n                listPart.PushBack(part{\r\n                    Time: nowSecond+int64(i),\r\n                    Count: 0,\r\n                })\r\n            }\r\n        }\r\n\r\n        \/\/ \u6253\u5370\u65b9\u4fbf\u8c03\u8bd5\r\n        for e := listPart.Front(); e != nil; e = e.Next() {\r\n            fmt.Printf(\"e = %v\\n\", e.Value)\r\n        }\r\n\r\n        if listSum(listPart) >= 500 {\r\n            \/\/ \u53f3\u79fb\u52a8\u961f\u5217\u5143\u7d20\r\n            if listPart.Back().Value.(part).Time == nowSecond{\r\n                listPart.Remove(listPart.Front())\r\n                listPart.PushBack(part{\r\n                    Time: nowSecond+1,\r\n                    Count: 0,\r\n                })\r\n            }else {\r\n                fmt.Println(\"reach limit\")\r\n                time.Sleep(time.Second)\r\n                continue\r\n            }\r\n        }\r\n\r\n        time.Sleep(time.Millisecond*10)\r\n        doSomething()\r\n\r\n        element := listGet(listPart, nowSecond)\r\n        count := element.Value.(part).Count\r\n        element.Value = part{\r\n            Time: nowSecond,\r\n            Count: count+1,\r\n        }\r\n    }\r\n}\r\n\r\nfunc doSomething() {\r\n    fmt.Printf(\"do something \\n\")\r\n}\r\n\r\n\/\/ \u961f\u5217\u6c42\u548c\r\nfunc listSum(l *list.List) int {\r\n    var sum int\r\n    for e := l.Front(); e != nil; e = e.Next() {\r\n        sum += e.Value.(part).Count\r\n    }\r\n    return sum\r\n}\r\n\r\n\/\/ \u67e5\u627e\u961f\u5217\u5143\u7d20\r\nfunc listGet(l *list.List, t int64) *list.Element {\r\n    for e := l.Front(); e != nil; e = e.Next() {\r\n        if e.Value.(part).Time == t {\r\n            return e\r\n        }\r\n    }\r\n    return nil\r\n}<\/code><\/pre>\n\n\n\n<p>\u5b9e\u9645\u8fd0\u884c\u4e2d\uff0c\u6574\u4e2a\u961f\u5217\u5143\u7d20\u4fdd\u6301\u4e0d\u53d8\uff0c\u4e00\u76f4\u662f10\u4e2a\uff0c\u4f46\u662f\u5176\u65f6\u95f4\u6bb5\u4e00\u76f4\u5728\u53d8\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e2a\u60c5\u51b5\u4e0b\uff0c\u524d\u9762\u6240\u6709\u7684\u65f6\u95f4\u533a\u5757\u8ba1\u6570\u4e4b\u548c\u5df2\u7ecf\u8fbe\u5230500\uff0c\u5f00\u59cb\u9650\u6d41\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>e = {1592323440 53} \/\/\u5373\u5c06\u88ab\u79fb\u9664\r\n------------------------------------\r\ne = {1592323441 98}\r\ne = {1592323442 98}\r\ne = {1592323443 98}\r\ne = {1592323444 98}\r\ne = {1592323445 55}\r\ne = {1592323446 0}\r\ne = {1592323447 0}\r\ne = {1592323448 0}\r\ne = {1592323449 0}\r\n-------------------------------------\r\ne = {1592323450 0} \/\/\u5373\u5c06\u65b0\u5165\u5217\r\n-------------------------------------<\/code><\/pre>\n\n\n\n<p>\u968f\u7740\u65f6\u95f4\u7684\u63a8\u8fdb\uff0c\u65b0\u52a0\u5165\u4e00\u4e2a\u533a\u5757\uff0c\u5c31\u6709\u65b0\u7684\u6d41\u91cf\u53ef\u7528\u4e86\uff0c\u5982\u6b64\u5f80\u590d\u4e0d\u95f4\u65ad\uff0c\u5b9e\u73b0\u4e86\u52a8\u6001\u65f6\u95f4\u7a97\u53e3\uff01<\/p>\n\n\n\n<p>\u8fd9\u79cd\u5b9e\u73b0\u65b9\u6cd5\u533a\u5757\u5212\u5206\u7684\u8d8a\u591a\u8d8a\u7cbe\u786e,\u4f46\u662f\u5230\u5e95\u8be5\u8bbe\u7f6e\u591a\u5c11\u4e2a\u683c\u5b50\u624d\u8db3\u591f\u7cbe\u786e\u5462\uff1f\u800c\u4e14\u8fd9\u79cd\u65b9\u5f0f\u4f9d\u7136\u6ca1\u6709\u89e3\u51b3\u7a81\u53d1\u6d41\u91cf\u77ac\u95f4\u5360\u7528\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u53c8\u5ef6\u4f38\u51fa\u4e862\u79cd\u6d41\u884c\u7684\u5e73\u6ed1\u9650\u6d41\u7b97\u6cd5\u5206\u522b\u662f <code>\u6f0f\u6876\u7b97\u6cd5<\/code> \u548c <code>\u4ee4\u724c\u6876\u7b97\u6cd5<\/code>\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>3.\u6f0f\u6876\u7b97\u6cd5<\/p><\/blockquote>\n\n\n\n<p>\u6240\u8c13\u6f0f\u6876\u7b97\u6cd5\u5c31\u662f\u6709\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u6876\uff0c\u8fdb\u6c34\u901f\u7387\u4e0d\u786e\u5b9a\uff0c\u4f46\u662f\u51fa\u6c34\u901f\u7387\u56fa\u5b9a\uff0c\u8fd9\u91cc\u6240\u8c13\u7684\u8fdb\u6c34\u6307\u7684\u5c31\u662f\u8bf7\u6c42\uff0c\u800c\u51fa\u6c34\u5219\u662f\u5904\u7406\u8bf7\u6c42\u3002<br>\u6f0f\u6876\u7b97\u6cd5\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul><li>\u6f0f\u6876\u5177\u6709\u56fa\u5b9a\u5bb9\u91cf\uff0c\u51fa\u6c34\u901f\u7387\u662f\u56fa\u5b9a\u5e38\u91cf\uff08\u6d41\u51fa\u8bf7\u6c42\uff09<\/li><li>\u5982\u679c\u6876\u662f\u7a7a\u7684\uff0c\u5219\u4e0d\u9700\u6d41\u51fa\u6c34\u6ef4<\/li><li>\u53ef\u4ee5\u4ee5\u4efb\u610f\u901f\u7387\u6d41\u5165\u6c34\u6ef4\u5230\u6f0f\u6876\uff08\u6d41\u5165\u8bf7\u6c42\uff09<\/li><li>\u5982\u679c\u6d41\u5165\u6c34\u6ef4\u8d85\u51fa\u4e86\u6876\u7684\u5bb9\u91cf\uff0c\u5219\u6d41\u5165\u7684\u6c34\u6ef4\u6ea2\u51fa\uff08\u65b0\u8bf7\u6c42\u88ab\u62d2\u7edd\uff09<\/li><\/ul>\n\n\n\n<p>\u6f0f\u6876\u7b97\u6cd5\u5b9e\u73b0\u7684\u91cd\u70b9\u5728\u4e8e\u6709\u4e00\u4e2a\u6876\u7684\u5bb9\u91cf\u3001\u901f\u7387\u3001\u5f53\u524d\u6c34\u91cf\u3001\u4e00\u4e2a\u52a8\u6001\u7684\u65f6\u95f4\u6233\uff0c\u6211\u4eec\u901a\u8fc7\u6bcf\u6b21\u8ba1\u7b97\u5f53\u524d\u65f6\u95f4\u548c\u4e0a\u6b21\u65f6\u95f4\u7684\u95f4\u9694*\u901f\u7387\u5f97\u5230\u4e00\u4e2a\u6f0f\u53bb\u7684\u6c34\u91cf\uff0c\u628a\u5f53\u524d\u6c34\u91cf\u51cf\u53bb\u8fd9\u4e2a\u503c\u5c31\u662f\u5f53\u524d\u5269\u4f59\u6c34\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    var capacity int64 = 100 \/\/ \u6876\u5bb9\u91cf\r\n    var rate = 10            \/\/ \u51fa\u6c34\u901f\u5ea6\uff0c\u6bcf\u79d210\u4e2a\r\n    var water int64 = 0      \/\/ \u5f53\u524d\u6c34\u91cf\r\n    var lastTime = time.Now().Unix()\r\n\r\n    for {\r\n        now := time.Now().Unix()\r\n        di := water - (now-lastTime)*int64(rate) \/\/\u8ba1\u7b97\u8fc7\u53bb\u7684\u65f6\u95f4\u5185\u4ea7\u751f\u7684\u6c34\u91cf\r\n        if di > 0 {\r\n            water = di\r\n        } else {\r\n            water = 0\r\n        }\r\n        lastTime = now\r\n        if water &lt; capacity {\r\n            water++\r\n            doSomething()\r\n        } else {\r\n            fmt.Println(\"reach limit\")\r\n            time.Sleep(500 * time.Millisecond)\r\n        }\r\n    }\r\n}\r\n\r\nfunc doSomething() {\r\n    fmt.Printf(\"do something \\n\")\r\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u7b97\u6cd5\u4f9d\u7136\u6709\u4e00\u4e2a\u7f3a\u70b9\uff1a\u5c31\u662f\u65e0\u6cd5\u5e94\u5bf9\u7a81\u53d1\u6d41\u91cf\uff0c\u6240\u4ee5\u4e0b\u9762\u6709\u4e86 \u4ee4\u724c\u6876\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>4.\u4ee4\u724c\u6876\u7b97\u6cd5<\/p><\/blockquote>\n\n\n\n<p>\u4ee4\u724c\u6876\u7b97\u6cd5\u548c\u6f0f\u6876\u7b97\u6cd5\u7684\u65b9\u5411\u521a\u597d\u662f\u76f8\u53cd\u7684\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u56fa\u5b9a\u7684\u6876\uff0c\u6876\u91cc\u5b58\u653e\u7740\u4ee4\u724c\uff08token\uff09\u3002\u4e00\u5f00\u59cb\u6876\u662f\u7a7a\u7684\uff0c\u7cfb\u7edf\u6309\u56fa\u5b9a\u7684\u65f6\u95f4\uff08rate\uff09\u5f80\u6876\u91cc\u6dfb\u52a0\u4ee4\u724c\uff0c\u76f4\u5230\u6876\u91cc\u7684\u4ee4\u724c\u6570\u6ee1\uff0c\u591a\u4f59\u7684\u8bf7\u6c42\u4f1a\u88ab\u4e22\u5f03\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    var rate = 10        \/\/ \u4ee4\u724c\u751f\u6210\u901f\u5ea6\uff0c\u6bcf\u79d210\u4e2a\r\n    var tokens int64 = 0 \/\/ \u5f53\u524d\u4ee4\u724c\u6570\u91cf\r\n    var timeStamp = time.Now().Unix()\r\n\r\n    for {\r\n        now := time.Now().Unix()\r\n        di := tokens + (now-timeStamp)*int64(rate) \/\/\u8ba1\u7b97\u8fc7\u53bb\u7684\u65f6\u95f4\u6bb5\u4ea7\u751f\u7684\u4ee4\u724c\u6570\r\n        if di > 0 {\r\n            tokens = di\r\n        } else {\r\n            tokens = 0\r\n        }\r\n        timeStamp = now\r\n        if tokens &lt; 1 {\r\n            fmt.Println(\"reach limit\")\r\n            time.Sleep(500 * time.Millisecond)\r\n        } else {\r\n            tokens--\r\n            doSomething()\r\n        }\r\n    }\r\n}\r\n\r\nfunc doSomething() {\r\n    fmt.Printf(\"do something \\n\")\r\n}<\/code><\/pre>\n\n\n\n<p>\u8bf4\u5230\u8fd9\u4e2a\u7b97\u6cd5\uff0c\u5728Go\u91cc\u9762\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528chanel\u7ed3\u5408\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u5f0f\u8f7b\u677e\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    var ch = make(chan int64, 100)\r\n    go func() {\r\n        \/\/ \u5f00\u542f\u534f\u7a0b\uff0c\u6bcf\u9694100ms\u5f80\u7ba1\u9053\u91cc\u9762\u653e\u4e00\u4e2a\u6570\uff0c\u4e5f\u5c31\u662f\u5b9e\u73b0\u6bcf\u79d210s\u7684\u9650\u901f\u6548\u679c\r\n        var ticker = time.NewTicker(100 * time.Millisecond)\r\n        for {\r\n            select {\r\n            case &lt;-ticker.C:\r\n                ch &lt;- 1\r\n            }\r\n        }\r\n    }()\r\n    for {\r\n        &lt;-ch    \/\/\u5229\u7528\u7ba1\u9053\u7684\u963b\u585e\u7279\u6027\r\n        doSomething()\r\n    }\r\n}\r\n\r\nfunc doSomething() {\r\n    fmt.Printf(\" %s :do something \\n\", time.Now().Format(\"2006-01-02 15:04:05\"))\r\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>5.Go WaitGroup \u9650\u901f<\/p><\/blockquote>\n\n\n\n<p>\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u5f88\u591a\u65f6\u5019\u6211\u4eec\u5bf9\u9650\u901f\u8981\u6c42\u5e76\u4e0d\u662f\u5341\u5206\u7cbe\u51c6\uff0c\u6bd4\u5982\u8bf4\u6211\u4eec\u8981\u7528\u534f\u7a0b\u5e76\u53d1\u8c03\u4e00\u4e2a\u63a5\u53e3100\u6b21\uff0c\u4e00\u4e2a\u63a5\u53e3\u8017\u65f63s\uff0c\u4f46\u662f\u8fd9\u4e2a\u63a5\u53e3\u7684\u5e76\u53d1\u80fd\u529b\u4e0d\u5f3a\uff0c\u5982\u679c\u4e0d\u505a\u9650\u5236\u5f88\u53ef\u80fd\u628a\u63a5\u53e3\u6253\u57ae\uff0c\u5047\u8bbe\u8981\u6c42qps\u4f4e\u4e8e10\uff0c\u4e5f\u5c31\u8bf4\u6700\u591a\u6bcf\u79d210\u4e2a\u8bf7\u6c42\uff0c\u5229\u7528Go\u7684\u534f\u7a0b\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"sync\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    var wg = sync.WaitGroup{}\r\n    var count = 0\r\n    for i := 0; i &lt; 100; i++ {\r\n        wg.Add(1)\r\n        go doSomething(&amp;wg)\r\n        count++\r\n        if count > 10 {\r\n            wg.Wait()\r\n            count = 0\r\n        }\r\n    }\r\n}\r\n\r\nfunc doSomething(wg *sync.WaitGroup) {\r\n    defer wg.Done()\r\n    time.Sleep(time.Second * 3)\r\n    fmt.Printf(\" %s :do something \\n\", time.Now().Format(\"2006-01-02 15:04:05\"))\r\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>6.Go\u201c\u6807\u51c6\u5e93\u201d\u7684\u9650\u901f\u5305<\/p><\/blockquote>\n\n\n\n<p>\u8bf4\u662fGo\u6807\u51c6\u5e93\uff0c\u5176\u5b9e\u4e25\u683c\u6765\u8bf4\u5e76\u4e0d\u662f\uff0c\u8fd9\u662f<code>golang.org\/x<\/code>\u4e0b\u9762\u7684\u5305\uff0c\u4e00\u822c\u88ab\u8ba4\u4e3a\u662fGo\u7684\u4e00\u4e9b\u6b63\u5728\u5f00\u53d1\u6216\u8005\u9a8c\u8bc1\u4e2d\u7684\u5305\uff0c\u6211\u89c9\u5f97\u6709\u70b9\u7c7b\u4f3c\u5f00\u53d1\u7248\u7684\u5473\u9053\uff0c\u8fd9\u4e9b\u5305\u9700\u8981\u624b\u52a8\u5b89\u88c5\uff0c\u5e76\u4e0d\u662f\u76f4\u63a5\u53ef\u4ee5\u7528\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>These packages are part of the Go Project but outside the main Go tree. They are developed under looser compatibility requirements than the Go core.<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u9762\u7684\u5305\u8fd8\u662f\u633a\u591a\u7684\uff0c\u6709\u4e9b\u975e\u5e38\u5b9e\u7528\uff0c\u4f46\u662f\u5728\u56fd\u5185\u7528\u6709\u4e2a\u5c0f\u95ee\u9898\uff0c\u76f4\u63a5go get\u62c9\u4e0d\u4e0b\u6765\uff0c\u9700\u8981\u8bbe\u7f6e\u4e00\u4e0bgoproxy\uff0c\u5177\u4f53\u5c31\u4e0d\u7ec6\u8bf4\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go get golang.org\/x\/time\/rate<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u5305\u4f7f\u7528\u7684\u9650\u901f\u7b97\u6cd5\u5c31\u662f\u4ee4\u724c\u6876\u7b97\u6cd5\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"golang.org\/x\/time\/rate\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    limiter := rate.NewLimiter(5, 10)\r\n    for {\r\n        if limiter.Allow() {\r\n            fmt.Printf(\"%s: allow\\n\", time.Now().Format(\"2006-01-02 15:04:05\"))\r\n        }\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p><strong>NewLimiter<\/strong>\u67092\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662frate\u901f\u7387\uff0c\u5355\u4f4d\u65f6\u95f4\u5185\u7684\u4ea7\u751f\u7684\u4ee4\u724c\u6570\uff0c\u7b2c\u4e8c\u4e2a\u5219\u662f\u6876\u7684\u5bb9\u91cf\uff0c\u7136\u540e\u4f7f\u7528<strong>Allow()<\/strong>\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u610f\u601d\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Allow is shorthand for AllowN(time.Now(), 1).\r\nfunc (lim *Limiter) Allow() bool {\r\n    return lim.AllowN(time.Now(), 1)\r\n}\r\n\r\n\/\/ AllowN reports whether n events may happen at time now.\r\n\/\/ Use this method if you intend to drop \/ skip events that exceed the rate limit.\r\n\/\/ Otherwise use Reserve or Wait.\r\nfunc (lim *Limiter) AllowN(now time.Time, n int) bool {\r\n    return lim.reserveN(now, n, 0).ok\r\n}<\/code><\/pre>\n\n\n\n<p>\u5565\u610f\u601d\u5462\uff1f\u622a\u6b62\u5230\u67d0\u4e00\u65f6\u523b\uff0c\u76ee\u524d\u6876\u4e2d\u6570\u76ee\u662f\u5426\u81f3\u5c11\u4e3an\u4e2a\uff0c\u6ee1\u8db3\u5219\u8fd4\u56de true\uff0c\u540c\u65f6\u4ece\u6876\u4e2d\u6d88\u8d39n\u4e2atoken\uff0c\u8fd0\u884c\u7ed3\u679c\u662f\u7b2c\u4e00\u79d2\u6253\u5370\u4ea7\u751f15\u4e2a\u8bb0\u5f55\uff0c\u7136\u540e\u662f\u6bcf\u79d25\u4e2a\u8bb0\u5f55\uff0c\u6709\u4eba\u8bf4\u4e3a\u4ec0\u4e48\u7b2c\u4e00\u79d215\u4e2a\uff0c\u90a3\u662f\u56e0\u4e3a\u6876\u91cc\u9762\u521d\u59cb\u5316\u7684\u65f6\u5019\u670910\u4e2a\uff0c\u4e5f\u5c31\u662f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u8bf4\u51e0\u53e5\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5bf9\u4e8eWeb\u5e94\u7528\u7684\u9650\u901f\u4e00\u822c\u90fd\u5728\u7f51\u5173\u5904\u7406\uff0c\u6bd4\u5982nginx\u5c31\u6709\u9650\u901f\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\uff0c\u5305\u62ec\u6309\u8fde\u63a5\u6570\u9650\u901f(ngx_http_limit_conn_module)\u3001\u6309\u8bf7\u6c42\u901f\u7387\u9650\u901f(ngx_http_limit_req_module)\uff0c\u57fa\u672c\u4e0a\u53ef\u4ee5\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\u4e8e\uff1ahttps:\/\/wangbjun.site\/2020\/coding\/golang\/limiter.ht [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/posts\/287"}],"collection":[{"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":1,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":289,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions\/289"}],"wp:attachment":[{"href":"http:\/\/blog.kijk.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}