-
Notifications
You must be signed in to change notification settings - Fork 280
/
health.go
50 lines (45 loc) · 958 Bytes
/
health.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
package xdserr
import (
"context"
"errors"
"io"
"net/http"
"net/url"
"github.com/pomerium/pomerium/pkg/grpc/config"
)
// WaitForHealthy waits until all routes are up
func WaitForHealthy(ctx context.Context, client *http.Client, routes []*config.Route) error {
healthy := 0
for healthy != len(routes) && ctx.Err() == nil {
healthy = 0
for _, r := range routes {
if err := checkHealth(ctx, client, r.From); err != nil {
continue
}
healthy++
}
}
return ctx.Err()
}
func checkHealth(ctx context.Context, client *http.Client, addr string) error {
u, err := url.Parse(addr)
if err != nil {
return err
}
req := http.Request{
Method: http.MethodGet,
URL: u,
}
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
if _, err = io.ReadAll(resp.Body); err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status)
}
return nil
}