2023-12-01 21:29:19 +00:00
|
|
|
package util
|
|
|
|
|
2023-12-02 02:13:42 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
2023-12-01 21:29:19 +00:00
|
|
|
|
|
|
|
func GetTime() uint64 {
|
|
|
|
return uint64(time.Now().UnixMilli())
|
|
|
|
}
|
2023-12-02 02:13:42 +00:00
|
|
|
|
2023-12-02 02:23:27 +00:00
|
|
|
func SelectWithTimeout(ch <-chan struct{}, timeout time.Duration) bool {
|
2023-12-02 02:13:42 +00:00
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
return true
|
2023-12-02 02:23:27 +00:00
|
|
|
case <-time.After(timeout):
|
2023-12-02 02:13:42 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 02:23:27 +00:00
|
|
|
func WaitWithTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
|
2023-12-02 02:13:42 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
|
2023-12-02 02:23:27 +00:00
|
|
|
return SelectWithTimeout(done, timeout)
|
2023-12-02 02:13:42 +00:00
|
|
|
}
|