如何限制goroutine

时间:2017-08-07 09:54:40

标签: go google-api gmail-api goroutine

我正在开发基于google api的gmail客户端。

我有一个通过此次调用获得的标签列表

r, err := s.gClient.Service.Users.Labels.List(s.gClient.User).Do()

然后,对于我需要获取详细信息的每个标签

for _, l := range r.Labels {
    d, err := s.gClient.Service.Users.Labels.Get(s.gClient.User, l.Id).Do()
}

我想以更强大的方式处理循环,所以我在循环中实现了一个goroutine:

ch := make(chan label.Label)

for _, l := range r.Labels {

    go func(gmailLabels *gmailclient.Label, gClient *gmail.Client, ch chan<- label.Label) {

        d, err := s.gClient.Service.Users.Labels.Get(s.gClient.User, l.Id).Do()

        if err != nil {
            panic(err)
        }

        // Performs some operation with the label `d`
        preparedLabel := ....

        ch <- preparedLabel

    }(l, s.gClient, ch)
}

for i := 0; i < len(r.Labels); i++ {
    lab := <-ch
    fmt.Printf("Processed %v\n", lab.LabelID)
}

此代码的问题是gmail api有速率限制,因此,我收到此错误:

panic: googleapi: Error 429: Too many concurrent requests for user, rateLimitExceeded

处理这种情况的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如何才开始,例如10个goroutines并在另一个例程中从一个for循环传递值。通道具有较小的缓冲区以减少同步时间。

chIn := make(chan label.Label, 20)
chOut := make(chan label.Label, 20)

for i:=0;i<10;i++ {
    go func(gClient *gmail.Client, chIn chan label.Label, chOut chan<- label.Label) {

        for gmailLabels := range chIn {
            d, err := s.gClient.Service.Users.Labels.Get(s.gClient.User, l.Id).Do()

            if err != nil {
                panic(err)
            }

            // Performs some operation with the label `d`
            preparedLabel := ....

            chOut <- preparedLabel

        }

    }(s.gClient, chIn, chOut)

}

go func(chIn chan label.Label) {
    defer close(chIn)
    for _, l := range r.Labels {
        chIn <- l
    }
}(chIn)

for i := 0; i < len(r.Labels); i++ {
    lab := <-chOut
    fmt.Printf("Processed %v\n", lab.LabelID)
}

编辑:

这是playground sample