harness-drone/server/worker/dispatch.go

51 lines
1 KiB
Go
Raw Normal View History

package worker
2014-07-13 02:01:58 +00:00
import (
"github.com/drone/drone/shared/model"
)
// http://nesv.github.io/golang/2014/02/25/worker-queues-in-go.html
type Dispatch struct {
2014-07-13 02:01:58 +00:00
requests chan *model.Request
workers chan chan *model.Request
quit chan bool
}
2014-07-13 02:01:58 +00:00
func NewDispatch(requests chan *model.Request, workers chan chan *model.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 }()
}