diff --git a/queue.go b/queue.go index 7425f43..0ad95ce 100644 --- a/queue.go +++ b/queue.go @@ -17,6 +17,7 @@ type LoafleQueue struct { items []*Item queueType string interval time.Duration + size int eventChanel chan interface{} senderChanel chan interface{} @@ -40,16 +41,19 @@ func (lq *LoafleQueue) getItems() { for { time.Sleep(time.Second * lq.interval) - resultItems := make([]*Item, 0) + if lq.Len() > 0 { + lq.size = lq.Len() + resultItems := make([]*Item, 0) lq.mtx.Lock() - for i := 0; i < lq.Len(); i++ { + for i := 0; i < lq.size; i++ { item := heap.Pop(lq).(*Item) resultItems = append(resultItems, item) } lq.mtx.Unlock() lq.senderChanel <- resultItems + lq.size = lq.Len() } } diff --git a/queue_test.go b/queue_test.go index 00d3ea6..533c608 100644 --- a/queue_test.go +++ b/queue_test.go @@ -4,6 +4,7 @@ import ( "fmt" "loafle.com/overflow/agent_api/observer" "loafle.com/overflow/agent_api/observer/messages" + "log" "reflect" "testing" "time" @@ -18,6 +19,21 @@ func TestNewQueue(t *testing.T) { observer.Notify(messages.QUEUE_DATA, "data") observer.Notify(messages.QUEUE_DATA, "data1111") + observer.Notify(messages.QUEUE_DATA, "data2222") + observer.Notify(messages.QUEUE_DATA, "data3333") + observer.Notify(messages.QUEUE_DATA, "data4444") + observer.Notify(messages.QUEUE_DATA, "data5555") + observer.Notify(messages.QUEUE_DATA, "data6666") + observer.Notify(messages.QUEUE_DATA, "data7777") + observer.Notify(messages.QUEUE_DATA, "data8888") + observer.Notify(messages.QUEUE_DATA, "data9999") + observer.Notify(messages.QUEUE_DATA, "data1111") + observer.Notify(messages.QUEUE_DATA, "data2222") + observer.Notify(messages.QUEUE_DATA, "data3333") + observer.Notify(messages.QUEUE_DATA, "data4444") + observer.Notify(messages.QUEUE_DATA, "data5555") + observer.Notify(messages.QUEUE_DATA, "data6666") + observer.Notify(messages.QUEUE_DATA, "data7777") go GetItemTest(senderChanel) @@ -34,10 +50,11 @@ func GetItemTest(senderChanel chan interface{}) { if items.Kind() != reflect.Slice { fmt.Println("ddddd") } - + //log.Println(items.Len()) for i := 0; i < items.Len(); i++ { d := items.Index(i) - fmt.Println(d) + log.Println(d) } + //fmt.Println(sc) } }