util: added SelectWithTimeout && WaitWithTImeout

This commit is contained in:
2023-12-01 20:13:42 -06:00
parent 557117f093
commit 3445b852fd
2 changed files with 28 additions and 25 deletions

View File

@@ -1,7 +1,29 @@
package util
import "time"
import (
"sync"
"time"
)
func GetTime() uint64 {
return uint64(time.Now().UnixMilli())
}
func SelectWithTimeout(ch <-chan struct{}, seconds int) bool {
select {
case <-ch:
return true
case <-time.After(time.Duration(seconds) * time.Second):
return false
}
}
func WaitWithTimeout(wg *sync.WaitGroup, seconds int) bool {
done := make(chan struct{})
go func() {
defer close(done)
wg.Wait()
}()
return SelectWithTimeout(done, seconds)
}