Skip to content

Commit

Permalink
feat: healthcheck (#11)
Browse files Browse the repository at this point in the history
* Quick healthcheck

* Fmt

* address lint
  • Loading branch information
Vui-Chee authored Dec 3, 2024
1 parent 273ca68 commit 4b088c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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

0 comments on commit 4b088c7

Please sign in to comment.