Skip to content

Instantly share code, notes, and snippets.

@nesv
Last active December 11, 2018 21:41
Show Gist options
  • Save nesv/9233300 to your computer and use it in GitHub Desktop.
Save nesv/9233300 to your computer and use it in GitHub Desktop.
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
}()
}
}
}()
}
@satarsa
Copy link

satarsa commented Feb 15, 2018

I have tried this example. What I have found out that if you stop your workers, then these goroutines

go func() {
          worker := <-WorkerQueue
          fmt.Println("Dispatching work request")
          worker <- work

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment