github.com/ethersphere/bee/v2@v2.2.0/pkg/util/syncutil/syncutil.go (about)

     1  // Copyright 2023 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package syncutil
     6  
     7  import (
     8  	"sync"
     9  	"time"
    10  )
    11  
    12  // WaitWithTimeout waits for the waitgroup to finish for the given timeout.
    13  // It returns true if the waitgroup finished before the timeout, false otherwise.
    14  func WaitWithTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
    15  	c := make(chan struct{})
    16  	go func() {
    17  		wg.Wait()
    18  		close(c)
    19  	}()
    20  	select {
    21  	case <-c:
    22  		return true
    23  	case <-time.After(timeout):
    24  		return false
    25  	}
    26  }