{"id":207795,"date":"2025-07-08T15:00:01","date_gmt":"2025-07-08T07:00:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207795\/"},"modified":"2025-07-08T15:00:01","modified_gmt":"2025-07-08T07:00:01","slug":"%e4%bd%bf%e7%94%a8didip-tollbooth%e9%99%90%e5%88%b6%e6%af%8f%e5%b0%8f%e6%97%b6%e6%9c%80%e5%a4%a7%e8%af%b7%e6%b1%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207795\/","title":{"rendered":"\u4f7f\u7528\u201cdidip\/tollbooth\u201d\u9650\u5236\u6bcf\u5c0f\u65f6\u6700\u5927\u8bf7\u6c42\u6570"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4f7f\u7528\u201cdidip\/tollbooth\u201d\u9650\u5236\u6bcf\u5c0f\u65f6\u6700\u5927\u8bf7\u6c42\u6570<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 17:45:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528\u201cdidip\/tollbooth\u201d\u9650\u5236\u6bcf\u5c0f\u65f6\u6700\u5927\u8bf7\u6c42\u6570\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5bf9\u901f\u7387\u9650\u5236\u4e0d\u719f\u6089\uff0c\u60f3\u8981\u4f7f\u7528\u6536\u8d39\u7ad9\u6765\u9650\u5236 http \u8bf7\u6c42\u3002<\/p>\n<p>\u6211\u8fd8\u9605\u8bfb\u4e86\u7ef4\u57fa\u767e\u79d1\u4e0a\u7684\u4ee4\u724c\u6876\u7b97\u6cd5\u9875\u9762\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u5e0c\u671b\u5c06\u6700\u5927\u5e76\u53d1\u8bf7\u6c42\u6570\u9650\u5236\u4e3a <code>10<\/code>\uff0c\u65e0\u8bba\u8bf7\u6c42 ip \u4e3a\u4f55\uff0c\u5e76\u6839\u636e\u8bf7\u6c42 ip \u5c06\u6700\u5927\u7a81\u53d1\u5927\u5c0f\u8bbe\u7f6e\u4e3a <code>3<\/code>\u3002<\/p>\n<p>\u6ce8\u610f\uff1a<code>10<\/code> \u548c <code>3<\/code> \u53ea\u662f\u4e3a\u4e86\u4f7f\u901f\u7387\u9650\u5236\u66f4\u5bb9\u6613\u89c2\u5bdf\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u6211\u7684\u4ee3\u7801\uff0c\u57fa\u4e8e <code>tollbooth<\/code> \u7684 github \u9875\u9762\u4e0a\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"net\/http\"\n    \"time\"\n\n    \"github.com\/didip\/tollbooth\/v7\"\n    \"github.com\/didip\/tollbooth\/v7\/limiter\"\n)\n\nfunc main() {\n    lmt := tollbooth.newlimiter(3, &amp;limiter.expirableoptions{defaultexpirationttl: time.hour})\n\n    http.handle(\"\/\", tollbooth.limitfunchandler(lmt, hellohandler))\n    http.listenandserve(\":8080\", nil)\n}\n\nfunc hellohandler(w http.responsewriter, req *http.request) {\n    w.write([]byte(\"hello, world!\"))\n}<\/pre>\n<p>\u6211\u901a\u8fc7\u5feb\u901f\u8fde\u7eed\u591a\u6b21\u8fd0\u884c <code>curl -i localhost:8080<\/code> \u6765\u6d4b\u8bd5\u4ee3\u7801\uff0c\u6bcf\u5f53\u8d85\u51fa\u6211\u8bbe\u7f6e\u7684\u901f\u7387\u9650\u5236\u65f6\uff0c\u6211\u90fd\u4f1a\u6536\u5230 <code>http\/1.1 429 too many requests<\/code> \u9519\u8bef\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u6211\u7684\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\n<p>\u5982\u4f55\u4f7f\u7528 <code>tollbooth<\/code> \u5c06\u6700\u5927\u5e76\u53d1\u8bf7\u6c42\u6570\u9650\u5236\u4e3a <code>10<\/code> \u4e4b\u7c7b\u7684\u5185\u5bb9\uff1f\u8fd9\u6837\u505a\u6709\u610f\u4e49\u5417\uff1f\u6211\u8ba4\u4e3a\u786e\u5b9e\u5982\u6b64\uff0c\u56e0\u4e3a\u4ec5\u57fa\u4e8e ip \u7684\u901f\u7387\u9650\u5236\u542c\u8d77\u6765\u50cf\u662f\u5f53\u592a\u591a ip \u540c\u65f6\u8bbf\u95ee\u670d\u52a1\u5668\u65f6\uff0c\u670d\u52a1\u5668\u4ecd\u7136\u53ef\u80fd\u4f1a\u51fa\u73b0\u5185\u5b58\u4e0d\u8db3\u7684\u60c5\u51b5\u3002<\/p>\n<\/li>\n<li>\n<p>\u6211\u662f\u5426\u6b63\u786e\u5730\u63a5\u8fd1\u4e86\u901f\u7387\u9650\u5236\uff0c\u6216\u8005\u6211\u9519\u8fc7\u4e86\u4ec0\u4e48\uff1f\u4e5f\u8bb8\u65e0\u8bba\u8d1f\u8f7d\u5747\u8861\u5668\u4e0e\u4e91\u4e2d\u7684\u5e94\u7528\u7a0b\u5e8f\u4e00\u8d77\u4f7f\u7528\uff0c\u90fd\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\uff1f<\/p>\n<\/li>\n<\/ol>\n<p><em><strong>\u66f4\u65b0\uff1a<\/strong><\/em>\u8fd9\u662f\u6211\u57fa\u4e8e woody1193 \u7684\u7b54\u6848\u7684\u5de5\u4f5c\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"net\/http\"\n    \"sync\"\n    \"time\"\n\n    \"github.com\/didip\/tollbooth\/v7\"\n    \"github.com\/didip\/tollbooth\/v7\/limiter\"\n)\n\nfunc main() {\n    ipLimiter := tollbooth.NewLimiter(3, &amp;limiter.ExpirableOptions{DefaultExpirationTTL: time.Hour})\n    globalLimiter := NewConcurrentLimiter(10)\n\n    http.Handle(\"\/\", globalLimiter.LimitConcurrentRequests(ipLimiter, HelloHandler))\n    http.ListenAndServe(\":8080\", nil)\n}\n\nfunc HelloHandler(w http.ResponseWriter, req *http.Request) {\n    w.Write([]byte(\"Hello, World!\"))\n}\n\ntype ConcurrentLimiter struct {\n    max     int\n    current int\n    mut     sync.Mutex\n}\n\nfunc NewConcurrentLimiter(limit int) *ConcurrentLimiter {\n    return &amp;ConcurrentLimiter{\n        max: limit,\n    }\n}\n\nfunc (limiter *ConcurrentLimiter) LimitConcurrentRequests(lmt *limiter.Limiter,\n    handler func(http.ResponseWriter, *http.Request)) http.Handler {\n\n    middle := func(w http.ResponseWriter, r *http.Request) {\n\n        limiter.mut.Lock()\n        maxHit := limiter.current == limiter.max\n\n        if maxHit {\n            limiter.mut.Unlock()\n            http.Error(w, http.StatusText(429), http.StatusTooManyRequests)\n            return\n        }\n\n        limiter.current += 1\n        limiter.mut.Unlock()\n\n        defer func() {\n            limiter.mut.Lock()\n            limiter.current -= 1\n            limiter.mut.Unlock()\n        }()\n\n        \/\/ There's no rate-limit error, serve the next handler.\n        handler(w, r)\n    }\n\n    return tollbooth.LimitHandler(lmt, http.HandlerFunc(middle))\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6536\u8d39\u7ad9\u4f3c\u4e4e\u4e0d\u63d0\u4f9b\u60a8\u6b63\u5728\u5bfb\u627e\u7684\u529f\u80fd\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u81ea\u5df1\u63a8\u51fa\uff1a<\/p>\n<pre>type concurrentlimiter struct {\n    max int\n    current int\n    mut sync.mutex\n}\n\nfunc newconcurrentlimiter(limit int) *concurrentlimiter {\n    return &amp;concurrentlimiter {\n        max: limit,\n        mut: new(sync.mutex),\n    }\n}\n\nfunc (limiter *concurrentlimiter) limitconcurrentrequests(lmt *limiter.limiter, \n    next http.handler) http.handler {\n\n    middle := func(w http.responsewriter, r *http.request) {\n\n        limiter.mut.lock()\n        maxhit := limiter.current == limiter.max\n        if maxhit {\n            limiter.mut.unlock()\n            httperror := \/\/ insert your http error here\n            return\n        }\n\n        limiter.current += 1\n        limiter.mut.unlock()\n\n        defer func() {\n            limiter.mut.lock()\n            limiter.current -= 1\n            limiter.mut.unlock()\n        }()\n\n        \/\/ there's no rate-limit error, serve the next handler.\n        next.servehttp(w, r)\n    }\n\n    return tollbooth.limithandler(lmt, http.handlerfunc(middle))\n}<\/pre>\n<p>\u7136\u540e\uff0c\u5728\u60a8\u7684\u8bbe\u7f6e\u4e2d\u60a8\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<pre>http.Handle(\"\/\", NewConcurrentLimiter(10).LimitConcurrentRequests(HelloHandler))<\/pre>\n<p>\u6b64\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\u662f\u7ef4\u62a4\u4e00\u4e2a\u503c\uff0c\u8be5\u503c\u63cf\u8ff0 api \u5f53\u524d\u6b63\u5728\u5904\u7406\u7684\u8bf7\u6c42\u6570\u91cf\uff0c\u5e76\u5728\u8fbe\u5230\u6700\u5927\u503c\u65f6\u8fd4\u56de\u9519\u8bef\u3002 <code>mutex<\/code> \u7528\u4e8e\u786e\u4fdd\u65e0\u8bba\u5e76\u53d1\u8bf7\u6c42\u5982\u4f55\uff0c\u8be5\u503c\u90fd\u4f1a\u66f4\u65b0\u3002<\/p>\n<p>\u7531\u4e8e tollbooth \u5904\u7406\u6b64\u7c7b\u51fd\u6570\u7684\u65b9\u5f0f\uff08\u5373\u5b83\u4e0d\u4f5c\u4e3a\u4e2d\u95f4\u4ef6\u8fd0\u884c\uff09\uff0c\u6211\u5fc5\u987b\u5c06 <code>tollbooth.limiter<\/code> \u6ce8\u5165\u5230\u6211\u7f16\u5199\u7684\u9650\u5236\u5668\u4e2d\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4f7f\u7528\u201cdidip\/tollbooth\u201d\u9650\u5236\u6bcf\u5c0f\u65f6\u6700\u5927\u8bf7\u6c42\u6570\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207795","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207795"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207795\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}