{"id":207689,"date":"2025-07-08T10:00:23","date_gmt":"2025-07-08T02:00:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207689\/"},"modified":"2025-07-08T10:00:23","modified_gmt":"2025-07-08T02:00:23","slug":"%e5%b0%86-errgroup-%e5%b5%8c%e5%a5%97%e5%9c%a8%e4%b8%80%e5%a0%86-goroutine-%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207689\/","title":{"rendered":"\u5c06 errgroup \u5d4c\u5957\u5728\u4e00\u5806 goroutine \u4e2d"},"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>\u5c06 errgroup \u5d4c\u5957\u5728\u4e00\u5806 goroutine \u4e2d<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 17:51: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>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u5c06 errgroup \u5d4c\u5957\u5728\u4e00\u5806 goroutine \u4e2d\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5bf9 golang \u53ca\u5176\u5e76\u53d1\u539f\u5219\u76f8\u5f53\u964c\u751f\u3002\u6211\u7684\u7528\u4f8b\u6d89\u53ca\u5bf9\u4e00\u6279\u5b9e\u4f53\u6267\u884c\u591a\u4e2a http \u8bf7\u6c42\uff08\u9488\u5bf9\u5355\u4e2a\u5b9e\u4f53\uff09\u3002\u5982\u679c\u67d0\u4e2a\u5b9e\u4f53\u7684\u4efb\u4f55 http \u8bf7\u6c42\u5931\u8d25\uff0c\u6211\u9700\u8981\u505c\u6b62\u5b83\u7684\u6240\u6709\u5e76\u884c http \u8bf7\u6c42\u3002\u53e6\u5916\uff0c\u6211\u5fc5\u987b\u7ba1\u7406\u56e0\u9519\u8bef\u800c\u5931\u8d25\u7684\u5b9e\u4f53\u7684\u6570\u91cf\u3002\u6211\u6b63\u5728\u5c1d\u8bd5\u5728\u5b9e\u4f53 goroutine \u5185\u5b9e\u73b0 errorgroup\uff0c\u8fd9\u6837\uff0c\u5982\u679c\u5355\u4e2a\u5b9e\u4f53\u7684\u4efb\u4f55 http \u8bf7\u6c42\u5931\u8d25\uff0c\u5219 errorgroup \u5c06\u7ec8\u6b62\u5e76\u5411\u5176\u7236 goroutine \u8fd4\u56de\u9519\u8bef\u3002\u4f46\u6211\u4e0d\u77e5\u9053\u5982\u4f55\u7ef4\u62a4\u9519\u8bef\u8ba1\u6570\u3002<\/p>\n<pre>func main(entity[] string) {\n    errorc := make(chan string) \/\/ channel to insert failed entity\n    var wg sync.waitgroup\n\n    for _, link := range entity {\n        wg.add(1)\n        \/\/ spawn errorgroup here. errorgroup_spawn\n    }\n\n    go func() {\n        wg.wait()   \n        close(errorc)    \n    }()\n\n    for msg := range errorc {\n        \/\/ here storing error entityids somewhere.\n    }\n}<\/pre>\n<p>\u548c\u8fd9\u6837\u7684\u9519\u8bef\u7ec4<\/p>\n<pre>func errorgroup_spawn(ctx context.Context, errorC chan string, wg *sync.WaitGroup) { \/\/ and other params\n    defer (*wg).Done()\n    \n   goRoutineCollection, ctxx := errgroup.WithContext(ctx)\n    results := make(chan *result)\n    goRoutineCollection.Go(func() error {\n        \/\/ http calls for single entity\n        \/\/ if error occurs, push it in errorC, and return Error.\n        return nil\n    })\n\n    go func() {\n        goRoutineCollection.Wait()\n        close(result)\n    }()\n\n   return goRoutineCollection.Wait()\n}<\/pre>\n<p>ps\uff1a\u6211\u4e5f\u5728\u8003\u8651\u5e94\u7528\u5d4c\u5957\u9519\u8bef\u7ec4\uff0c\u4f46\u65e0\u6cd5\u5728\u8fd0\u884c\u5176\u4ed6\u9519\u8bef\u7ec4\u65f6\u7ef4\u62a4\u9519\u8bef\u8ba1\u6570 \u8c01\u80fd\u6307\u5bfc\u6211\uff0c\u8fd9\u662f\u5904\u7406\u6b64\u7c7b\u73b0\u5b9e\u4e16\u754c\u573a\u666f\u7684\u6b63\u786e\u65b9\u6cd5\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8ddf\u8e2a\u9519\u8bef\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u72b6\u6001\u7ed3\u6784\u6765\u8ddf\u8e2a\u54ea\u4e2a\u9519\u8bef\u6765\u81ea\u4f55\u5904\uff1a<\/p>\n<pre>type status struct {\n   entity string\n   err error\n}\n...\n\nerrorc := make(chan status) \n\n\/\/ spawn error groups with name of the entity, and when error happens, push status{entity:entityname,err:err} to the chanel<\/pre>\n<p>\u7136\u540e\u60a8\u53ef\u4ee5\u4ece\u9519\u8bef\u901a\u9053\u8bfb\u53d6\u6240\u6709\u9519\u8bef\u5e76\u627e\u51fa\u5931\u8d25\u7684\u539f\u56e0\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u6839\u672c\u4e0d\u4f7f\u7528\u9519\u8bef\u7ec4\u3002\u8fd9\u4f7f\u5f97\u4e8b\u60c5\u66f4\u52a0\u660e\u786e\uff0c\u4f46\u662f\u5426\u66f4\u597d\u8fd8\u6709\u5f85\u5546\u69b7\uff1a<\/p>\n<pre>\/\/ Keep entity statuses\nstatuses:=make([]Status,len(entity))\nfor i, link := range entity {\n   statuses[i].Entity=link\n   wg.Add(1)\n   go func(i index) {\n      defer wg.Done()\n      ctx, cancel:=context.WithCancel(context.Background())\n      defer cancel()\n\n      \/\/ Error collector\n      status:=make(chan error)\n      defer close(status)\n      go func() {\n         for st:=range status {\n             if st!=nil {\n                cancel()  \/\/ Stop all calls \n                \/\/ store first error\n                if statuses[i].Err==nil {\n                   statuses[i].Err=st\n                }\n             }\n         }\n      }()\n\n      innerWg:=sync.WaitGroup{}\n      innerWg.Add(1)\n      go func() {\n         defer innerWg.Done()\n         status&lt;- makeHttpCall(ctx)\n      }()\n      innerWg.Add(1)\n      go func() {\n         defer innerWg.Done()\n         status&lt;- makeHttpCall(ctx)\n      }()\n      ...\n      innerWg.Wait()\n\n   }(i)\n}<\/pre>\n<p>\u5f53\u4e00\u5207\u5b8c\u6210\u540e\uff0c<code>statuses<\/code>\u5c06\u5305\u542b\u6240\u6709\u5b9e\u4f53\u548c\u76f8\u5e94\u7684\u72b6\u6001\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5c06 errgroup \u5d4c\u5957\u5728\u4e00\u5806 goroutine \u4e2d\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-207689","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207689","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=207689"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207689\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}