queue_go/queue.go

118 lines
2.1 KiB
Go
Raw Normal View History

2017-04-26 06:45:40 +00:00
package queue
import (
"container/heap"
2017-04-26 06:46:04 +00:00
"loafle.com/overflow/agent_api/observer"
2017-04-27 06:23:04 +00:00
"sync"
2017-04-26 06:46:04 +00:00
"time"
2017-04-26 06:45:40 +00:00
)
2017-04-26 07:48:49 +00:00
type Item struct {
Value interface{}
Priority int
2017-04-26 06:45:40 +00:00
}
type LoafleQueue struct {
2017-04-27 06:23:04 +00:00
mtx sync.Mutex
2017-04-26 07:48:49 +00:00
items []*Item
2017-04-27 06:23:04 +00:00
queueType string
2017-04-26 06:46:04 +00:00
interval time.Duration
2017-04-27 08:38:35 +00:00
size int
2017-04-26 06:45:40 +00:00
2017-04-27 06:23:04 +00:00
eventChanel chan interface{}
senderChanel chan interface{}
2017-04-26 06:45:40 +00:00
}
func (lq LoafleQueue) Len() int {
return len(lq.items)
}
func (lq LoafleQueue) Less(i, j int) bool {
2017-04-26 07:48:49 +00:00
return lq.items[i].Priority < lq.items[j].Priority
2017-04-26 06:45:40 +00:00
}
func (lq LoafleQueue) Swap(i, j int) {
lq.items[i], lq.items[j] = lq.items[j], lq.items[i]
}
2017-04-27 06:23:04 +00:00
func (lq *LoafleQueue) getItems() {
2017-04-26 06:45:40 +00:00
2017-04-27 06:23:04 +00:00
for {
time.Sleep(time.Second * lq.interval)
2017-04-27 08:38:35 +00:00
2017-04-27 06:23:04 +00:00
if lq.Len() > 0 {
2017-04-27 08:38:35 +00:00
lq.size = lq.Len()
resultItems := make([]*Item, 0)
2017-04-27 06:23:04 +00:00
lq.mtx.Lock()
2017-04-27 08:38:35 +00:00
for i := 0; i < lq.size; i++ {
2017-04-27 06:23:04 +00:00
item := heap.Pop(lq).(*Item)
resultItems = append(resultItems, item)
}
lq.mtx.Unlock()
lq.senderChanel <- resultItems
2017-04-27 08:38:35 +00:00
lq.size = lq.Len()
2017-04-27 06:23:04 +00:00
}
2017-04-26 06:45:40 +00:00
}
2017-04-27 06:23:04 +00:00
2017-04-26 06:45:40 +00:00
}
func (lq *LoafleQueue) Push(i interface{}) {
2017-04-27 06:23:04 +00:00
lq.mtx.Lock()
defer lq.mtx.Unlock()
2017-04-26 06:45:40 +00:00
n := len(lq.items)
2017-04-26 07:48:49 +00:00
nItem := i.(*Item)
nItem.Priority = n
2017-04-26 06:45:40 +00:00
lq.items = append(lq.items, nItem)
}
func (lq *LoafleQueue) Pop() interface{} {
old := lq.items
n := len(old)
nItem := old[n-1]
lq.items = old[0 : n-1]
return nItem
}
2017-04-26 07:48:49 +00:00
func (lq LoafleQueue) newItem(value interface{}) *Item {
return &Item{
Value: value,
2017-04-26 06:45:40 +00:00
}
}
func (lq *LoafleQueue) notifyEventHandler(c chan interface{}) {
for data := range c {
it := lq.newItem(data)
heap.Push(lq, it)
}
}
func (lq *LoafleQueue) Close() {
if lq.eventChanel != nil {
2017-04-27 06:23:04 +00:00
observer.Remove(lq.queueType, lq.eventChanel)
2017-04-26 06:45:40 +00:00
}
2017-04-27 06:35:35 +00:00
lq.items = nil
2017-04-26 06:45:40 +00:00
}
2017-04-27 06:23:04 +00:00
func NewQueue(eventType string, interval time.Duration, senderChanel chan interface{}) *LoafleQueue {
2017-04-26 07:48:49 +00:00
items := make([]*Item, 0)
2017-04-26 06:46:04 +00:00
event := make(chan interface{}, 0)
2017-04-26 06:45:40 +00:00
lq := &LoafleQueue{
2017-04-27 06:23:04 +00:00
items: items,
queueType: eventType,
interval: interval,
eventChanel: event,
senderChanel: senderChanel,
2017-04-26 06:45:40 +00:00
}
2017-04-26 06:58:38 +00:00
heap.Init(lq)
2017-04-27 06:23:04 +00:00
observer.Add(eventType, lq.eventChanel)
2017-04-26 06:45:40 +00:00
go lq.notifyEventHandler(event)
2017-04-27 06:23:04 +00:00
go lq.getItems()
2017-04-26 06:45:40 +00:00
return lq
}