core/utils/timeout.go
2024-07-31 20:14:46 +07:00

21 lines
302 B
Go

package utils
import (
"sync"
"time"
)
func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
return true // timed out
}
}