I was Using PProf to analyze tracing for a data pipeline with producer and consumer. I'm not sure how I should read this, but from the scheduler latency profile, chanrecv
and chansend
accounts for a decent portion of my total run time.
I find it hard to believe, so does this suggest go channel send/receive taking that long, or this only referring t producer lagging behind that leads to chanrecv
blocking. And potentially how I can make this more efficient?
Sample code:
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))
}
}