53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package containers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"buf.build/gen/go/grpc/grpc/connectrpc/go/grpc/health/v1/healthv1connect"
|
|
v1 "buf.build/gen/go/grpc/grpc/protocolbuffers/go/grpc/health/v1"
|
|
"connectrpc.com/connect"
|
|
|
|
"code.icb4dc0.de/buildr/buildr/internal/rpc"
|
|
)
|
|
|
|
var _ Probe = (*GrpcHealthProbe)(nil)
|
|
|
|
func NewGRPCHealthProbe(baseUrl string) GrpcHealthProbe {
|
|
return GrpcHealthProbe{
|
|
client: healthv1connect.NewHealthClient(rpc.NewH2cHTTPClient(), baseUrl),
|
|
}
|
|
}
|
|
|
|
type GrpcHealthProbe struct {
|
|
client healthv1connect.HealthClient
|
|
backoffDuration time.Duration
|
|
}
|
|
|
|
func (g GrpcHealthProbe) Execute(ctx context.Context) (ready chan bool, errs chan error) {
|
|
ready = make(chan bool)
|
|
errs = make(chan error)
|
|
|
|
go func() {
|
|
for ctx.Err() == nil {
|
|
resp, err := g.client.Check(ctx, connect.NewRequest(new(v1.HealthCheckRequest)))
|
|
if err != nil {
|
|
var conErr *connect.Error
|
|
if errors.As(err, &conErr) && conErr.Code() == connect.CodeUnavailable {
|
|
continue
|
|
}
|
|
errs <- err
|
|
continue
|
|
}
|
|
|
|
if resp.Msg.Status == v1.HealthCheckResponse_SERVING {
|
|
ready <- true
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
return
|
|
}
|