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

     1  package queue
     2  
     3  func newEventQueue() (chan<- struct{}, <-chan struct{}) {
     4  	send := make(chan struct{}, 1)
     5  	receive := make(chan struct{}, 1)
     6  	go manageEventQueue(send, receive)
     7  	return send, receive
     8  }
     9  
    10  func manageEventQueue(send <-chan struct{}, receive chan<- struct{}) {
    11  	numInQueue := 0
    12  	for {
    13  		if numInQueue < 1 {
    14  			if send == nil {
    15  				close(receive)
    16  				return
    17  			}
    18  			_, ok := <-send
    19  			if !ok {
    20  				close(receive)
    21  				return
    22  			}
    23  			numInQueue++
    24  		} else {
    25  			select {
    26  			case receive <- struct{}{}:
    27  				numInQueue--
    28  			case _, ok := <-send:
    29  				if ok {
    30  					numInQueue++
    31  				} else {
    32  					send = nil
    33  				}
    34  			}
    35  		}
    36  	}
    37  }