monitor a good
Former-commit-id: 5a37bfdfe208d3d1a71e6b4924209d0c8e6f53a0
This commit is contained in:
67
monitor/monitor.go
Normal file
67
monitor/monitor.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"local/rssmon3/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Monitor struct {
|
||||
queue *Queue
|
||||
Incoming chan *Item
|
||||
Outgoing chan *Item
|
||||
}
|
||||
|
||||
func New() *Monitor {
|
||||
return &Monitor{
|
||||
queue: newQueue(),
|
||||
Incoming: make(chan *Item),
|
||||
Outgoing: make(chan *Item),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Monitor) Run() error {
|
||||
for {
|
||||
select {
|
||||
case <-m.stopped():
|
||||
return nil
|
||||
case i := <-m.enqueued():
|
||||
m.enqueue(i)
|
||||
continue
|
||||
case <-m.triggered():
|
||||
m.trigger()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Monitor) stopped() <-chan struct{} {
|
||||
return config.Values().Ctx.Done()
|
||||
}
|
||||
|
||||
func (m *Monitor) enqueued() chan *Item {
|
||||
return m.Incoming
|
||||
}
|
||||
|
||||
func (m *Monitor) enqueue(i *Item) {
|
||||
m.queue.Push(i)
|
||||
}
|
||||
|
||||
func (m *Monitor) triggered() <-chan time.Time {
|
||||
if m.queue.Len() < 1 {
|
||||
return nil
|
||||
}
|
||||
top := m.queue.Peek()
|
||||
if top == nil {
|
||||
return nil
|
||||
}
|
||||
return time.After(time.Until(top.Last().Add(top.Interval())))
|
||||
}
|
||||
|
||||
func (m *Monitor) trigger() {
|
||||
i := m.queue.Pop()
|
||||
if i == nil {
|
||||
return
|
||||
}
|
||||
m.Outgoing <- i
|
||||
i.Mark()
|
||||
m.queue.Push(i)
|
||||
}
|
||||
Reference in New Issue
Block a user