diff --git a/main.go b/main.go index b31ec96a..40b0fa47 100644 --- a/main.go +++ b/main.go @@ -15,11 +15,15 @@ import ( "path/filepath" "strings" "syscall" + "time" _ "github.com/gripmock/grpc-interceptors" "github.com/rs/zerolog" _ "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" _ "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" + "google.golang.org/grpc" + _ "google.golang.org/grpc/health" + healthv1 "google.golang.org/grpc/health/grpc_health_v1" _ "github.com/bavix/gripmock-sdk-go" "github.com/bavix/gripmock/internal/pkg/patcher" @@ -27,7 +31,7 @@ import ( "github.com/bavix/gripmock/stub" ) -var version string +var version = "development" //nolint:funlen,cyclop func main() { @@ -58,11 +62,6 @@ func main() { logger.Fatal().Msg("at least one proto file is required") } - // for backwards compatibility - if os.Args[1] == "gripmock" { - os.Args = append(os.Args[:1], os.Args[2:]...) - } - //nolint:godox // fixme: move validation of required arguments to a separate service logger.Info().Str("release", version).Msg("Starting GripMock") @@ -105,7 +104,16 @@ func main() { // I have an idea to combine gripmock and grpcmock services into one, then this check will be easier to do. // Checking the grpc port of the service. If the port appears, the service has started successfully. go func() { - if _, err := builder.Reflector().Services(ctx); err == nil { + ctx, cancel := context.WithTimeout(ctx, time.Minute) + defer cancel() + + waiter := healthv1.NewHealthClient(builder.GRPCClient()) + check, err := waiter.Check(ctx, &healthv1.HealthCheckRequest{Service: ""}, grpc.WaitForReady(true)) + if err != nil { + return + } + + if check.GetStatus() == healthv1.HealthCheckResponse_SERVING { chReady <- struct{}{} } }() diff --git a/protoc-gen-gripmock/server.tmpl b/protoc-gen-gripmock/server.tmpl index 9895fbe8..04de4660 100644 --- a/protoc-gen-gripmock/server.tmpl +++ b/protoc-gen-gripmock/server.tmpl @@ -20,6 +20,8 @@ import ( "google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "google.golang.org/grpc/health" + healthgrpc "google.golang.org/grpc/health/grpc_health_v1" _ "google.golang.org/grpc/encoding/gzip" "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" @@ -65,6 +67,7 @@ func main() { {{ template "register_services" . }} {{ end }} + healthgrpc.RegisterHealthServer(s, health.NewServer()) reflection.Register(s) builder.Logger().Info().