-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: export WaitObserver methods into wait package
Signed-off-by: Gilberto Bertin <[email protected]>
- Loading branch information
Showing
4 changed files
with
90 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package wait | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/cilium/cilium-cli/defaults" | ||
) | ||
|
||
type LogFunc func(err error, waitTime string) | ||
|
||
type Parameters struct { | ||
RetryInterval time.Duration | ||
WarningInterval time.Duration | ||
Timeout time.Duration | ||
Log LogFunc | ||
} | ||
|
||
func (w Parameters) retryInterval() time.Duration { | ||
if w.RetryInterval != time.Duration(0) { | ||
return w.RetryInterval | ||
} | ||
|
||
return defaults.WaitRetryInterval | ||
} | ||
|
||
func (w Parameters) warningInterval() time.Duration { | ||
if w.WarningInterval != time.Duration(0) { | ||
return w.WarningInterval | ||
} | ||
|
||
return defaults.WaitWarningInterval | ||
} | ||
|
||
type Observer struct { | ||
ctx context.Context | ||
params Parameters | ||
lastWarning time.Time | ||
waitStarted time.Time | ||
cancel context.CancelFunc | ||
} | ||
|
||
func NewObserver(ctx context.Context, p Parameters) *Observer { | ||
w := &Observer{ | ||
ctx: ctx, | ||
params: p, | ||
waitStarted: time.Now(), | ||
} | ||
|
||
if p.Timeout != time.Duration(0) { | ||
w.ctx, w.cancel = context.WithTimeout(ctx, p.Timeout) | ||
} | ||
|
||
return w | ||
} | ||
|
||
func (w *Observer) Cancel() { | ||
if w.cancel != nil { | ||
w.cancel() | ||
} | ||
} | ||
|
||
func (w *Observer) Retry(err error) error { | ||
if w.params.Log != nil && time.Since(w.lastWarning) > w.params.warningInterval() { | ||
waitString := time.Since(w.waitStarted).Truncate(time.Second).String() | ||
w.params.Log(err, waitString) | ||
w.lastWarning = time.Now() | ||
} | ||
|
||
select { | ||
case <-w.ctx.Done(): | ||
if err != nil { | ||
return fmt.Errorf("timeout while waiting for condition, last error: %s", err) | ||
} | ||
return fmt.Errorf("timeout while waiting for condition") | ||
case <-time.After(w.params.retryInterval()): | ||
} | ||
|
||
return nil | ||
} |