docs(go): 全量事实核查,修正 14 章与 index 约 180 处与事实不符的内容

This commit is contained in:
2026-11-03 21:17:47 +08:00
parent ceb4a7fae0
commit 142f4f8559
15 changed files with 234 additions and 206 deletions

View File

@@ -104,7 +104,7 @@
<p class="take"><b>一句话:</b>context 不是 goroutine 的 kill 信号,只是一棵广播「别再做了」的树;接不接这个信号,取决于每个阻塞点有没有在听。</p>
<h3 id="sec-11-2">11.2 四个实现:emptyCtx、cancelCtx、timerCtx、valueCtx</h3>
<p><span class="tag">现象</span>打印 <code>context.Background()</code> 的类型,Go 1.21 起是 <code>*context.backgroundCtx</code>,1.20 及以前是 <code>*context.emptyCtx</code>。对同一个根调三个方法,返回全是空:Done 给 nil,Err 给 nil,Value 也是 nil,可它照样是合法的树根。</p>
<p><span class="tag">现象</span>打印 <code>context.Background()</code> 的类型,Go 1.21 起是 <code>context.backgroundCtx</code>,1.20 及以前是 <code>*context.emptyCtx</code>。对同一个根调三个方法,返回全是空:Done 给 nil,Err 给 nil,Value 也是 nil,可它照样是合法的树根。</p>
<p><span class="tag">机制</span>标准库没有用一个大而全的结构体,而是四个小类型,每个节点带一个指向父节点的字段,串成一条单链。逻辑上看到的是一棵树,物理上只有「父指针」这一个方向:取消顺着它往下走,值查找逆着它往上走。</p>
<div class="tablewrap">
<table>
@@ -194,7 +194,7 @@ go func() {
cancel() // 关闭 ctx.done,并级联关闭 child.done
time.Sleep(time.Millisecond)
fmt.Println("parent err:", ctx.Err()) // context.Canceled</pre>
<p>每次取消做四件事:关自己的 done、写 err、遍历 children 递归取消、按需把自己从父节点的 children 里摘掉。第四件不是无条件的——只有外部显式调用 cancel 函数时才传 true(context.go:576-578),父节点级联下来时一律传 false(child.cancel(false, ...))。理由是整棵子树都要丢,逐个摘除只是白花时间。</p>
<p>每次取消做四件事:写 err、关自己的 done、遍历 children 递归取消、按需把自己从父节点的 children 里摘掉。第四件不是无条件的——只有自己主动取消(调用 cancel 函数或超时定时器到点)时才传 true(context.go:576-578),父节点级联下来时一律传 false(child.cancel(false, ...))。理由是整棵子树都要丢,逐个摘除只是白花时间。</p>
<figure class="figure">
<svg viewBox="0 0 720 340" role="img" aria-label="取消从根节点向下级联到 A、B、C;A 被手动取消时还会把自己从父节点的 children 集合里摘掉">
@@ -242,7 +242,7 @@ fmt.Println(errors.Is(ctx.Err(), context.Canceled)) // true
fmt.Println(context.Cause(ctx)) // order 42 cancelled by user</pre>
<div class="box warn">
<span class="bt">易错点 · cancel 只是关 channel,不会杀 goroutine</span>
<p>很多人以为父 ctx 一取消,子 goroutine 就被结束掉。实际它只让 select 里一个分支就绪:<b>没监听 Done 的 goroutine 照样跑完</b>。验证很简单——起一个死循环 goroutine,cancel 之后 NumGoroutine 不降反升。</p>
<p>很多人以为父 ctx 一取消,子 goroutine 就被结束掉。实际它只让 select 里一个分支就绪:<b>没监听 Done 的 goroutine 照样跑完</b>。验证很简单——起一个死循环 goroutine,cancel 之后 NumGoroutine 不会下降。</p>
</div>
<p><span class="tag">代价</span>children 是 map 加互斥锁,注册和摘除都要抢锁;级联是同步递归,在调 cancel 的那个 goroutine 上一次性做完,深树上一次取消就是 N 次函数调用加 N 次 close;close 又会把全部等待者同时唤醒,下游一起被放出来抢同一把锁。</p>
<p class="take"><b>一句话:</b>级联不是遍历出来的,是建节点时就登记好的:向上找祖先注册,向下沿 children 递归;代价全部压在调用 cancel 的那一个 goroutine 上。</p>
@@ -266,11 +266,11 @@ fmt.Println(context.Cause(ctx)) // order 42 cancelled by us
return io.ReadAll(resp.Body)
}</pre>
<p>还有一个能省掉一堆困惑的细节:如果父 ctx 的截止时间本来就比新设的更早,构造会直接退化成不带定时器的取消节点——预算自动继承,子层不可能比父层活得更久。</p>
<pre data-lang="go" data-name="context.go:父的 deadline 更早时不再起定时器">func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
<pre data-lang="go" data-name="context.go:父的 deadline 更早时不再起定时器">func WithDeadlineCause(parent Context, d time.Time, cause error) (Context, CancelFunc) {
if cur, ok := parent.Deadline(); ok &amp;&amp; cur.Before(d) {
return WithCancel(parent) // 已有更紧的预算,继承即可
}
// 否则才建 timerCtx 并起定时器
// 否则才建 timerCtx 并起定时器(Go 1.21 起 WithDeadline 只是转调这里)
}</pre>
<figure class="figure">
@@ -311,7 +311,7 @@ fmt.Println(context.Cause(ctx)) // order 42 cancelled by us
<span class="bt">易错点 · DeadlineExceeded 不等于 Canceled</span>
<p>超时取消时 Err 是 DeadlineExceeded,不是 Canceled,用 errors.Is 互相比较也不相等。要判断「是不是被取消了」,统一写 <code>ctx.Err() != nil</code>,别只跟 Canceled 比。</p>
</div>
<p><span class="tag">代价</span>每个带超时的节点都要向 runtime 登记一条定时器记录,提前返回就得靠 defer cancel 把它撤掉,否则一直挂到到期;定时器堆的增删走全局锁,高频短超时会在这把锁上排队。</p>
<p><span class="tag">代价</span>每个带超时的节点都要向 runtime 登记一条定时器记录,提前返回就得靠 defer cancel 把它撤掉,否则一直挂到到期;定时器堆按 P 分片,增删只抢所在 P 的锁(runtime/time.go:130-134),高频短超时不会挤在一把全局锁上。</p>
<p class="take"><b>一句话:</b>超时是绝对时刻,父子取更早;「每层设 500ms」不等于每层都有 500ms,真正决定死活的是入口那一个 deadline。</p>
<div class="box note">
<span class="bt">跨进程传递</span>
@@ -503,7 +503,7 @@ func TestWorker(t *testing.T) {
return repoOrder, nil
}</pre>
<p><code>errgroup.WithContext</code> 在第一个 goroutine 返回错误时取消派生 context,
另一个请求会收到 <code>context.Canceled</code> 并尽快退出,避免「一个失败、另一个还在跑」。</p>
另一个分支会收到 <code>context.Canceled</code> 并尽快退出,避免「一个失败、另一个还在跑」。</p>
<p>这里最容易写错的是共享变量:两个闭包各自只写自己的结果(缓存侧写 <code>cached</code>/<code>cacheHit</code>,仓储侧写 <code>repoOrder</code>),由 <code>g.Wait()</code> 建立 happens-before。如果两个闭包都去写同一个 <code>order</code>,<code>errgroup</code> 只在 <code>Wait</code> 处同步、闭包之间没有任何同步,<code>go test -race</code> 会直接报 write-write race。注意 <code>cancel</code> 仍然要 <code>defer</code>,errgroup 的取消不会释放父 context 的定时器。</p>
<div class="box case">
<span class="bt">一个排查案例</span>