问题
As it will be made obvious soon, I am a golang n00b.
I have some go code that starts goroutines based on an event channel. Say it starts 2 goroutines because we receive 2 events of type START.
The goroutine is started with an uri as parameter, which gives us something unique about it.
Later we receive one event of type STOP.
How can I stop the goroutine that was started with the same uri ?
for {
select {
case event := <-eventCh:
if event.Entry != nil {
switch event.Action {
case foo.START:
log.Println("uri: ", event.Entry.URI)
go func(c chan []byte, u string) error{
//awesome goroutine code
}(myChan, event.Entry.URI)
case foo.STOP:
log.Println("uri: ", event.Entry.URI)
//I'd like to terminate the goroutine that matches event.Entry.URI
}
}
}
}
回答1:
You cannot stop a goroutine "from the outside". You have to pass some kind of cancellation signal to each goroutine and remember them for later in the main goroutine. A Context is typically used as a cancellation signal. The goroutine then has to check for cancellation and exit voluntarily:
package main
import (
"context"
)
type Event struct {
Action string
URI string
}
func main() {
var eventCh chan Event
ctx := context.Background()
cancels := make(map[string]context.CancelFunc) // Maps URIs to cancellation functions.
for event := range eventCh {
switch event.Action {
case "START":
if cancels[event.URI] != nil {
panic("duplicate URI: " + event.URI)
}
ctx, cancel := context.WithCancel(ctx)
cancels[event.URI] = cancel
defer cancel() // cancel must always be called to free resources.
go func(u string) {
// Awesome goroutine code
// Check ctx.Done or ctx.Err in strategic places and return if done.
select {
case <-ctx.Done():
return
default:
}
// More awesome goroutine code
if ctx.Err() != nil {
return
}
// Even more awesome goroutine code
}(event.URI)
case "STOP":
if cancel, ok := cancels[event.URI]; ok {
cancel()
delete(cancels, event.URI)
}
}
}
}
来源:https://stackoverflow.com/questions/55246721/how-to-stop-one-of-multilpe-of-the-same-goroutine