Files
scroll/common/utils/utils.go
maskpp c13e8aafc4 feat(tests): update docker app (#402)
Co-authored-by: Péter Garamvölgyi <peter@scroll.io>
Co-authored-by: ChuhanJin <60994121+ChuhanJin@users.noreply.github.com>
Co-authored-by: vincent <419436363@qq.com>
Co-authored-by: colinlyguo <651734127@qq.com>
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
2023-04-13 08:40:51 +08:00

52 lines
948 B
Go

package utils
import (
"context"
"time"
"github.com/modern-go/reflect2"
)
// TryTimes try run several times until the function return true.
func TryTimes(times int, run func() bool) {
for i := 0; i < times; i++ {
if run() {
return
}
time.Sleep(time.Millisecond * 500)
}
}
// LoopWithContext Run the f func with context periodically.
func LoopWithContext(ctx context.Context, period time.Duration, f func(ctx context.Context)) {
tick := time.NewTicker(period)
defer tick.Stop()
for ; ; <-tick.C {
select {
case <-ctx.Done():
return
default:
f(ctx)
}
}
}
// Loop Run the f func periodically.
func Loop(ctx context.Context, period time.Duration, f func()) {
tick := time.NewTicker(period)
defer tick.Stop()
for ; ; <-tick.C {
select {
case <-ctx.Done():
return
default:
f()
}
}
}
// IsNil Check if the interface is empty.
func IsNil(i interface{}) bool {
return i == nil || reflect2.IsNil(i)
}