interruptible_wait: allow nil Predicate
Allow for the case where InterruptibleWait is used purely for the timeout. See the documentation string for details.
This commit is contained in:
parent
651274e508
commit
ba7c5ddcd2
@ -6,9 +6,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type InterruptibleWait struct {
|
type InterruptibleWait struct {
|
||||||
|
Timeout time.Duration
|
||||||
|
|
||||||
|
// optional:
|
||||||
Predicate func() (result bool, err error)
|
Predicate func() (result bool, err error)
|
||||||
PredicateInterval time.Duration
|
PredicateInterval time.Duration
|
||||||
Timeout time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TimeoutError struct{}
|
type TimeoutError struct{}
|
||||||
@ -28,26 +30,34 @@ type PredicateResult struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Wait waits for up to Timeout duration, checking an optional Predicate every PredicateInterval duration.
|
||||||
|
The first run of Predicate is immediately after Wait is called.
|
||||||
|
If the command is interrupted by the user, then an InterruptedError is returned.
|
||||||
|
If Predicate is not nil, a timeout leads to TimeoutError being returned, and a successful Predicate run leads to nil being returned.
|
||||||
|
If Predicate is nil, a timeout is not an error, and nil is returned.
|
||||||
|
*/
|
||||||
func (wait InterruptibleWait) Wait(state multistep.StateBag) error {
|
func (wait InterruptibleWait) Wait(state multistep.StateBag) error {
|
||||||
predicateResult := make(chan PredicateResult, 1)
|
predicateResult := make(chan PredicateResult, 1)
|
||||||
stopWaiting := make(chan struct{})
|
stopWaiting := make(chan struct{})
|
||||||
defer close(stopWaiting)
|
defer close(stopWaiting)
|
||||||
|
|
||||||
go func() {
|
if wait.Predicate != nil {
|
||||||
for {
|
go func() {
|
||||||
if complete, err := wait.Predicate(); err != nil || complete {
|
for {
|
||||||
predicateResult <- PredicateResult{complete, err}
|
if complete, err := wait.Predicate(); err != nil || complete {
|
||||||
return
|
predicateResult <- PredicateResult{complete, err}
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-time.After(wait.PredicateInterval):
|
case <-time.After(wait.PredicateInterval):
|
||||||
// do nothing; loop again
|
// do nothing; loop again
|
||||||
case <-stopWaiting:
|
case <-stopWaiting:
|
||||||
return
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
}()
|
}
|
||||||
|
|
||||||
timeout := time.After(wait.Timeout)
|
timeout := time.After(wait.Timeout)
|
||||||
for {
|
for {
|
||||||
@ -63,7 +73,11 @@ func (wait InterruptibleWait) Wait(state multistep.StateBag) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case <-timeout:
|
case <-timeout:
|
||||||
return TimeoutError{}
|
if wait.Predicate != nil {
|
||||||
|
return TimeoutError{}
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user