Go 基于通道的管道卡住

Go 基于通道的管道卡住,go,Go,我正在尝试使用Go构建一个接收数据的管道。这三个阶段是“下载批”、“转换每条消息”和“将消息排入队列” 我觉得很自然的逻辑是为每个阶段创建3个功能,并将这些功能与无缓冲通道联系起来 在我的代码中,我没有正确实现通道,或者没有使用waitgroup?只有一条消息到达最后阶段,程序似乎停止/阻塞 func (c *worker) startWork() { // channel for messages to be sent to the queue chMe

我正在尝试使用Go构建一个接收数据的管道。这三个阶段是“下载批”、“转换每条消息”和“将消息排入队列”

我觉得很自然的逻辑是为每个阶段创建3个功能,并将这些功能与无缓冲通道联系起来

在我的代码中,我没有正确实现通道,或者没有使用waitgroup?只有一条消息到达最后阶段,程序似乎停止/阻塞

    func (c *worker) startWork() {
        // channel for messages to be sent to the queue
        chMessagesToEnqueue := make(chan types.Foo)

        // channel for messages to be transformed
        chMessagesToTransform := make(chan []types.UpstreamFooType)

        // start the goroutines with the channels
        go c.startTransformer(chMessagesToTransform, chMessagesToEnqueue)
        go c.startEnqueuer(chMessagesToEnqueue)
        go c.startDownloader(chMessagesToTransform)
    }

    func (c *worker) startDownloader(out chan []types.UpstreamFooType) {
        // https://github.com/SebastiaanKlippert/go-soda
        // uses a library here to fetch data from upstream APIs, but the gist is:
        var wg sync.WaitGroup
        for i := 0; i < c.workerCount; i++ {
            wg.Add(1)
            go func() {
                defer wg.Done()
                for {
                    // i've cut out some meat out to make more concise
                    var results []types.UpstreamFooType
                    err = json.NewDecoder(resp.Body).Decode(&results)
                    out <- results
                }
            }()
        }
        wg.Wait()        
    }

    func (c *worker) startTransformer(in <-chan []types.UpstreamFooType, out chan types.Foo) {
        data := <-in
        for _, record := range data {
            msg := types.Foo{
                name: record.fifa,
            }
            out <- msg
        }
    }

    func (c *worker) startEnqueuer(in <-chan []types.Foo) {
        data := <-in
        c.logger.Infow("startEnqueuer", "data", data)
    }
func(c*worker)startWork(){
//要发送到队列的消息的通道
chMessagesToeQueue:=make(chan types.Foo)
//要转换的消息的通道
chMessageStaterTransform:=make(chan[]类型.上游类型)
//使用频道启动goroutines
go c.启动变压器(CHMESSAgestTransform,CHMESSAgestOequeue)
转到c.startEnqueuer(chMessagesToeQueuee)
go c.启动下载器(chMessageStatorTransform)
}
func(c*工作者)开始下载程序(输出chan[]类型。上游OOTYPE){
// https://github.com/SebastiaanKlippert/go-soda
//此处使用库从上游API获取数据,但要点如下:
var wg sync.WaitGroup
对于i:=0;i输出您的
startDownloader
startttransformer
不要在其输入通道上进行测距。它们各自读取一条消息,处理它,然后返回。
startDownloader
继续尝试在其通道上发送,但在第一条消息之后,没有收到任何消息。非常感谢!因此,通过通道进行测距有效地消除了干扰重新设置消息,取消阻止通道?在通道上进行测距将继续读取,直到通道关闭。这就像在切片上进行测距,但仅限于通道。