mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-30 02:46:30 +02:00
This also replaces instances where we manually write "return ctx.Err()" with "return context.Cause(ctx)" which is functionally identical, but will also correctly propagate cause errors if present.
50 lines
967 B
Go
50 lines
967 B
Go
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 context.Cause(ctx)
|
|
}
|
|
|
|
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
|
|
}
|