From 2cba8fafd851a47bdc9ea6d96b27db2b701c306e Mon Sep 17 00:00:00 2001 From: "Bo-Yi.Wu" Date: Sat, 20 Aug 2022 11:53:19 +0800 Subject: [PATCH] chore(grpc): add health check and grpc v1, alpha version Signed-off-by: Bo-Yi.Wu --- routers/api/bots/bots.go | 4 +++ routers/api/bots/grpc.go | 32 +++++++++++++++++++++ routers/api/bots/{process.go => handler.go} | 0 routers/api/bots/health.go | 29 +++++++++++++++++++ routers/api/bots/ping.go | 23 --------------- routers/api/bots/runner.go | 23 --------------- 6 files changed, 65 insertions(+), 46 deletions(-) create mode 100644 routers/api/bots/grpc.go rename routers/api/bots/{process.go => handler.go} (100%) create mode 100644 routers/api/bots/health.go diff --git a/routers/api/bots/bots.go b/routers/api/bots/bots.go index 0584cb86bd..62991f07bc 100644 --- a/routers/api/bots/bots.go +++ b/routers/api/bots/bots.go @@ -15,4 +15,8 @@ func Routes(r *web.Route) { runnerServiceRoute(r) // ping service pingServiceRoute(r) + // health service + healthServiceRoute(r) + // grpcv1 and v1alpha service + grpcServiceRoute(r) } diff --git a/routers/api/bots/grpc.go b/routers/api/bots/grpc.go new file mode 100644 index 0000000000..282aaa2980 --- /dev/null +++ b/routers/api/bots/grpc.go @@ -0,0 +1,32 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package bots + +import ( + "code.gitea.io/gitea/modules/web" + "gitea.com/gitea/proto-go/ping/v1/pingv1connect" + + "github.com/bufbuild/connect-go" + grpcreflect "github.com/bufbuild/connect-grpcreflect-go" +) + +func grpcServiceRoute(r *web.Route) { + compress1KB := connect.WithCompressMinBytes(1024) + + // grpcV1 + grpcPath, gHandler := grpcreflect.NewHandlerV1( + grpcreflect.NewStaticReflector(pingv1connect.PingServiceName), + compress1KB, + ) + + // grpcV1Alpha + grpcAlphaPath, gAlphaHandler := grpcreflect.NewHandlerV1Alpha( + grpcreflect.NewStaticReflector(pingv1connect.PingServiceName), + compress1KB, + ) + + r.Post(grpcPath+"{name}", grpcHandler(gHandler)) + r.Post(grpcAlphaPath+"{name}", grpcHandler(gAlphaHandler)) +} diff --git a/routers/api/bots/process.go b/routers/api/bots/handler.go similarity index 100% rename from routers/api/bots/process.go rename to routers/api/bots/handler.go diff --git a/routers/api/bots/health.go b/routers/api/bots/health.go new file mode 100644 index 0000000000..46eb5c7eeb --- /dev/null +++ b/routers/api/bots/health.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package bots + +import ( + "code.gitea.io/gitea/modules/web" + "gitea.com/gitea/proto-go/ping/v1/pingv1connect" + "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" + + "github.com/bufbuild/connect-go" + grpchealth "github.com/bufbuild/connect-grpchealth-go" +) + +func healthServiceRoute(r *web.Route) { + compress1KB := connect.WithCompressMinBytes(1024) + + // grpcHealthCheck + grpcHealthPath, gHealthHandler := grpchealth.NewHandler( + grpchealth.NewStaticChecker( + runnerv1connect.RunnerServiceName, + pingv1connect.PingServiceName, + ), + compress1KB, + ) + + r.Post(grpcHealthPath+"{name}", grpcHandler(gHealthHandler)) +} diff --git a/routers/api/bots/ping.go b/routers/api/bots/ping.go index df23ba6cb9..efbc416657 100644 --- a/routers/api/bots/ping.go +++ b/routers/api/bots/ping.go @@ -14,8 +14,6 @@ import ( "gitea.com/gitea/proto-go/ping/v1/pingv1connect" "github.com/bufbuild/connect-go" - grpchealth "github.com/bufbuild/connect-grpchealth-go" - grpcreflect "github.com/bufbuild/connect-grpcreflect-go" ) type PingService struct{} @@ -43,26 +41,5 @@ func pingServiceRoute(r *web.Route) { compress1KB, ) - // grpcV1 - grpcPath, gHandler := grpcreflect.NewHandlerV1( - grpcreflect.NewStaticReflector(pingv1connect.PingServiceName), - compress1KB, - ) - - // grpcV1Alpha - grpcAlphaPath, gAlphaHandler := grpcreflect.NewHandlerV1Alpha( - grpcreflect.NewStaticReflector(pingv1connect.PingServiceName), - compress1KB, - ) - - // grpcHealthCheck - grpcHealthPath, gHealthHandler := grpchealth.NewHandler( - grpchealth.NewStaticChecker(pingv1connect.PingServiceName), - compress1KB, - ) - r.Post(connectPath+"{name}", grpcHandler(connecthandler)) - r.Post(grpcPath+"{name}", grpcHandler(gHandler)) - r.Post(grpcAlphaPath+"{name}", grpcHandler(gAlphaHandler)) - r.Post(grpcHealthPath+"{name}", grpcHandler(gHealthHandler)) } diff --git a/routers/api/bots/runner.go b/routers/api/bots/runner.go index 17853c3ae0..a6cc32fbda 100644 --- a/routers/api/bots/runner.go +++ b/routers/api/bots/runner.go @@ -13,8 +13,6 @@ import ( "gitea.com/gitea/proto-go/runner/v1/runnerv1connect" "github.com/bufbuild/connect-go" - grpchealth "github.com/bufbuild/connect-grpchealth-go" - grpcreflect "github.com/bufbuild/connect-grpcreflect-go" ) type RunnerService struct{} @@ -55,26 +53,5 @@ func runnerServiceRoute(r *web.Route) { compress1KB, ) - // grpcV1 - grpcPath, gHandler := grpcreflect.NewHandlerV1( - grpcreflect.NewStaticReflector(runnerv1connect.RunnerServiceName), - compress1KB, - ) - - // grpcV1Alpha - grpcAlphaPath, gAlphaHandler := grpcreflect.NewHandlerV1Alpha( - grpcreflect.NewStaticReflector(runnerv1connect.RunnerServiceName), - compress1KB, - ) - - // grpcHealthCheck - grpcHealthPath, gHealthHandler := grpchealth.NewHandler( - grpchealth.NewStaticChecker(runnerv1connect.RunnerServiceName), - compress1KB, - ) - r.Post(connectPath+"{name}", grpcHandler(connecthandler)) - r.Post(grpcPath+"{name}", grpcHandler(gHandler)) - r.Post(grpcAlphaPath+"{name}", grpcHandler(gAlphaHandler)) - r.Post(grpcHealthPath+"{name}", grpcHandler(gHealthHandler)) }