{"id":208230,"date":"2025-07-08T09:28:37","date_gmt":"2025-07-08T01:28:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208230\/"},"modified":"2025-07-08T09:28:37","modified_gmt":"2025-07-08T01:28:37","slug":"%e5%9c%a8-echo-go-%e4%b8%8a%e5%ae%9e%e7%8e%b0%e7%89%b9%e5%ae%9a%e8%b7%af%e7%ba%bf%e8%b6%85%e6%97%b6%e7%9a%84%e6%9c%80%e4%bd%b3%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208230\/","title":{"rendered":"\u5728 Echo\/Go \u4e0a\u5b9e\u73b0\u7279\u5b9a\u8def\u7ebf\u8d85\u65f6\u7684\u6700\u4f73\u65b9\u6cd5"},"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>\u5728 Echo\/Go \u4e0a\u5b9e\u73b0\u7279\u5b9a\u8def\u7ebf\u8d85\u65f6\u7684\u6700\u4f73\u65b9\u6cd5<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 15:45:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728Golang\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u5728 Echo\/Go \u4e0a\u5b9e\u73b0\u7279\u5b9a\u8def\u7ebf\u8d85\u65f6\u7684\u6700\u4f73\u65b9\u6cd5\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u77e5\u9053\u4e3a echo \u5e93\u4e0a\u7684\u7279\u5b9a\u8def\u7ebf\u8bbe\u7f6e\u8d85\u65f6\u7684\u6700\u4f73\u65b9\u6cd5\u3002<\/p>\n<p>\u6211\u7528<strong>context.withtimeout<\/strong>\u5b9e\u73b0\u4e86\u8d85\u65f6\uff0c\u4f46\u4f7f\u7528\u4e86\u51e0\u4e2a\u51fd\u6570\u6765\u5c01\u88c5\u4e0a\u4e0b\u6587\uff0c\u6211\u8ba4\u4e3a\u8fd9\u662f\u9519\u8bef\u7684\u3002<\/p>\n<pre>ctx, cancel := context.WithTimeout(ctx, 30*time.Second)<\/pre>\n<p>\u662f\u5426\u6709\u4efb\u4f55\u4e2d\u95f4\u4ef6\u6216\u66f4\u597d\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<h2>\u5411\u670d\u52a1\u5668 context.context \u6dfb\u52a0\u8d85\u65f6<\/h2>\n<p>\u5728\u4e0d\u786e\u5207\u77e5\u9053\u60a8\u8981\u505a\u4ec0\u4e48\u7684\u60c5\u51b5\u4e0b\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\u6709\u70b9\u68d8\u624b\u3002\u6211\u5c06\u9996\u5148\u56de\u7b54\u5982\u4f55\u4f7f\u7528 <code>withtimeout<\/code> \u548c\u4e2d\u95f4\u4ef6\u6765\u5904\u7406\u4e0a\u4e0b\u6587\u5305\u88c5\u3002<\/p>\n<p>\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u6dfb\u52a0\/\u4fee\u6539\u8bf7\u6c42\u4e0a\u4e0b\u6587\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>func timeoutmiddleware(timeout time.duration, next func(w http.responsewriter, req *http.request)) func(w http.responsewriter, req *http.request) {\n    return func(w http.responsewriter, req *http.request) {\n        \/\/ wrap the existing context from the request\n        ctx, cancel := context.withtimeout(req.context(), timeout)\n        \/\/ always do this to clean up contexts, otherwise they'll hang out\n        \/\/ and gather since they are blocked go rountines\n        defer cancel()\n\n        \/\/ put the new context into the request\n        req = req.withcontext(ctx)\n\n        \/\/ pass the modified request forward\n        next(w, req)\n\n        \/\/ handle any ctx timeout issues in the general middleware\n        if err := ctx.err(); err != nil {\n            if errors.is(err, context.deadlineexceeded) {\n                log.println(\"http request timed out\")\n                w.write([]byte(\"timed out\"))\n            }\n        }\n    }\n}\n<\/pre>\n<p>\u95ee\u9898\u662f <code>next(w, req)<\/code> \u9700\u8981\u60a8\u7684 http \u5904\u7406\u7a0b\u5e8f\u6765\u5904\u7406\u4e0a\u4e0b\u6587\u8d85\u65f6\u3002\u5982\u679c http \u5904\u7406\u7a0b\u5e8f\u5ffd\u7565\u4e0a\u4e0b\u6587\uff0c\u5219\u4e0d\u4f1a\u8d85\u65f6\u3002\u6bd4\u5982\u8fd9\u6837\uff1a<\/p>\n<pre>func endless(w http.responsewriter, req *http.request) {\n    ctx := req.context()\n    \/\/ just a busy loop....\n    for {\n        select {\n        case &lt;-ctx.done():\n            \/\/ exit the handler if the context is done(), even if our function is not.\n            return\n        default:\n            fmt.println(\"wait\")\n            time.sleep(1 * time.second)\n        }\n    }\n}\n<\/pre>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u8fdb\u884c\u6570\u636e\u5e93\u8c03\u7528\u6216\u9700\u8981\u5728\u5904\u7406\u7a0b\u5e8f\u4e2d\u82b1\u8d39\u65f6\u95f4\u7684\u64cd\u4f5c\uff0c\u901a\u5e38\u6570\u636e\u5e93 api \u4f1a\u63a5\u53d7 <code>context.context<\/code> \u4ee5\u4fbf\u5728\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\u65f6\u63d0\u524d\u4e2d\u6b62\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u6b64\u89e3\u51b3\u65b9\u6848\u4e3a\u8fdb\u5165\u5904\u7406\u7a0b\u5e8f\u7684\u8bf7\u6c42\u4e0a\u4e0b\u6587\u6dfb\u52a0\u4e86\u8d85\u65f6\uff0c\u60a8\u4ecd\u7136\u9700\u8981\u7ba1\u7406\u8be5\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<h2>\u5ba2\u6237\u7aef\u8d85\u65f6<\/h2>\n<p>\u60a8\u59cb\u7ec8\u53ef\u4ee5\u5c06\u8d85\u65f6\u6dfb\u52a0\u5230\u60a8\u7684\u8bf7\u6c42\u4e2d\uff1a<\/p>\n<pre>    client := http.Client{\n        Timeout: time.Second,\n    }\n    client.Do(...)\n<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5728 Echo\/Go \u4e0a\u5b9e\u73b0\u7279\u5b9a\u8def\u7ebf\u8d85\u65f6\u7684\u6700\u4f73\u65b9\u6cd5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\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-208230","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208230","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=208230"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208230\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}