channel size 应为 1 或是无缓冲的。默认情况下,channel 是无缓冲的,其 size 为零。任何其他尺寸都必须经过严格的审查,而不是拍脑袋给出一个大致的值。我们需要考虑如何确定大小,什么原因会导致 channel 在高负载下被填满并阻塞写入者,以及当这种情况发生时系统会发生哪些变化。

// Bad
// 应该足以满足任何情况!
c := make(chan int, 64)

// Good
// size 为 1
c := make(chan int, 1) 
// 或无缓冲 size 为 0
c := make(chan int)
powered by Gitbook该文章修订时间: 2024-03-22 15:30:00

results matching ""

    No results matching ""