{"id":764,"date":"2025-04-21T16:29:32","date_gmt":"2025-04-21T08:29:32","guid":{"rendered":"https:\/\/beijian99.top\/?p=764"},"modified":"2025-05-03T02:44:59","modified_gmt":"2025-05-02T18:44:59","slug":"%e6%80%8e%e4%b9%88%e5%8a%9e%ef%bc%9f%e7%94%9f%e4%ba%a7%e7%8e%af%e5%a2%83%e6%ad%bb%e9%94%81","status":"publish","type":"post","link":"https:\/\/beijian99.top\/?p=764","title":{"rendered":"\u751f\u4ea7\u73af\u5883\u6b7b\u9501\uff1f\u5e74\u7ec8\u5956\u4e0d\u4fdd\uff01"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6b7b\u9501\u662f\u751f\u4ea7\u73af\u5883\u4e2d\u5e38\u89c1\u7684\u4e25\u91cd\u95ee\u9898\uff0c\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u90e8\u5206\u6216\u5168\u90e8\u529f\u80fd\u4e0d\u53ef\u7528\u3002\u5982\u4f55\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u6392\u67e5\u3001\u5b9a\u4f4d\u6b7b\u9501\u95ee\u9898\uff0c\u5e76\u63d0\u4f9b\u6709\u6548\u7684\u4fee\u590d\u65b9\u6848\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u6b7b\u9501\uff1f<\/h2>\n\n\n\n<p>\u6b7b\u9501\uff08Deadlock\uff09\u662f\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u6307\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u4e92\u76f8\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u8d44\u6e90\u800c\u9677\u5165\u65e0\u9650\u963b\u585e\u7684\u72b6\u6001\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u591a\u4e2a\u7ebf\u7a0b\u4e92\u76f8&#8221;\u5361\u4f4f&#8221;\uff0c\u8c01\u90fd\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\u3002<\/p>\n\n\n\n<p>\u6b7b\u9501\u7684\u5178\u578b\u7c7b\u6bd4\u662f&#8221;\u54f2\u5b66\u5bb6\u5c31\u9910\u95ee\u9898&#8221;\uff1a\u4e94\u4f4d\u54f2\u5b66\u5bb6\u56f4\u5750\u5728\u5706\u684c\u65c1\uff0c\u6bcf\u4eba\u5de6\u53f3\u5404\u6709\u4e00\u628a\u53c9\u5b50\u3002\u54f2\u5b66\u5bb6\u4eec\u8981\u4e48\u601d\u8003\uff0c\u8981\u4e48\u5c31\u9910\uff0c\u5c31\u9910\u65f6\u9700\u8981\u540c\u65f6\u62ff\u8d77\u5de6\u53f3\u4e24\u628a\u53c9\u5b50\u3002\u5982\u679c\u6240\u6709\u54f2\u5b66\u5bb6\u540c\u65f6\u62ff\u8d77\u5de6\u8fb9\u7684\u53c9\u5b50\uff0c\u7136\u540e\u8bd5\u56fe\u62ff\u53f3\u8fb9\u7684\u53c9\u5b50\u65f6\uff0c\u4f1a\u53d1\u73b0\u53f3\u8fb9\u7684\u53c9\u5b50\u5df2\u88ab\u53f3\u8fb9\u7684\u54f2\u5b66\u5bb6\u62ff\u8d70\uff0c\u4e8e\u662f\u6240\u6709\u4eba\u90fd\u62ff\u7740\u4e00\u4e2a\u53c9\u5b50\u7b49\u5f85\u53e6\u4e00\u4e2a\u53c9\u5b50\uff0c\u5bfc\u81f4\u6240\u6709\u4eba\u90fd\u65e0\u6cd5\u5c31\u9910\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6b7b\u9501\u53d1\u751f\u7684\u56db\u4e2a\u5fc5\u8981\u6761\u4ef6<\/h2>\n\n\n\n<p>\u6b7b\u9501\u7684\u53d1\u751f\u5fc5\u987b\u540c\u65f6\u6ee1\u8db3\u4ee5\u4e0b\u56db\u4e2a\u6761\u4ef6\uff0c\u7f3a\u4e00\u4e0d\u53ef\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u200b<strong>\u200b\u4e92\u65a5\u6761\u4ef6\uff08Mutual Exclusion\uff09\u200b<\/strong>\u200b\uff1a\u8d44\u6e90\u4e00\u6b21\u53ea\u80fd\u7531\u4e00\u4e2a\u7ebf\u7a0b\u5360\u7528\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5fc5\u987b\u7b49\u5f85\u8be5\u8d44\u6e90\u88ab\u91ca\u653e\u3002<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u5360\u6709\u5e76\u7b49\u5f85\uff08Hold and Wait\uff09\u200b<\/strong>\u200b\uff1a\u7ebf\u7a0b\u5df2\u7ecf\u6301\u6709\u81f3\u5c11\u4e00\u4e2a\u8d44\u6e90\uff0c\u540c\u65f6\u53c8\u5728\u7b49\u5f85\u83b7\u53d6\u5176\u4ed6\u88ab\u5360\u7528\u7684\u8d44\u6e90\u3002<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u975e\u62a2\u5360\u6761\u4ef6\uff08No Preemption\uff09\u200b<\/strong>\u200b\uff1a\u5df2\u5206\u914d\u7ed9\u7ebf\u7a0b\u7684\u8d44\u6e90\u4e0d\u80fd\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5f3a\u884c\u593a\u53d6\uff0c\u5fc5\u987b\u7531\u6301\u6709\u8005\u663e\u5f0f\u91ca\u653e\u3002<\/li>\n\n\n\n<li>\u200b<strong>\u200b\u5faa\u73af\u7b49\u5f85\uff08Circular Wait\uff09\u200b<\/strong>\u200b\uff1a\u5b58\u5728\u4e00\u4e2a\u7ebf\u7a0b-\u8d44\u6e90\u7684\u5faa\u73af\u94fe\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u4e0b\u4e00\u4e2a\u7ebf\u7a0b\u6240\u5360\u7528\u7684\u8d44\u6e90\u3002<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u6b7b\u9501\u95ee\u9898\u8bc6\u522b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u6b7b\u9501\u7684\u5178\u578b\u8868\u73b0<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7cfb\u7edf\u90e8\u5206\u529f\u80fd\u65e0\u54cd\u5e94<\/li>\n\n\n\n<li>\u8bf7\u6c42\u5904\u7406\u65f6\u95f4\u5f02\u5e38\u5ef6\u957f<\/li>\n\n\n\n<li>CPU\u4f7f\u7528\u7387\u7a81\u7136\u4e0b\u964d\u4f46\u7cfb\u7edf\u8d1f\u8f7d\u4ecd\u9ad8<\/li>\n\n\n\n<li>\u65e5\u5fd7\u4e2d\u51fa\u73b0\u5927\u91cf\u8d85\u65f6\u6216\u963b\u585e\u8b66\u544a<\/li>\n\n\n\n<li>\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u8017\u5c3d<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u76d1\u63a7\u6307\u6807\u5f02\u5e38<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ebf\u7a0b\/goroutine\u6570\u91cf\u6fc0\u589e\u540e\u505c\u6ede<\/li>\n\n\n\n<li>\u9501\u7b49\u5f85\u65f6\u95f4\u6307\u6807\u5f02\u5e38\u5347\u9ad8<\/li>\n\n\n\n<li>\u8bf7\u6c42\u961f\u5217\u5806\u79ef<\/li>\n\n\n\n<li>\u6570\u636e\u5e93\u9501\u7b49\u5f85\u8d85\u65f6\u9519\u8bef\u589e\u591a<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u6b7b\u9501\u95ee\u9898\u6392\u67e5\u5de5\u5177<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Go\u8bed\u8a00\u7279\u6709\u5de5\u5177<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">(1) pprof\u5de5\u5177<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# \u83b7\u53d6goroutine\u5806\u6808\ngo tool pprof http:\/\/localhost:6060\/debug\/pprof\/goroutine\n\n# \u751f\u6210\u706b\u7130\u56fe\ngo tool pprof -http=:8080 http:\/\/localhost:6060\/debug\/pprof\/goroutine\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">(2) trace\u5de5\u5177<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# \u751f\u6210trace\u6587\u4ef6\ncurl http:\/\/localhost:6060\/debug\/pprof\/trace?seconds=5 &gt; trace.out\n\n# \u5206\u6790trace\ngo tool trace trace.out\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">(3) gops\u5de5\u5177<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ngops stack &amp;lt;pid&gt;  # \u67e5\u770b\u6307\u5b9a\u8fdb\u7a0b\u7684\u5806\u6808\ngops trace &amp;lt;pid&gt;  # \u8ddf\u8e2agoroutine\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. \u901a\u7528\u6392\u67e5\u5de5\u5177<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">(1) \u65e5\u5fd7\u5206\u6790<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5173\u952e\u9501\u64cd\u4f5c\u524d\u540e\u6dfb\u52a0\u8be6\u7ec6\u65e5\u5fd7<\/li>\n\n\n\n<li>\u8bb0\u5f55goroutine ID\u548c\u9501\u83b7\u53d6\/\u91ca\u653e\u65f6\u95f4<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nimport &quot;runtime&quot;\n\nfunc getGoroutineID() uint64 {\n    b := make(&#x5B;]byte, 64)\n    b = b&#x5B;:runtime.Stack(b, false)]\n    \/\/ \u63d0\u53d6goroutine ID\n    var id uint64\n    fmt.Sscanf(string(b), &quot;goroutine %d &quot;, &amp;id)\n    return id\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">(2) Prometheus\/Grafana\u76d1\u63a7<\/h4>\n\n\n\n<p>\u76d1\u63a7\u5173\u952e\u6307\u6807\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>goroutine\u6570\u91cf<\/li>\n\n\n\n<li>channel\u7f13\u51b2\u533a\u4f7f\u7528\u7387<\/li>\n\n\n\n<li>\u4e92\u65a5\u9501\u7b49\u5f85\u65f6\u95f4<\/li>\n\n\n\n<li>\u7cfb\u7edf\u8c03\u7528\u963b\u585e\u65f6\u95f4<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u6b7b\u9501\u5b9a\u4f4d\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5206\u6790goroutine\u5806\u6808<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# \u83b7\u53d6\u6240\u6709goroutine\u5806\u6808\nkill -SIGABRT &amp;lt;pid&gt;  # \u751f\u6210core dump\n<\/pre><\/div>\n\n\n<p>\u5206\u6790\u5806\u6808\u91cd\u70b9\u5173\u6ce8\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u963b\u585e\u5728channel\u64cd\u4f5c\u7684goroutine<\/li>\n\n\n\n<li>\u7b49\u5f85\u9501\u7684goroutine<\/li>\n\n\n\n<li>\u76f8\u4e92\u7b49\u5f85\u7684\u5faa\u73af\u4f9d\u8d56<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u590d\u73b0\u95ee\u9898<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">(1) \u538b\u529b\u6d4b\u8bd5\u590d\u73b0<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nfunc TestDeadlock(t *testing.T) {\n    for i := 0; i &lt; 1000; i++ {\n        go func() {\n            \/\/ \u53ef\u80fd\u4ea7\u751f\u6b7b\u9501\u7684\u4ee3\u7801\n        }()\n    }\n    time.Sleep(10 * time.Second)\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">(2) \u4f7f\u7528go test -race<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ngo test -race -v .\/...\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. \u52a8\u6001\u8ffd\u8e2a<\/h3>\n\n\n\n<p>\u4f7f\u7528Linux perf\u6216bcc\u5de5\u5177\u52a8\u6001\u8ffd\u8e2a\u9501\u64cd\u4f5c\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# \u5b89\u88c5bcc\u5de5\u5177\nsudo apt install bpfcc-tools\n\n# \u8ffd\u8e2amutex\u9501\nsudo funclatency-bpfcc &#039;sync.(*Mutex).Lock&#039;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5e38\u89c1\u6b7b\u9501\u573a\u666f\u4e0e\u4fee\u590d\u65b9\u6848<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Channel\u6b7b\u9501<\/h3>\n\n\n\n<p><strong>\u573a\u666f<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nch := make(chan int)\nch &lt;- 42  \/\/ \u963b\u585e\nval := &lt;-ch\n<\/pre><\/div>\n\n\n<p><strong>\u4fee\u590d\u65b9\u6848<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4f7f\u7528\u5e26\u7f13\u51b2channel<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nch := make(chan int, 1)\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u4f7f\u7528select\u907f\u514d\u963b\u585e<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nselect {\ncase ch &lt;- 42:\ndefault:\n    \/\/ \u5904\u7406\u53d1\u9001\u5931\u8d25\n}\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u786e\u4fdd\u6709goroutine\u5728\u63a5\u6536<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\ngo func() { val := &lt;-ch }()\nch &lt;- 42\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. \u4e92\u65a5\u9501\u6b7b\u9501<\/h3>\n\n\n\n<p><strong>\u573a\u666f<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nvar mu sync.Mutex\nmu.Lock()\nmu.Lock() \/\/ \u7b2c\u4e8c\u6b21\u52a0\u9501\u6b7b\u9501\n<\/pre><\/div>\n\n\n<p><strong>\u4fee\u590d\u65b9\u6848<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4f7f\u7528sync.RWMutex\u66ff\u4ee3<\/li>\n\n\n\n<li>\u68c0\u67e5\u9012\u5f52\u8c03\u7528<\/li>\n\n\n\n<li>\u4f7f\u7528defer\u786e\u4fdd\u89e3\u9501<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nvar mu sync.Mutex\nmu.Lock()\ndefer mu.Unlock()\n\/\/ \u4e34\u754c\u533a\u4ee3\u7801\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. \u5faa\u73af\u7b49\u5f85\u6b7b\u9501<\/h3>\n\n\n\n<p><strong>\u573a\u666f<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\n\/\/ Goroutine1\nmuA.Lock()\nmuB.Lock()\n\n\/\/ Goroutine2\nmuB.Lock()\nmuA.Lock()\n<\/pre><\/div>\n\n\n<p><strong>\u4fee\u590d\u65b9\u6848<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u56fa\u5b9a\u9501\u83b7\u53d6\u987a\u5e8f<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\n\/\/ \u6240\u6709goroutine\u90fd\u6309A-&gt;B\u987a\u5e8f\u83b7\u53d6\u9501\nmuA.Lock()\nmuB.Lock()\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u4f7f\u7528sync.Once\u6216\u539f\u5b50\u64cd\u4f5c<\/li>\n\n\n\n<li>\u5408\u5e76\u9501\u51cf\u5c11\u9501\u6570\u91cf<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6761\u4ef6\u53d8\u91cf\u8bef\u7528<\/h3>\n\n\n\n<p><strong>\u573a\u666f<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\ncond.L.Lock()\nfor condition == false {\n    cond.Wait() \/\/ \u53ef\u80fd\u6c38\u4e45\u963b\u585e\n}\ncond.L.Unlock()\n<\/pre><\/div>\n\n\n<p><strong>\u4fee\u590d\u65b9\u6848<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6dfb\u52a0\u8d85\u65f6\u673a\u5236<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\ntimeout := time.NewTimer(1 * time.Second)\nfor condition == false {\n    select {\n    case &lt;-timeout.C:\n        return errors.New(&quot;timeout&quot;)\n    default:\n        cond.Wait()\n    }\n}\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u786e\u4fdd\u6709goroutine\u4f1a\u8c03\u7528cond.Broadcast()<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u751f\u4ea7\u73af\u5883\u4fee\u590d\u6d41\u7a0b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u7d27\u6025\u5904\u7406\u63aa\u65bd<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u91cd\u542f\u670d\u52a1<\/strong>\uff1a\u4e34\u65f6\u6062\u590d\u53ef\u7528\u6027<\/li>\n\n\n\n<li><strong>\u6d41\u91cf\u964d\u7ea7<\/strong>\uff1a\u5173\u95ed\u975e\u6838\u5fc3\u529f\u80fd<\/li>\n\n\n\n<li><strong>\u6269\u5bb9\u5b9e\u4f8b<\/strong>\uff1a\u5206\u6563\u9501\u7ade\u4e89<\/li>\n\n\n\n<li><strong>\u7194\u65ad\u673a\u5236<\/strong>\uff1a\u9632\u6b62\u8fde\u9501\u53cd\u5e94<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u957f\u671f\u89e3\u51b3\u65b9\u6848<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4ee3\u7801\u91cd\u6784<\/strong>\uff1a<br>\u2022 \u51cf\u5c11\u5171\u4eab\u72b6\u6001<br>\u2022 \u4f7f\u7528\u65e0\u9501\u6570\u636e\u7ed3\u6784<br>\u2022 \u5b9e\u73b0\u66f4\u7ec6\u7c92\u5ea6\u7684\u9501<\/li>\n\n\n\n<li><strong>\u67b6\u6784\u4f18\u5316<\/strong>\uff1a<br>\u2022 \u5f15\u5165\u6d88\u606f\u961f\u5217\u89e3\u8026<br>\u2022 \u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u670d\u52a1<br>\u2022 \u5b9e\u73b0\u8bfb\u5199\u5206\u79bb<\/li>\n\n\n\n<li><strong>\u9632\u5fa1\u6027\u7f16\u7a0b<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\n   func SafeOperation() error {\n       var mu sync.Mutex\n       acquired := make(chan struct{})\n\n       go func() {\n           mu.Lock()\n           close(acquired)\n       }()\n\n       select {\n       case &lt;-acquired:\n           defer mu.Unlock()\n           \/\/ \u6267\u884c\u64cd\u4f5c\n           return nil\n       case &lt;-time.After(1 * time.Second):\n           return errors.New(&quot;lock acquire timeout&quot;)\n       }\n   }\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. \u9884\u9632\u63aa\u65bd<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4ee3\u7801\u5ba1\u67e5<\/strong>\uff1a\u91cd\u70b9\u5173\u6ce8\u9501\u548cchannel\u4f7f\u7528<\/li>\n\n\n\n<li><strong>\u538b\u529b\u6d4b\u8bd5<\/strong>\uff1a\u6a21\u62df\u9ad8\u5e76\u53d1\u573a\u666f<\/li>\n\n\n\n<li><strong>\u6df7\u6c8c\u5de5\u7a0b<\/strong>\uff1a\u6ce8\u5165\u6545\u969c\u6d4b\u8bd5\u7cfb\u7edf\u97e7\u6027<\/li>\n\n\n\n<li><strong>\u76d1\u63a7\u544a\u8b66<\/strong>\uff1a\u8bbe\u7f6e\u9501\u7b49\u5f85\u65f6\u95f4\u9608\u503c\u544a\u8b66<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u6848\u4f8b\u5206\u6790\uff1a\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u6b7b\u9501<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u95ee\u9898\u73b0\u8c61<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u5e93\u67e5\u8be2\u8d85\u65f6<\/li>\n\n\n\n<li>\u8fde\u63a5\u6c60\u8017\u5c3d<\/li>\n\n\n\n<li>\u5e94\u7528\u65e5\u5fd7\u663e\u793a\u5927\u91cfgoroutine\u963b\u585e\u5728\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u6392\u67e5\u6b65\u9aa4<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u83b7\u53d6goroutine\u5806\u6808<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncurl http:\/\/localhost:6060\/debug\/pprof\/goroutine?debug=2\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u53d1\u73b0\u5927\u91cfgoroutine\u963b\u585e\u5728sql.Open()\u6216\u8fde\u63a5\u83b7\u53d6<\/li>\n\n\n\n<li>\u68c0\u67e5\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e\u548cSQL\u8bed\u53e5<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u6839\u672c\u539f\u56e0<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e8b\u52a1\u4e2d\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684SQL<\/li>\n\n\n\n<li>\u672a\u8bbe\u7f6e\u67e5\u8be2\u8d85\u65f6<\/li>\n\n\n\n<li>\u8fde\u63a5\u6cc4\u6f0f<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u590d\u65b9\u6848<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6dfb\u52a0\u67e5\u8be2\u8d85\u65f6<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\nctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)\ndefer cancel()\nrows, err := db.QueryContext(ctx, &quot;SELECT ...&quot;)\n<\/pre><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u4f18\u5316\u4e8b\u52a1\u8303\u56f4<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\n\/\/ \u9519\u8bef\u505a\u6cd5\ntx, _ := db.Begin()\n\/\/ \u6267\u884c\u591a\u4e2a\u8017\u65f6\u64cd\u4f5c\ntx.Commit()\n\n\/\/ \u6b63\u786e\u505a\u6cd5\nfunc doWork() error {\n    tx, err := db.Begin()\n    if err != nil {\n        return err\n    }\n    defer func() {\n        if err != nil {\n            tx.Rollback()\n        }\n    }()\n\n    \/\/ \u6bcf\u4e2a\u64cd\u4f5c\u5355\u72ec\u8d85\u65f6\n    ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)\n    defer cancel()\n    _, err = tx.ExecContext(ctx, &quot;...&quot;)\n    if err != nil {\n        return err\n    }\n\n    return tx.Commit()\n}\n<\/pre><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u6dfb\u52a0\u8fde\u63a5\u6cc4\u6f0f\u68c0\u6d4b<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: go; title: ; notranslate\" title=\"\">\n\/\/ \u4f7f\u7528sql.DB\u7684SetConnMaxLifetime\ndb.SetConnMaxLifetime(30 * time.Minute)\ndb.SetConnMaxIdleTime(5 * time.Minute)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u603b\u7ed3<\/h2>\n\n\n\n<p>\u751f\u4ea7\u73af\u5883\u6b7b\u9501\u95ee\u9898\u6392\u67e5\u4e0e\u4fee\u590d\u9700\u8981\u7cfb\u7edf\u6027\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5feb\u901f\u8bc6\u522b<\/strong>\uff1a\u901a\u8fc7\u76d1\u63a7\u548c\u65e5\u5fd7\u786e\u8ba4\u6b7b\u9501\u73b0\u8c61<\/li>\n\n\n\n<li><strong>\u7cbe\u51c6\u5b9a\u4f4d<\/strong>\uff1a\u5229\u7528\u5de5\u5177\u83b7\u53d6goroutine\u5806\u6808\u548c\u9501\u72b6\u6001<\/li>\n\n\n\n<li><strong>\u6709\u6548\u4fee\u590d<\/strong>\uff1a\u6839\u636e\u6b7b\u9501\u7c7b\u578b\u9009\u62e9\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848<\/li>\n\n\n\n<li><strong>\u9884\u9632\u590d\u53d1<\/strong>\uff1a\u901a\u8fc7\u4ee3\u7801\u5ba1\u67e5\u3001\u538b\u529b\u6d4b\u8bd5\u548c\u76d1\u63a7\u544a\u8b66\u9632\u6b62\u518d\u6b21\u53d1\u751f<\/li>\n<\/ol>\n\n\n\n<p>\u9884\u9632\u80dc\u4e8e\u6cbb\u7597\u3002\u826f\u597d\u7684\u5e76\u53d1\u8bbe\u8ba1\u3001\u9002\u5f53\u7684\u9501\u7c92\u5ea6\u548c\u5b8c\u5584\u7684\u76d1\u63a7\u4f53\u7cfb\u662f\u907f\u514d\u751f\u4ea7\u73af\u5883\u6b7b\u9501\u7684\u5173\u952e\u3002\u5728Go\u4e2d\uff0c\u9075\u5faa&#8221;\u901a\u8fc7\u901a\u4fe1\u5171\u4eab\u5185\u5b58&#8221;\u7684\u54f2\u5b66\uff0c\u5408\u7406\u4f7f\u7528channel\u548c\u540c\u6b65\u539f\u8bed\uff0c\u53ef\u4ee5\u5927\u5927\u964d\u4f4e\u6b7b\u9501\u98ce\u9669\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"311\" height=\"162\" src=\"https:\/\/beijian99.top\/wp-content\/uploads\/2025\/04\/image-69.png\" alt=\"\" class=\"wp-image-965\" srcset=\"https:\/\/beijian99.top\/wp-content\/uploads\/2025\/04\/image-69.png 311w, https:\/\/beijian99.top\/wp-content\/uploads\/2025\/04\/image-69-300x156.png 300w\" sizes=\"auto, (max-width: 311px) 100vw, 311px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6b7b\u9501\u95ee\u9898\u8bc6\u522b 1. \u6b7b\u9501\u7684\u5178\u578b\u8868\u73b0 2. \u76d1\u63a7\u6307\u6807\u5f02\u5e38 \u4e8c\u3001\u6b7b\u9501\u95ee\u9898\u6392\u67e5\u5de5\u5177 1. Go\u8bed\u8a00\u7279\u6709\u5de5\u5177 (1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":965,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[143],"tags":[144],"class_list":["post-764","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-143","tag-144"],"_links":{"self":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=764"}],"version-history":[{"count":4,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions\/966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/media\/965"}],"wp:attachment":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}