dongren4147 2016-05-25 14:12
浏览 41


I've been attempting to take a swing at concurrency in Golang by refactoring one of my command-line utilities over the past few days, but I'm stuck.

Here's the original code (master branch).

Here's the branch with concurrency (x_concurrent branch).

When I execute the concurrent code with go run jira_open_comment_emailer.go, the defer wg.Done() never executes if the JIRA issue is added to the channel here, which causes my wg.Wait() to hang forever.

The idea is that I have a large amount of JIRA issues, and I want to spin off a goroutine for each one to see if it has a comment I need to respond to. If it does, I want to add it to some structure (I chose a channel after some research) that I can read from like a queue later to build up an email reminder.

Here's the relevant section of the code:

// Given an issue, determine if it has an open comment
// Returns true if there is an open comment on the issue, otherwise false
func getAndProcessComments(issue Issue, channel chan<- Issue, wg *sync.WaitGroup) {
    // Decrement the wait counter when the function returns
    defer wg.Done()

    needsReply := false

    // Loop over the comments in the issue
    for _, comment := range issue.Fields.Comment.Comments {
        commentMatched, err := regexp.MatchString("~"+config.JIRAUsername, comment.Body)
        checkError("Failed to regex match against comment body", err)

        if commentMatched {
            needsReply = true

        if comment.Author.Name == config.JIRAUsername {
            needsReply = false

    // Only add the issue to the channel if it needs a reply
    if needsReply == true {
        // This never allows the defered wg.Done() to execute?
        channel <- issue

func main() {
    start := time.Now()

    // This retrieves all issues in a search from JIRA
    allIssues := getFullIssueList()

    // Initialize a wait group
    var wg sync.WaitGroup

    // Set the number of waits to the number of issues to process

    // Create a channel to store issues that need a reply
    channel := make(chan Issue)

    for _, issue := range allIssues {
        go getAndProcessComments(issue, channel, &wg)

    // Block until all of my goroutines have processed their issues.

    // Only send an email if the channel has one or more issues
    if len(channel) > 0 {

    fmt.Printf("Script ran in %s", time.Since(start))
  • 写回答

1条回答 默认 最新

  • dongxing2710 2016-05-25 15:05

    The goroutines block on sending to the unbuffered channel. A minimal change unblocks the goroutines is to create a buffered channel with capacity for all issues:

    channel := make(chan Issue, len(allIssues))

    and close the channel after the call to wg.Wait().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥30 sort cuteSV.vcf by bcftools用IGV可视化出现报错
  • ¥100 SOS!对STK中导出的天体图像进行质心提取有没有人做过啊
  • ¥15 python 欧式距离
  • ¥15 运行qteasy报错
  • ¥15 遗传算法解决有工序顺序约束的大规模FJSP问题
  • ¥15 企业消防水炮塔设计方案
  • ¥20 WORKBENCH网格划分
  • ¥60 急招师兄远程解决下载NPCAP的BUG!
  • ¥15 关于#51单片机#的问题:51单片机LM1602的数据只能显示一个字符,在使用矩形键盘送数据的时候不能显示出来而是在显示初始位置上,达不到密码锁的效果
  • ¥15 求旧版本ns-2软件