-
Notifications
You must be signed in to change notification settings - Fork 596
/
await.go
91 lines (82 loc) · 2.34 KB
/
await.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright 2020 The Knative Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lib
import (
"context"
"fmt"
"io"
"net/http"
"time"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/util/wait"
)
// WaitFor will register a wait routine to be resolved later
func WaitFor(name string, routine AwaitRoutine) {
w := &namedAwait{
name: name,
routine: routine,
}
waits = append(waits, w)
}
// AwaitForAll will wait until all registered wait routines resolves
func AwaitForAll(log *zap.SugaredLogger) error {
var g errgroup.Group
for _, w := range waits {
w := w // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
log.Infof("Wait for %s", w.name)
before := time.Now()
err := w.routine()
took := time.Since(before)
if err != nil {
log.Errorf("Error while waiting for %s: %v", w.name, err)
} else {
log.Infof("Successful wait for %s, took %v to complete", w.name, took)
}
return err
})
}
waits = nil
// Wait for all waits to complete.
if err := g.Wait(); err != nil {
return err
}
return nil
}
// WaitForReadiness will wait until readiness endpoint reports OK
func WaitForReadiness(port int, log *zap.SugaredLogger) error {
return wait.PollUntilContextTimeout(context.Background(), 10*time.Millisecond, 5*time.Minute, true, func(ctx context.Context) (done bool, err error) {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/healthz", port))
if err != nil {
log.Debugf("Error while connecting: %v", err)
return false, nil
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
return resp.StatusCode == 200 && string(body) == "OK", nil
})
}
type AwaitRoutine func() error
type namedAwait struct {
name string
routine AwaitRoutine
}
var waits []*namedAwait