{"id":207877,"date":"2025-07-08T11:24:12","date_gmt":"2025-07-08T03:24:12","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207877\/"},"modified":"2025-07-08T11:24:12","modified_gmt":"2025-07-08T03:24:12","slug":"%e5%bd%93%e9%80%9a%e9%81%93%e5%85%b3%e9%97%ad%e6%97%b6%ef%bc%8c%e4%bb%a5%e6%8e%a5%e6%94%b6%e9%80%9a%e9%81%93%e4%b8%ba%e5%8f%82%e6%95%b0%e7%9a%84-goroutine-%e6%98%af%e5%90%a6%e4%bc%9a%e5%81%9c%e6%ad%a2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207877\/","title":{"rendered":"\u5f53\u901a\u9053\u5173\u95ed\u65f6\uff0c\u4ee5\u63a5\u6536\u901a\u9053\u4e3a\u53c2\u6570\u7684 goroutine \u662f\u5426\u4f1a\u505c\u6b62\uff1f"},"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>\u5f53\u901a\u9053\u5173\u95ed\u65f6\uff0c\u4ee5\u63a5\u6536\u901a\u9053\u4e3a\u53c2\u6570\u7684 goroutine \u662f\u5426\u4f1a\u505c\u6b62\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 12:03:42<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5f53\u901a\u9053\u5173\u95ed\u65f6\uff0c\u4ee5\u63a5\u6536\u901a\u9053\u4e3a\u53c2\u6570\u7684 goroutine \u662f\u5426\u4f1a\u505c\u6b62\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4e00\u76f4\u5728\u9605\u8bfb\u300a\u7528 go \u6784\u5efa\u5fae\u670d\u52a1\u300b\uff0c\u4e66\u4e2d\u4ecb\u7ecd\u4e86\u7528\u4e8e\u5904\u7406\u8d85\u65f6\u7684 <code>apache\/go-resiliency\/deadline<\/code> \u5305\u3002<\/p>\n<p><code>deadline.go<\/code><\/p>\n<pre>\/\/ package deadline implements the deadline (also known as \"timeout\") resiliency pattern for go.\npackage deadline\n\nimport (\n    \"errors\"\n    \"time\"\n)\n\n\/\/ errtimedout is the error returned from run when the deadline expires.\nvar errtimedout = errors.new(\"timed out waiting for function to finish\")\n\n\/\/ deadline implements the deadline\/timeout resiliency pattern.\ntype deadline struct {\n    timeout time.duration\n}\n\n\/\/ new constructs a new deadline with the given timeout.\nfunc new(timeout time.duration) *deadline {\n    return &amp;deadline{\n        timeout: timeout,\n    }\n}\n\n\/\/ run runs the given function, passing it a stopper channel. if the deadline passes before\n\/\/ the function finishes executing, run returns errtimeout to the caller and closes the stopper\n\/\/ channel so that the work function can attempt to exit gracefully. it does not (and cannot)\n\/\/ simply kill the running function, so if it doesn't respect the stopper channel then it may\n\/\/ keep running after the deadline passes. if the function finishes before the deadline, then\n\/\/ the return value of the function is returned from run.\nfunc (d *deadline) run(work func(&lt;-chan struct{}) error) error {\n    result := make(chan error)\n    stopper := make(chan struct{})\n\n    go func() {\n        result &lt;- work(stopper)\n    }()\n\n    select {\n    case ret := &lt;-result:\n        return ret\n    case &lt;-time.after(d.timeout):\n        close(stopper)\n        return errtimedout\n    }\n}<\/pre>\n<p><code>deadline_test.go<\/code><\/p>\n<pre>package deadline\n\nimport (\n    \"errors\"\n    \"testing\"\n    \"time\"\n)\n\nfunc takesfivemillis(stopper &lt;-chan struct{}) error {\n    time.sleep(5 * time.millisecond)\n    return nil\n}\n\nfunc takestwentymillis(stopper &lt;-chan struct{}) error {\n    time.sleep(20 * time.millisecond)\n    return nil\n}\n\nfunc returnserror(stopper &lt;-chan struct{}) error {\n    return errors.new(\"foo\")\n}\n\nfunc testdeadline(t *testing.t) {\n    dl := new(10 * time.millisecond)\n\n    if err := dl.run(takesfivemillis); err != nil {\n        t.error(err)\n    }\n\n    if err := dl.run(takestwentymillis); err != errtimedout {\n        t.error(err)\n    }\n\n    if err := dl.run(returnserror); err.error() != \"foo\" {\n        t.error(err)\n    }\n\n    done := make(chan struct{})\n    err := dl.run(func(stopper &lt;-chan struct{}) error {\n        &lt;-stopper\n        close(done)\n        return nil\n    })\n    if err != errtimedout {\n        t.error(err)\n    }\n    &lt;-done\n}\n\nfunc exampledeadline() {\n    dl := new(1 * time.second)\n\n    err := dl.run(func(stopper &lt;-chan struct{}) error {\n        \/\/ do something possibly slow\n        \/\/ check stopper function and give up if timed out\n        return nil\n    })\n\n    switch err {\n    case errtimedout:\n        \/\/ execution took too long, oops\n    default:\n        \/\/ some other error\n    }\n}<\/pre>\n<h2>\u7b2c\u4e00\u4e2a\u95ee\u9898<\/h2>\n<pre>\/\/ in deadline_test.go\nif err := dl.run(takestwentymillis); err != errtimedout {\n    t.error(err)\n}<\/pre>\n<p>\u6211\u65e0\u6cd5\u7406\u89e3\u4e0a\u8ff0\u4ee3\u7801\u7684\u6267\u884c\u6d41\u7a0b\u3002\u636e\u6211\u4e86\u89e3\uff0c\u56e0\u4e3a<code>takestwentymillis<\/code>\u51fd\u6570\u4f11\u7720\u7684\u65f6\u95f4\u8d85\u8fc7\u4e86\u8bbe\u7f6e\u7684\u8d85\u65f6\u65f6\u95f410\u6beb\u79d2\uff0c<\/p>\n<pre>\/\/ in deadline.go\ncase &lt;-time.after(d.timeout):\n    close(stopper)\n    return errtimedout<\/pre>\n<p>time.after \u53d1\u51fa\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u4e14\u9009\u62e9\u8fd9\u79cd\u60c5\u51b5\u3002\u7136\u540e\u5173\u95edstopper\u901a\u9053\u5e76\u8fd4\u56deerrtimeout\u3002<\/p>\n<p>\u6211\u4e0d\u660e\u767d\u7684\u662f\uff0c<strong><em>\u5173\u95ed\u505c\u6b62\u5668\u901a\u9053\u4f1a\u5bf9\u53ef\u80fd\u4ecd\u5728\u8fd0\u884c\u7684\u533f\u540d goroutine \u4ea7\u751f\u4ec0\u4e48\u4f5c\u7528<\/em><\/strong> \u6211\u8ba4\u4e3a\uff0c\u5f53 stopper \u901a\u9053\u5173\u95ed\u65f6\uff0c\u4e0b\u9762\u7684 goroutine \u53ef\u80fd\u4ecd\u5728\u8fd0\u884c\u3002<\/p>\n<pre>go func() {\n        result &lt;- work(stopper)\n    }()<\/pre>\n<p>\uff08\u5982\u679c\u6211\u9519\u4e86\uff0c\u8bf7\u7ea0\u6b63\u6211\uff09\u6211\u8ba4\u4e3a\u5728 <code>close(stopper)<\/code> \u4e4b\u540e\uff0c\u8fd9\u4e2a goroutine \u5c06\u8c03\u7528 <code>takestwentymillis<\/code>(=work function)\uff0c\u5e76\u4ee5 stopper \u901a\u9053\u4f5c\u4e3a\u53c2\u6570\u3002\u8be5\u51fd\u6570\u5c06\u7ee7\u7eed\u8fd0\u884c\u5e76\u4f11\u7720 20 \u6beb\u79d2\u5e76\u8fd4\u56de nil \u4ee5\u4f20\u9012\u5230\u7ed3\u679c\u901a\u9053\u3002 main() \u5230\u8fd9\u91cc\u5c31\u7ed3\u675f\u4e86\uff0c\u5bf9\u5417\uff1f<\/p>\n<p>\u6211\u4e0d\u660e\u767d\u5728\u8fd9\u91cc\u5173\u95ed\u585e\u5b50\u901a\u9053\u6709\u4ec0\u4e48\u610f\u4e49\u3002 <code>takestwentymillis<\/code> \u51fd\u6570\u4f3c\u4e4e\u5e76\u6ca1\u6709\u4f7f\u7528\u51fd\u6570\u4f53\u5185\u7684\u901a\u9053:(\u3002<\/p>\n<h2>\u7b2c\u4e8c\u4e2a\u95ee\u9898<\/h2>\n<pre>\/\/ in deadline_test.go within testdeadline()\ndone := make(chan struct{})\nerr := dl.run(func(stopper &lt;-chan struct{}) error {\n    &lt;-stopper\n    close(done)\n    return nil\n})\nif err != errtimedout {\n    t.error(err)\n}\n&lt;-done<\/pre>\n<p>\u8fd9\u662f\u6211\u4e0d\u5b8c\u5168\u7406\u89e3\u7684\u90e8\u5206\u3002\u6211\u8ba4\u4e3a\u5f53 <code>dl.run<\/code> \u8fd0\u884c\u65f6\uff0c\u585e\u5b50\u901a\u9053\u88ab\u521d\u59cb\u5316\u3002\u4f46\u662f\u56e0\u4e3a\u585e\u5b50\u901a\u9053\u4e2d\u6ca1\u6709\u503c\uff0c\u6240\u4ee5\u51fd\u6570\u8c03\u7528\u5c06\u88ab\u963b\u6b62\u5728 <code>&lt;-stopper<\/code>&#8230;\u4f46\u662f\u56e0\u4e3a\u6211\u4e0d\u7406\u89e3\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6240\u4ee5\u6211\u4e0d\u660e\u767d\u4e3a\u4ec0\u4e48\u8fd9\u6bb5\u4ee3\u7801\u5b58\u5728\uff08\u5373\u8fd9\u6bb5\u4ee3\u7801\u662f\u4ec0\u4e48\uff09\u6b63\u5728\u5c1d\u8bd5\u6d4b\u8bd5\uff0c\u4ee5\u53ca\u5b83\u662f\u5982\u4f55\u6267\u884c\u7684\uff0c\u7b49\u7b49\uff09\u3002<\/p>\n<h2>\u5173\u4e8e\u7b2c\u4e8c\u4e2a\u95ee\u9898\u7684\u7b2c\u4e09\u4e2a\uff08\u9644\u52a0\uff09\u95ee\u9898<\/h2>\n<p>\u6240\u4ee5\u6211\u660e\u767d\uff0c\u5f53\u7b2c\u4e8c\u4e2a\u95ee\u9898\u4e2d\u7684 <code>run<\/code> \u51fd\u6570\u89e6\u53d1\u585e\u5b50\u901a\u9053\u5173\u95ed\u65f6\uff0c\u5de5\u4f5c\u51fd\u6570\u4f1a\u6536\u5230\u4fe1\u53f7\u3002\u7136\u540eworker\u5173\u95eddone\u901a\u9053\u5e76\u8fd4\u56denil\u3002 \u6211\u4f7f\u7528 delve(=go debugger) \u6765\u67e5\u770b\u8fd9\u4e00\u70b9\uff0cgdb \u5c06\u6211\u5e26\u5230 <code>deadline.go<\/code> \u4e2d <code>return nil<\/code> \u884c\u4e4b\u540e\u7684 goroutine\u3002 <\/p>\n<pre>err := dl.run(func(stopper &lt;-chan struct{}) error {\n        &lt;-stopper\n        close(done)\n--&gt;     return nil   \n    })<\/pre>\n<p>\u8f93\u5165 n \u8fdb\u5165\u4e0b\u4e00\u884c\u540e\uff0cdelve \u5c06\u6211\u5e26\u5230\u8fd9\u91cc<\/p>\n<pre>go func() {\n--&gt;             result &lt;- work(stopper)\n            }()<\/pre>\n<p>\u8fd9\u4e2a\u8fc7\u7a0b\u5728\u8fd9\u91cc\u7ed3\u675f\uff0c\u56e0\u4e3a\u5f53\u6211\u518d\u6b21\u8f93\u5165 n \u65f6\uff0c\u547d\u4ee4\u884c\u63d0\u793a pass \u5e76\u4e14\u8fc7\u7a0b\u9000\u51fa\u3002\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u5728\u8fd9\u91cc\u7ed3\u675f\uff1f <code>work(stopper)<\/code> \u4f3c\u4e4e\u8fd4\u56de <code>nil<\/code>\uff0c\u7136\u540e\u5e94\u8be5\u5c06\u5176\u4f20\u9012\u5230\u7ed3\u679c\u901a\u9053\uff0c\u5bf9\u5427\uff1f\u4f46\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u8fd9\u4e00\u884c\u4f3c\u4e4e\u6ca1\u6709\u6267\u884c\u3002<\/p>\n<p>\u6211\u77e5\u9053\u4e3b goroutine\uff0c\u5373 <code>run<\/code> \u51fd\u6570\uff0c\u5df2\u7ecf\u8fd4\u56de\u4e86 errtimedout\u3002\u6240\u4ee5\u6211\u60f3\u8fd9\u4e0e\u6b64\u6709\u5173\uff1f <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p><strong>\u7b2c\u4e00\u4e2a\u95ee\u9898<\/strong><\/p>\n<p><code>stopper<\/code> \u901a\u9053\u7684\u4f7f\u7528\u662f\u5411\u51fd\u6570\u53d1\u51fa\u4fe1\u53f7\uff0c\u4f8b\u5982<code>takestwentymillis<\/code> \u8868\u793a\u5df2\u8fbe\u5230\u622a\u6b62\u65e5\u671f\uff0c\u8c03\u7528\u8005\u4e0d\u518d\u5173\u5fc3\u5176\u7ed3\u679c\u3002\u901a\u5e38\u8fd9\u610f\u5473\u7740\u50cf <code>takestwentymillis<\/code> \u8fd9\u6837\u7684\u5de5\u4f5c\u51fd\u6570\u5e94\u8be5\u68c0\u67e5 <code>stopper<\/code> \u901a\u9053\u662f\u5426\u5df2\u7ecf\u5173\u95ed\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u53d6\u6d88\u5b83\u7684\u5de5\u4f5c\u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u68c0\u67e5 <code>stopper<\/code> \u901a\u9053\u4ecd\u7136\u662f\u5de5\u4f5c\u51fd\u6570\u7684\u9009\u62e9\u3002\u5b83\u53ef\u80fd\u4f1a\u4e5f\u53ef\u80fd\u4e0d\u4f1a\u68c0\u67e5\u901a\u9053\u3002<\/p>\n<pre>func takestwentymillis(stopper &lt;-chan struct{}) error {\n    for i := 0; i &lt; 20; i++ {\n        select {\n        case &lt;-stopper:\n            \/\/ caller doesn't care anymore might as well stop working\n            return nil\n        case &lt;-time.after(time.second): \/\/ simulating work\n        }\n    }\n    \/\/ work is done\n    return nil\n}<\/pre>\n<p><strong>\u7b2c\u4e8c\u4e2a\u95ee\u9898<\/strong><\/p>\n<p><code>deadline.run()<\/code> \u8fd9\u90e8\u5206\u5c06\u5173\u95ed\u585e\u5b50\u901a\u9053\u3002<\/p>\n<pre>case &lt;-time.After(d.timeout):\n    close(stopper)<\/pre>\n<p>\u8bfb\u53d6\u5173\u95ed\u901a\u9053 (<code>&lt;-stopper<\/code>) \u5c06\u7acb\u5373\u8fd4\u56de\u8be5\u901a\u9053\u7684\u96f6\u503c\u3002\u6211\u8ba4\u4e3a\u8fd9\u53ea\u662f\u6d4b\u8bd5\u6700\u7ec8\u8d85\u65f6\u7684\u5de5\u4f5c\u51fd\u6570\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5f53\u901a\u9053\u5173\u95ed\u65f6\uff0c\u4ee5\u63a5\u6536\u901a\u9053\u4e3a\u53c2\u6570\u7684 goroutine \u662f\u5426\u4f1a\u505c\u6b62\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/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-207877","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207877","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=207877"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207877\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}