Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: grpc health service #4020

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion cmd/dex/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"

"github.com/dexidp/dex/api/v2"
Expand Down Expand Up @@ -59,6 +61,8 @@ var buildInfo = prometheus.NewGaugeVec(
[]string{"version", "go_version", "platform"},
)

var healthCheckPeriod = 15 * time.Second

func commandServe() *cobra.Command {
options := serveOptions{}

Expand Down Expand Up @@ -379,7 +383,7 @@ func runServe(options serveOptions) error {
CheckName: "storage",
CheckFunc: storage.NewCustomHealthCheckFunc(serverConfig.Storage, serverConfig.Now),
},
gosundheit.ExecutionPeriod(15*time.Second),
gosundheit.ExecutionPeriod(healthCheckPeriod),
gosundheit.InitiallyPassing(true),
)

Expand Down Expand Up @@ -508,8 +512,24 @@ func runServe(options serveOptions) error {
}

grpcSrv := grpc.NewServer(grpcOptions...)
healthcheck := health.NewServer()
healthgrpc.RegisterHealthServer(grpcSrv, healthcheck)
api.RegisterDexServer(grpcSrv, server.NewAPI(serverConfig.Storage, logger, version, serv))

go func() {
var status healthgrpc.HealthCheckResponse_ServingStatus
for {
switch healthChecker.IsHealthy() {
case true:
status = healthgrpc.HealthCheckResponse_SERVING
default:
status = healthgrpc.HealthCheckResponse_NOT_SERVING
}
healthcheck.SetServingStatus("", status)
time.Sleep(healthCheckPeriod)
}
}()
Comment on lines +519 to +531
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could ae avoid using goroutines in goroutines here and use a separate group for the health endpoint?


grpcMetrics.InitializeMetrics(grpcSrv)
if c.GRPC.Reflection {
logger.Info("enabling reflection in grpc service")
Expand All @@ -520,6 +540,24 @@ func runServe(options serveOptions) error {
return grpcSrv.Serve(grpcListener)
}, func(err error) {
logger.Debug("starting graceful shutdown", "server", "grpc")
done := make(chan struct{})

go func() {
healthcheck.Shutdown()
grpcSrv.GracefulStop()
close(done)
}()

select {
case <-done:
// Graceful shutdown completed within the timeout
logger.Debug("Graceful shutdown completed", "server", "grpc")
case <-time.After(time.Minute):
// Timeout reached, force stop the server
logger.Debug("Graceful shutdown timed out. forcing shutdown", "server", "grpc")
grpcSrv.Stop()
}

grpcSrv.GracefulStop()
})
}
Expand Down
Loading