Refactor Package structure
This commit cleans up the package structure, to make two new top-level packages `internal` (for internal-use packages) and `pkg` (for general shared utility code).
This commit is contained in:
parent
487ce09979
commit
a360324f62
124 changed files with 97 additions and 101 deletions
31
pkg/wait/wait.go
Normal file
31
pkg/wait/wait.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package wait
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Wait repeatedly invokes f, until it returns true or the context is closed.
|
||||
// The invocation interval is determined by interval.
|
||||
func Wait(f func() bool, interval time.Duration, context context.Context) error {
|
||||
// create a new timer
|
||||
timer := time.NewTimer(interval)
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
defer timer.Stop()
|
||||
|
||||
for {
|
||||
if f() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// reset the timer, and wait for it again!
|
||||
timer.Reset(interval)
|
||||
select {
|
||||
case <-timer.C:
|
||||
case <-context.Done():
|
||||
return context.Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue