harness-drone/server/worker/dispatch.go
Brad Rydzewski 83577a7d5d removed css files. designers will be providing
removed amber files. replacing with angular
removed queue package in favor or worker package
removed channel package in favor of pubsub package
2014-06-21 14:22:38 -07:00

46 lines
960 B
Go

package worker
// http://nesv.github.io/golang/2014/02/25/worker-queues-in-go.html
type Dispatch struct {
requests chan *Request
workers chan chan *Request
quit chan bool
}
func NewDispatch(requests chan *Request, workers chan chan *Request) *Dispatch {
return &Dispatch{
requests: requests,
workers: workers,
quit: make(chan bool),
}
}
// Start tells the dispatcher to start listening
// for work requests and dispatching to workers.
func (d *Dispatch) Start() {
go func() {
for {
select {
// pickup a request from the queue
case request := <-d.requests:
go func() {
// find an available worker and
// send the request to that worker
worker := <-d.workers
worker <- request
}()
// listen for a signal to exit
case <-d.quit:
return
}
}
}()
}
// Stop tells the dispatcher to stop listening for new
// work requests.
func (d *Dispatch) Stop() {
go func() { d.quit <- true }()
}