package producer

import (
	"container/list"
)

// 블럭되지 않는 큐체널
func NewOSMetricQueue() (chan<- OSMetricInfo, <-chan OSMetricInfo) {
	send := make(chan OSMetricInfo, 1)
	receive := make(chan OSMetricInfo, 1)
	go manageOSMetricQueue(send, receive)
	return send, receive
}

func manageOSMetricQueue(send <-chan OSMetricInfo, receive chan<- OSMetricInfo) {
	queue := list.New()
	defer close(receive)
	for {
		if front := queue.Front(); front == nil {
			if value, ok := <-send; ok {
				queue.PushBack(value)
			} else {
				break
			}
		} else {
			select {
			case receive <- front.Value.(OSMetricInfo):
				queue.Remove(front)
			case value, ok := <-send:
				if ok {
					queue.PushBack(value)
				}
			}
		}
	}
}
