Skip to content

Commit

Permalink
Add healthz check
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Nov 27, 2024
1 parent 8e89c88 commit cafe1de
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ FROM gcr.io/distroless/static-debian11

COPY --from=builder /bin/quickpizza /bin

EXPOSE 3333 3334
EXPOSE 3333 3334 3335
ENTRYPOINT [ "/bin/quickpizza" ]
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func main() {
}

if envServe("QUICKPIZZA_GRPC") {
grpcServer := qpgrpc.NewServer(":3334")
grpcServer := qpgrpc.NewServer(":3334", ":3335")
go func() {
err := grpcServer.ListenAndServe()
if err != nil {
Expand Down
31 changes: 26 additions & 5 deletions pkg/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"math/rand"
"net"
"net/http"

pb "github.com/grafana/quickpizza/pkg/grpc/quickpizza"
"google.golang.org/grpc"
Expand All @@ -16,8 +17,9 @@ type serverImplementation struct {
}

type Server struct {
grpcServer *grpc.Server
listen string
grpcServer *grpc.Server
listen string
healthzListen string
}

func (s *serverImplementation) Status(_ context.Context, in *pb.StatusRequest) (*pb.StatusResponse, error) {
Expand All @@ -34,11 +36,28 @@ func (s *serverImplementation) EvaluatePizza(_ context.Context, in *pb.PizzaEval
}, nil
}

func NewServer(listen string) *Server {
func NewServer(listen string, healthzListen string) *Server {
s := grpc.NewServer()
pb.RegisterGRPCServer(s, &serverImplementation{})

return &Server{grpcServer: s, listen: listen}
return &Server{grpcServer: s, listen: listen, healthzListen: healthzListen}
}

func (s *Server) listenHealthz() {
mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNoContent)
})

health := &http.Server{
Addr: s.healthzListen,
Handler: mux,
}

slog.Info("Starting QuickPizza gRPC health check server", "listenAddress", s.healthzListen)
if err := health.ListenAndServe(); err != nil {
slog.Error("Error listening for gRPC health check server", "err", err)
}
}

func (s *Server) ListenAndServe() error {
Expand All @@ -47,6 +66,8 @@ func (s *Server) ListenAndServe() error {
return fmt.Errorf("failed to listen on port: %w", err)
}

slog.Info("Starting QuickPizza gRPC", "listenAddress", s.listen)
go s.listenHealthz()

slog.Info("Starting QuickPizza gRPC server", "listenAddress", s.listen)
return s.grpcServer.Serve(lis)
}

0 comments on commit cafe1de

Please sign in to comment.