Add simple cache warmer, disabled by default
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package pool
|
||||
|
||||
type Executor func(workload interface{})
|
||||
|
||||
type Pool struct {
|
||||
name string
|
||||
item interface{}
|
||||
workers []worker
|
||||
exec Executor
|
||||
//queue *dque.DQue
|
||||
queue chan work // receives jobs to send to workers
|
||||
end chan bool // when receives bool stops workers
|
||||
}
|
||||
|
||||
func NewPool(name string, workerCount int, item interface{}, exec Executor) (*Pool, error) {
|
||||
p := &Pool{
|
||||
name: name,
|
||||
item: item,
|
||||
exec: exec,
|
||||
queue: make(chan work),
|
||||
end: make(chan bool),
|
||||
}
|
||||
|
||||
//q, err := dque.NewOrOpen(name, filepath.Join(conf.Server.DataFolder, "queues", name), 50, p.itemBuilder)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//p.queue = q
|
||||
for i := 0; i < workerCount; i++ {
|
||||
worker := worker{
|
||||
p: p,
|
||||
id: i,
|
||||
channel: make(chan work),
|
||||
workerChannel: workerChannel,
|
||||
end: make(chan bool)}
|
||||
worker.Start()
|
||||
p.workers = append(p.workers, worker)
|
||||
}
|
||||
|
||||
// start pool
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-p.end:
|
||||
for _, w := range p.workers {
|
||||
w.Stop() // stop worker
|
||||
}
|
||||
return
|
||||
case work := <-p.queue:
|
||||
worker := <-workerChannel // wait for available channel
|
||||
worker <- work // dispatch work to worker
|
||||
}
|
||||
}
|
||||
}()
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *Pool) Submit(workload interface{}) {
|
||||
p.queue <- work{workload}
|
||||
}
|
||||
|
||||
//func (p *Pool) itemBuilder() interface{} {
|
||||
// t := reflect.TypeOf(p.item)
|
||||
// return reflect.New(t).Interface()
|
||||
//}
|
||||
//
|
||||
var workerChannel = make(chan chan work)
|
||||
|
||||
type work struct {
|
||||
workload interface{}
|
||||
}
|
||||
|
||||
type worker struct {
|
||||
id int
|
||||
p *Pool
|
||||
workerChannel chan chan work // used to communicate between dispatcher and workers
|
||||
channel chan work
|
||||
end chan bool
|
||||
}
|
||||
|
||||
// start worker
|
||||
func (w *worker) Start() {
|
||||
go func() {
|
||||
for {
|
||||
w.workerChannel <- w.channel // when the worker is available place channel in queue
|
||||
select {
|
||||
case job := <-w.channel: // worker has received job
|
||||
w.p.exec(job.workload) // do work
|
||||
case <-w.end:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// end worker
|
||||
func (w *worker) Stop() {
|
||||
w.end <- true
|
||||
}
|
||||
Reference in New Issue
Block a user