Skip to content

Commit

Permalink
chore: execute health check when health endpoint called (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Jan 30, 2023
1 parent dc79c6e commit 84cb3e5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
6 changes: 1 addition & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"syscall"
"time"

"github.com/ChainSafe/sygma-relayer/comm"

coreEvm "github.com/ChainSafe/chainbridge-core/chains/evm"
coreEvents "github.com/ChainSafe/chainbridge-core/chains/evm/calls/events"
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmclient"
Expand Down Expand Up @@ -72,8 +70,6 @@ func Run() error {

log.Info().Msg("Successfully loaded configuration")

go health.StartHealthEndpoint(configuration.RelayerConfig.HealthPort)

topologyProvider, err := topology.NewNetworkTopologyProvider(configuration.RelayerConfig.MpcConfig.TopologyConfiguration)
panicOnError(err)
topologyStore := topology.NewTopologyStore(configuration.RelayerConfig.MpcConfig.TopologyConfiguration.Path)
Expand Down Expand Up @@ -117,7 +113,7 @@ func Run() error {
log.Info().Str("peerID", host.ID().String()).Msg("Successfully created libp2p host")

healthComm := p2p.NewCommunication(host, "p2p/health")
go comm.ExecuteCommHealthCheck(healthComm, host.Peerstore().Peers())
go health.StartHealthEndpoint(configuration.RelayerConfig.HealthPort, healthComm, host)

communication := p2p.NewCommunication(host, "p2p/sygma")
electorFactory := elector.NewCoordinatorElectorFactory(host, configuration.RelayerConfig.BullyConfig)
Expand Down
5 changes: 0 additions & 5 deletions example/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"os/signal"
"syscall"

"github.com/ChainSafe/sygma-relayer/comm"

"github.com/ChainSafe/chainbridge-core/lvldb"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -84,9 +82,6 @@ func Run() error {
panic(err)
}

healthComm := p2p.NewCommunication(host, "p2p/health")
go comm.ExecuteCommHealthCheck(healthComm, host.Peerstore().Peers())

communication := p2p.NewCommunication(host, "p2p/sygma")
electorFactory := elector.NewCoordinatorElectorFactory(host, configuration.RelayerConfig.BullyConfig)
coordinator := tss.NewCoordinator(host, communication, electorFactory)
Expand Down
7 changes: 6 additions & 1 deletion health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import (
"fmt"
"net/http"

"github.com/ChainSafe/sygma-relayer/comm"
"github.com/libp2p/go-libp2p/core/host"
"github.com/rs/zerolog/log"
)

// StartHealthEndpoint starts /health endpoint on provided port that returns ok on invocation
func StartHealthEndpoint(port uint16) {
func StartHealthEndpoint(port uint16, c comm.Communication, h host.Host) {
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
log.Debug().Msg("Health endpoint called")
go comm.ExecuteCommHealthCheck(c, h.Peerstore().Peers())
_, _ = w.Write([]byte("ok"))
})

_ = http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
log.Info().Msgf("started /health endpoint on port %d", port)
}

0 comments on commit 84cb3e5

Please sign in to comment.