Go 通道发送/接收的延迟大吗? (PProf 追踪)

问题描述

我使用 PProf 来分析对生产者和消费者的数据管道的跟踪。我不知道该如何解读,但从调度程序延迟配置文件来看,chanrecvchansend 在我的总运行时间中占了相当大的一部分。

我觉得很难相信,这是否表明 go channel send/receive 花费了那么长时间,或者这只是指 t 生产者落后导致 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))
    }
}

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)