goroutine leak with buffered channel in Go -
th following codes in the go programming language
func mirroredquery() string { responses := make(chan string, 3) go func() { responses <- request("asia.gopl.io") }() go func() { responses <- request("europe.gopl.io") }() go func() { responses <- request("americas.gopl.io") }() return <-responses // return quickest response } func request(hostname string) (response string) { /* ... */ }
and book says
had used unbuffered channel, 2 slower goroutines have gotten stuck trying send responses on channel no goroutine ever receive . situation, called goroutine leak, bug . unlike garbage variables, leaked goroutines not automatically collec ted, important make sure goroutines terminate when no longer needed.
, question why situation cause goroutine
leak.in idea, buffered channel's cap
3, , 3 goroutines
send requests , exit not cause leak.
the code shown not cause leak.
as paragraph states:
had used unbuffered channel
meaning: if had used unbuffered channel...
so if channel unbuffered leak occur.
Comments
Post a Comment