{"id":207084,"date":"2025-07-08T09:21:53","date_gmt":"2025-07-08T01:21:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207084\/"},"modified":"2025-07-08T09:21:53","modified_gmt":"2025-07-08T01:21:53","slug":"%e5%a6%82%e6%9e%9c%e9%80%9a%e9%81%93%e5%b7%b2%e6%bb%a1%e6%88%96%e5%9c%a8%e7%89%b9%e5%ae%9a%e6%97%b6%e9%97%b4%e5%90%8e%e4%bb%8e-go-%e7%bc%93%e5%86%b2%e9%80%9a%e9%81%93%e8%af%bb%e5%8f%96","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207084\/","title":{"rendered":"\u5982\u679c\u901a\u9053\u5df2\u6ee1\u6216\u5728\u7279\u5b9a\u65f6\u95f4\u540e\u4ece Go \u7f13\u51b2\u901a\u9053\u8bfb\u53d6"},"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>\u5982\u679c\u901a\u9053\u5df2\u6ee1\u6216\u5728\u7279\u5b9a\u65f6\u95f4\u540e\u4ece Go \u7f13\u51b2\u901a\u9053\u8bfb\u53d6<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 22:27: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>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u679c\u901a\u9053\u5df2\u6ee1\u6216\u5728\u7279\u5b9a\u65f6\u95f4\u540e\u4ece Go \u7f13\u51b2\u901a\u9053\u8bfb\u53d6\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5982\u679c\u901a\u9053\u5df2\u6ee1\u6216\u7ecf\u8fc7\u4e00\u5b9a\u65f6\u95f4\uff0c\u6211\u60f3\u4ece\u901a\u9053\u8fdb\u884c\u5904\u7406\u3002\u6211\u7684\u7528\u4f8b\u4e0e\u73b0\u6709\u95ee\u9898\u7c7b\u4f3c\uff0c\u6211\u5c1d\u8bd5\u4fee\u6539\u73b0\u6709\u7b54\u6848<\/p>\n<p>\u6211\u7684\u4ee3\u7801\u4f4d\u4e8e https:\/\/go.dev\/play\/p\/hagz9hhqj0i\uff0c<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n    \"time\"\n)\n\ntype audit struct {\n    id int\n}\n\nconst batchsize = 5\n\nvar batch = make([]audit, 0, batchsize)\n\nfunc upsertbigqueryaudits(audits []audit) {\n    fmt.printf(\"processing batch of %d\\n\", len(audits))\n    for _, a := range audits {\n        fmt.printf(\"%d \", a.id)\n    }\n    fmt.println()\n    batch = []audit{}\n}\nfunc process(full &lt;-chan struct{}) {\n    ticker := time.newticker(1 * time.nanosecond)\n\n    for {\n        select {\n        case &lt;-full:\n            fmt.println(\"from full\")\n            upsertbigqueryaudits(batch)\n        case &lt;-ticker.c:\n            fmt.println(\"from ticker\")\n            if len(batch) &gt; 0 {\n                fmt.println(\"from ticker sending batch\")\n                upsertbigqueryaudits(batch)\n            }\n        }\n    }\n}\nfunc processaudits(audits &lt;-chan audit, full chan&lt;- struct{}, batchsize int) {\n    for audit := range audits {\n        batch = append(batch, audit)\n        if len(batch) == cap(batch) {\n            \/\/ upsertbigqueryaudits(batch)\n            fmt.println(\"sending full\")\n            full &lt;- struct{}{}\n\n        }\n    }\n\n}\n\nfunc produceaudits(x int, to chan audit) {\n    for i := 0; i &lt; x; i++ {\n        to &lt;- audit{\n            id: i,\n        }\n    }\n}\n\nfunc main() {\n    var wg sync.waitgroup\n    audits := make(chan audit)\n    full := make(chan struct{})\n    wg.add(1)\n    go func() {\n        defer wg.done()\n        process(full)\n    }()\n    wg.add(1)\n    go func() {\n        defer wg.done()\n        processaudits(audits, full, batchsize)\n    }()\n    wg.add(1)\n    go func() {\n        defer wg.done()\n        produceaudits(25, audits)\n        close(audits)\n    }()\n    wg.wait()\n    fmt.println(\"complete\")\n}<\/pre>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u60f3\u5728\u6279\u5904\u7406\u5145\u6ee1 <code>batchsize<\/code> \u5143\u7d20\u65f6\u6216\u5728\u7ecf\u8fc7\u4e00\u5b9a\u65f6\u95f4\uff08\u4f8b\u5982 1 \u7eb3\u79d2\uff09\u65f6\u5904\u7406\u6279\u5904\u7406\u3002<\/p>\n<p>\u7f16\u8f91\uff1a\u5185\u8054\u6dfb\u52a0\u4e86\u6211\u7684\u62c5\u5fe7\u3002 \u8fd9\u662f\u793a\u4f8b\u8f93\u51fa\u3002<\/p>\n<pre>timeout running program\nSending full &lt;--- this line comes twice\nSending full\nFrom full\nProcessing batch of 10 &lt;--- instead of 5 it is processing 10 items\n0 1 2 3 4 5 6 7 8 9 \nFrom full\nProcessing batch of 1\n10 \nSending full\nSending full\nFrom full\nProcessing batch of 1 &lt;-- instead of 5 it is processing 1 items. It does not process more than 1 item.\n11 \nFrom full\nProcessing batch of 0\n\n...\n...\nSending full\nSending full\nFrom full\nProcessing batch of 1 &lt;-- instead of 5 it is processing 1 items\n24 \nFrom full\nProcessing batch of 0\n\nFrom ticker &lt;-- this line comes only after consuming 25 items.  \nFrom ticker\nFrom ticker\nFrom ticker<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u5728\u627e\u5230\u4e86\u7b54\u6848<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"time\"\n)\n\nfunc BatchStringsCtx[T any](ctx context.Context, values &lt;-chan T, maxItems int, maxTimeout time.Duration) chan []T {\n    batches := make(chan []T)\n\n    go func() {\n        defer close(batches)\n\n        for keepGoing := true; keepGoing; {\n            var batch []T\n            expire := time.After(maxTimeout)\n            for {\n                select {\n                case &lt;-ctx.Done():\n                    keepGoing = false\n                    goto done\n\n                case value, ok := &lt;-values:\n                    if !ok {\n                        keepGoing = false\n                        goto done\n                    }\n\n                    batch = append(batch, value)\n                    if len(batch) == maxItems {\n                        goto done\n                    }\n\n                case &lt;-expire:\n                    goto done\n                }\n            }\n\n        done:\n            if len(batch) &gt; 0 {\n                batches &lt;- batch\n            }\n        }\n    }()\n\n    return batches\n}\n\nfunc main() {\n    strings := make(chan string)\n    go func() {\n        strings &lt;- \"hello\"\n        strings &lt;- \"there\" \/\/ hit limit of 2\n\n        strings &lt;- \"how\"\n        time.Sleep(15 * time.Millisecond) \/\/ hit timeout\n\n        strings &lt;- \"are\"\n        strings &lt;- \"you\" \/\/ hit limit of 2\n\n        \/\/ A really long time without any new values.\n        \/\/ The context was cancelled around 300ms,\n        \/\/ before this sleep finished.\n        time.Sleep(500 * time.Millisecond)\n\n        strings &lt;- \"doing?\" \/\/ never read\n\n        close(strings)\n    }()\n\n    \/\/ctx, cancel := context.WithTimeout(context.Background(), 300 * time.Millisecond)\n    ctx := context.Background()\n\n    start := time.Now()\n    batches := BatchStringsCtx[string](ctx, strings, 2, 10*time.Millisecond)\n    for batch := range batches {\n        fmt.Println(time.Now().Sub(start), batch)\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207084","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207084","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=207084"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207084\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}