github.com/Cloud-Foundations/Dominator@v0.3.4/lib/queue/dataQueue.go (about)

     1  package queue
     2  
     3  import "container/list"
     4  
     5  func newDataQueue() (chan<- interface{}, <-chan interface{}) {
     6  	send := make(chan interface{}, 1)
     7  	receive := make(chan interface{}, 1)
     8  	go manageDataQueue(send, receive)
     9  	return send, receive
    10  }
    11  
    12  func manageDataQueue(send <-chan interface{}, receive chan<- interface{}) {
    13  	queue := list.New()
    14  	for {
    15  		if front := queue.Front(); front == nil {
    16  			if send == nil {
    17  				close(receive)
    18  				return
    19  			}
    20  			value, ok := <-send
    21  			if !ok {
    22  				close(receive)
    23  				return
    24  			}
    25  			queue.PushBack(value)
    26  		} else {
    27  			select {
    28  			case receive <- front.Value:
    29  				queue.Remove(front)
    30  			case value, ok := <-send:
    31  				if ok {
    32  					queue.PushBack(value)
    33  				} else {
    34  					send = nil
    35  				}
    36  			}
    37  		}
    38  	}
    39  }