2013-01-04 11:17:31 +00:00
|
|
|
package retrieval
|
|
|
|
|
|
|
|
import (
|
2013-01-04 13:41:47 +00:00
|
|
|
"container/heap"
|
|
|
|
"log"
|
2013-01-04 11:17:31 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-01-04 13:41:47 +00:00
|
|
|
type TargetPool struct {
|
|
|
|
done chan bool
|
2013-01-15 16:06:17 +00:00
|
|
|
targets []Target
|
2013-01-04 13:41:47 +00:00
|
|
|
manager TargetManager
|
|
|
|
}
|
|
|
|
|
2013-01-15 16:06:17 +00:00
|
|
|
func NewTargetPool(m TargetManager) (p *TargetPool) {
|
|
|
|
return &TargetPool{
|
|
|
|
manager: m,
|
|
|
|
}
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2013-01-04 11:17:31 +00:00
|
|
|
|
|
|
|
func (p TargetPool) Len() int {
|
2013-01-04 13:41:47 +00:00
|
|
|
return len(p.targets)
|
2013-01-04 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p TargetPool) Less(i, j int) bool {
|
2013-01-12 20:22:59 +00:00
|
|
|
return p.targets[i].scheduledFor().Before(p.targets[j].scheduledFor())
|
2013-01-04 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) Pop() interface{} {
|
2013-01-04 13:41:47 +00:00
|
|
|
oldPool := p.targets
|
2013-01-04 11:17:31 +00:00
|
|
|
futureLength := p.Len() - 1
|
|
|
|
element := oldPool[futureLength]
|
|
|
|
futurePool := oldPool[0:futureLength]
|
2013-01-04 13:41:47 +00:00
|
|
|
p.targets = futurePool
|
2013-01-04 11:17:31 +00:00
|
|
|
|
|
|
|
return element
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) Push(element interface{}) {
|
2013-01-15 16:06:17 +00:00
|
|
|
p.targets = append(p.targets, element.(Target))
|
2013-01-04 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p TargetPool) Swap(i, j int) {
|
2013-01-04 13:41:47 +00:00
|
|
|
p.targets[i], p.targets[j] = p.targets[j], p.targets[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) Run(results chan Result, interval time.Duration) {
|
|
|
|
ticker := time.Tick(interval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker:
|
|
|
|
p.runIteration(results)
|
|
|
|
case <-p.done:
|
|
|
|
log.Printf("TargetPool exiting...")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p TargetPool) Stop() {
|
|
|
|
p.done <- true
|
|
|
|
}
|
|
|
|
|
2013-01-15 16:06:17 +00:00
|
|
|
func (p *TargetPool) runSingle(earliest time.Time, results chan Result, t Target) {
|
2013-01-04 13:41:47 +00:00
|
|
|
p.manager.acquire()
|
|
|
|
defer p.manager.release()
|
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
t.Scrape(earliest, results)
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TargetPool) runIteration(results chan Result) {
|
|
|
|
for i := 0; i < p.Len(); i++ {
|
2013-01-15 16:06:17 +00:00
|
|
|
target := heap.Pop(p).(Target)
|
2013-01-04 13:41:47 +00:00
|
|
|
if target == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
if target.scheduledFor().After(now) {
|
2013-01-04 13:41:47 +00:00
|
|
|
heap.Push(p, target)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2013-01-12 20:22:59 +00:00
|
|
|
p.runSingle(now, results, target)
|
2013-01-04 13:41:47 +00:00
|
|
|
heap.Push(p, target)
|
|
|
|
}()
|
|
|
|
}
|
2013-01-04 11:17:31 +00:00
|
|
|
}
|