[英]Why is my Golang Channel Write Blocking Forever?
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. 在过去的几天里,我一直试图通过重构我的一个命令行实用程序来解决Golang的并发问题,但我陷入了困境。
Here's the original code (master branch). 这是原始代码(主分支)。
Here's the branch with concurrency (x_concurrent branch). 这是具有并发性的分支(x_concurrent分支)。
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. 当我使用go run jira_open_comment_emailer.go
执行并发代码时,如果将JIRA问题添加到此处的通道,则defer wg.Done()
永远不会执行,这会导致我的wg.Wait()
永远挂起。
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. 这个想法是我有大量的JIRA问题,我想为每个问题分拆一个goroutine,看看它是否有我需要回应的评论。 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
wg.Add(len(allIssues))
// 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.
wg.Wait()
// Only send an email if the channel has one or more issues
if len(channel) > 0 {
sendEmail(channel)
}
fmt.Printf("Script ran in %s", time.Since(start))
}
The goroutines block on sending to the unbuffered channel. goroutines阻止发送到无缓冲的通道。 A minimal change unblocks the goroutines is to create a buffered channel with capacity for all issues: 解决goroutines的最小变化是创建一个缓冲通道,其容量适用于所有问题:
channel := make(chan Issue, len(allIssues))
and close the channel after the call to wg.Wait(). 并在调用wg.Wait()后关闭通道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.