如何推理 Go 并发模式扇入示例中的 Go 通道阻塞?

How to reason about Go channel blocking in Go Concurrency Patterns fan-in example?

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func boring(msg string) <-chan string { // Returns receive-only channel of strings.
    c := make(chan string)
    go func() { // We launch the goroutine from inside the function.
        for i := 0; ; i++ {
            c <- fmt.Sprintf("%s %d", msg, i)
            time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
        }
    }()
    return c // Return the channel to the caller.
}

func fanIn(input1, input2 <-chan string) <-chan string {
    c := make(chan string)
    go func() {
        for {
            c <- <-input1
        }
    }()
    go func() {
        for {
            c <- <-input2
        }
    }()
    return c
}

func main() {
    c := fanIn(boring("Joe"), boring("Ann"))
    for i := 0; i < 10; i++ {
        fmt.Println(<-c)
    }
    fmt.Println("You're both boring; I'm leaving.")
}

这是 Rob Pike 在 Go Concurrency Patterns 上的演讲中的一个例子。我了解扇入模式背后的想法,并且我了解在 main 中打印的消息顺序是不确定的:我们只打印 10 条结果准备就绪的消息。

然而,我不完全理解的是调用的顺序以及什么阻止了什么。

仅使用无缓冲通道,因此根据文档,无缓冲通道会阻止发送方。

boring 函数启动一个 goroutine,将字符串发送到 returned 的无缓冲通道 c。如果我理解正确,这个内部 goroutine 已启动但不会阻塞 boring。它可以立即return main 中的通道到fanIn 函数。但是 fanIn 做几乎相同的事情:它从输入通道接收值并将它们发送到它自己的通道,即 returned.

阻塞是如何发生的?在这种情况下是什么阻止了什么?一个示意图解释就完美了,因为老实说,即使我有一个直观的理解,我也想了解它背后的确切逻辑。

我的直觉理解是 boring 中的每个发送都会阻塞,直到在 fanIn 中接收到值,但是随后该值会立即发送到另一个通道,因此它会被阻塞,直到接收到该值在 main。粗略地说,这三个函数由于使用了通道

How does the blocking happen? What blocks what in this case?

如果另一端没有相应的接收操作(或者如果通道是nil,这就变成没有接收者的情况),则在无缓冲通道上的每个发送都会阻塞。

考虑在 main 中对 boringfanIn 的调用顺序发生。特别是这一行:

c := fanIn(boring("Joe"), boring("Ann"))

评估顺序:

  1. boring("Joe")
  2. boring("Ann")
  3. fanIn

boring("Joe")boring("Ann") 中的发送操作在 fanIn 中有相应的接收操作,因此它们会阻塞直到 fanIn 运行。因此 boring 生成自己的 goroutine 以确保它 returns 在 fanIn 可以开始接收之前的通道。

fanIn 中的发送操作在 main 中有相应的接收操作,因此它们将阻塞直到 fmt.Println(<-c) 运行。因此 fanIn 产生自己的 goroutine(s) 以确保它 returns 在 main 可以开始接收它之前的输出通道。

最后 main 的执行达到了 fmt.Println(<-c) 并启动了一切。在 c 接收解除阻塞 c <- <-input[1|2],在 <-input[1|2] 接收解除阻塞 c <- fmt.Sprintf("%s %d", msg, i).

如果去掉main中的receive操作,main仍然可以继续执行,程序马上退出,不会出现死锁。