46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
healthpb "google.golang.org/grpc/health/grpc_health_v1"
|
|
)
|
|
|
|
// main 是本地容器 healthcheck 使用的最小 gRPC health probe。
|
|
func main() {
|
|
addr := flag.String("addr", "", "gRPC address to probe")
|
|
service := flag.String("service", "", "gRPC health service name")
|
|
timeout := flag.Duration("timeout", 800*time.Millisecond, "probe timeout")
|
|
flag.Parse()
|
|
|
|
if *addr == "" {
|
|
log.Fatal("-addr is required")
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
|
defer cancel()
|
|
|
|
conn, err := grpc.DialContext(ctx, *addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
resp, err := healthpb.NewHealthClient(conn).Check(ctx, &healthpb.HealthCheckRequest{Service: *service})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if resp.GetStatus() != healthpb.HealthCheckResponse_SERVING {
|
|
_, _ = fmt.Fprintf(os.Stderr, "service %q is %s\n", *service, resp.GetStatus())
|
|
os.Exit(1)
|
|
}
|
|
}
|