Go: One producer many consumers

后端 未结 4 1718
情话喂你
情话喂你 2021-02-02 13:56

So I have seen a lot of ways of implementing one consumer and many producers in Go - the classic fanIn function from the Concurrency in Go talk.

What I want is a fanOut

4条回答
  •  情歌与酒
    2021-02-02 14:03

    We can handle multiple consumers without making the copy of channel data for each consumer.

    Go playground: https://play.golang.org/p/yOKindnqiZv

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    type data struct {
        msg string
        consumers int
    }
    
    func main() {
        ch := make(chan *data) // both block or non-block are ok
        var wg sync.WaitGroup
        consumerCount := 3 // specify no. of consumers
    
        producer := func() {
            obj := &data {
                msg: "hello everyone!",
                consumers: consumerCount,
            }
            ch <- obj
        }
        consumer := func(idx int) {
            defer wg.Done()
            obj := <-ch
            fmt.Printf("consumer %d received data %v\n", idx, obj)
            obj.consumers--
            if obj.consumers > 0 {
                ch <- obj // forward to others
            } else {
                fmt.Printf("last receiver: %d\n", idx)
            }
        }
    
        go producer()
        for i:=1; i<=consumerCount; i++ {
            wg.Add(1)
            go consumer(i)
        }
    
        wg.Wait()
    }
    

提交回复
热议问题