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: healthcheck #11

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const appName = "cdk"
const (
// NETWORK_CONFIGFILE name to identify the network_custom (genesis) config-file
NETWORK_CONFIGFILE = "custom_network" //nolint:stylecheck

HealthzPort = 3000
)

var (
Expand Down Expand Up @@ -62,6 +64,13 @@ var (
Usage: "Allow that config-files contains deprecated fields",
Required: false,
}

healthcheckPort = cli.Uint64Flag{
Name: "healthcheckPort",
Usage: "Specify port for healthcheck. Default is 3000.",
Required: false,
Value: HealthzPort,
}
)

func main() {
Expand All @@ -75,6 +84,7 @@ func main() {
&saveConfigFlag,
&disableDefaultConfigVars,
&allowDeprecatedFields,
&healthcheckPort,
}
app.Commands = []*cli.Command{
{
Expand Down
21 changes: 21 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"net/http"
"os"
"os/signal"
"runtime"
"time"

zkevm "github.com/0xPolygon/cdk"
dataCommitteeClient "github.com/0xPolygon/cdk-data-availability/client"
Expand Down Expand Up @@ -137,6 +139,25 @@ func start(cliCtx *cli.Context) error {
}
}

// Once service component starts, enable health check.
go func() {
const timeout = 3 * time.Second
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("OK"))
})
port := cliCtx.Uint64("healthcheckPort")
log.Infof("Listening healthcheck on port %d\n", port)
srv := http.Server{
Addr: fmt.Sprintf(":%d", port),
ReadHeaderTimeout: timeout,
}
if err := srv.ListenAndServe(); err != nil {
log.Errorf("Error listening on port = %v", err)
return
}
}()

// Blocking
waitSignal(nil)

return nil
Expand Down
Loading