首页 > 解决方案 > Go 通道发送/接收的延迟大?(PProf 追踪)

问题描述

我正在使用 PProf 分析与生产者和消费者的数据管道的跟踪。我不确定我应该如何阅读这个,但从调度程序延迟配置文件来看,chanrecv并且chansend占我总运行时间的相当一部分。

我觉得很难相信,所以这是否建议进行通道发送/接收需要那么长时间,或者这只是指落后的生产者会导致chanrecv阻塞。以及如何使这更有效?

示例代码:

func consume(in chan User) {
    out := make(chan UserDetail)
    go func() {
        for user := range in {
            if userDetail, err := getUserDetails(user); err == nil {
                out <- userDetail
            }
        }
        close(out)
    }()
    return out
}

func produce() {
    out := make(chan User)
    go func() {
        // could be issuing a paginated api call to get users list
        for user := range users {
            out <- user
        }
        close(out)
    }()
    return out
}


func main() {
    userChan := produce() // get all users
    userDetailChannels := []chan User

    // fanout to 8 workers
    for workerIndex := 0; workerIndex < 8; workerIndex++ {
        userDetailChannels = append(userDetailChannels, consume(userChan))
    }
}

在此处输入图像描述

标签: gochannelproducer-consumergoroutinepprof

解决方案


推荐阅读