Last active
December 11, 2018 21:41
-
-
Save nesv/9233300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
var WorkerQueue chan chan WorkRequest | |
func StartDispatcher(nworkers int) { | |
// First, initialize the channel we are going to but the workers' work channels into. | |
WorkerQueue = make(chan chan WorkRequest, nworkers) | |
// Now, create all of our workers. | |
for i := 0; i<nworkers; i++ { | |
fmt.Println("Starting worker", i+1) | |
worker := NewWorker(i+1, WorkerQueue) | |
worker.Start() | |
} | |
go func() { | |
for { | |
select { | |
case work := <-WorkQueue: | |
fmt.Println("Received work requeust") | |
go func() { | |
worker := <-WorkerQueue | |
fmt.Println("Dispatching work request") | |
worker <- work | |
}() | |
} | |
} | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have tried this example. What I have found out that if you stop your workers, then these goroutines
will be blocked forever and abandonned, i.e. they just leak. If I do not stop the workers, I don't understand, how I can cancel the previous workRequests.