github.com/xmidt-org/webpa-common@v1.11.9/concurrent/waitTimeout.go (about)

     1  package concurrent
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  // WaitTimeout performs a timed wait on a given sync.WaitGroup
     9  func WaitTimeout(waitGroup *sync.WaitGroup, timeout time.Duration) bool {
    10  	success := make(chan struct{})
    11  	go func() {
    12  		defer func() {
    13  			// swallow any panics, as they'll just be from the channel
    14  			// close if the timeout elapsed
    15  			recover()
    16  		}()
    17  		defer close(success)
    18  		waitGroup.Wait()
    19  	}()
    20  
    21  	timer := time.NewTimer(timeout)
    22  	defer timer.Stop()
    23  
    24  	select {
    25  	case <-success:
    26  		return true
    27  	case <-timer.C:
    28  		return false
    29  	}
    30  }