Skip to content

Commit

Permalink
feature: GetTime from substrate in the ntpCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
xmonader committed Nov 3, 2024
1 parent 10396ec commit 6f832d7
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions pkg/perf/healthcheck/ntp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,34 @@ package healthcheck
import (
"context"
"encoding/json"
"fmt"
"math"
"net/http"
"time"

"github.com/cenkalti/backoff/v3"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zos/pkg/perf"
"github.com/threefoldtech/zos/pkg/stubs"
"github.com/threefoldtech/zos/pkg/zinit"
)

const acceptableSkew = 10 * time.Minute

// TimeServer represents a time server with its name and fetching function
type TimeServer struct {
Name string
Func func() (time.Time, error)
}

// List of time servers
var timeServers = []TimeServer{
{
Name: "worldtimeapi",
Func: getWorldTimeAPI,
},
{
Name: "worldclockapi",
Func: getWorldClockAPI,
},
{
Name: "timeapi.io",
Func: getTimeAPI,
},
}

func RunNTPCheck(ctx context.Context) {
operation := func() error {
return ntpCheck(ctx)
}
go func() {
for {
exp := backoff.NewExponentialBackOff()
retryNotify := func(err error, d time.Duration) {
log.Error().Err(err).Msg("failed to run ntp check")
}

if err := backoff.RetryNotify(ntpCheck, backoff.WithContext(exp, ctx), retryNotify); err != nil {
if err := backoff.RetryNotify(operation, backoff.WithContext(exp, ctx), retryNotify); err != nil {
log.Error().Err(err).Send()
continue
}
Expand All @@ -62,10 +47,14 @@ func RunNTPCheck(ctx context.Context) {
}()
}

func ntpCheck() error {
func ntpCheck(ctx context.Context) error {
z := zinit.Default()
zcl, err := perf.TryGetZbusClient(ctx)

utcTime, err := getCurrentUTCTime()
if err != nil {
return fmt.Errorf("ntpCheck expects zbus client in the context and found none %w", err)
}
utcTime, err := getCurrentUTCTime(zcl)
if err != nil {
return err
}
Expand All @@ -79,7 +68,35 @@ func ntpCheck() error {
return nil
}

func getCurrentUTCTime() (time.Time, error) {
func getCurrentUTCTime(zcl zbus.Client) (time.Time, error) {

// TimeServer represents a time server with its name and fetching function
type TimeServer struct {
Name string
Func func() (time.Time, error)
}

// List of time servers
var timeServers = []TimeServer{
{
Name: "tfchain",
Func: func() (time.Time, error) {
return getTimeChainWithZCL(zcl)
},
},
{
Name: "worldtimeapi",
Func: getWorldTimeAPI,
},
{
Name: "worldclockapi",
Func: getWorldClockAPI,
},
{
Name: "timeapi.io",
Func: getTimeAPI,
},
}
for _, server := range timeServers {
utcTime, err := server.Func()
if err == nil {
Expand Down Expand Up @@ -140,3 +157,8 @@ func getTimeAPI() (time.Time, error) {

return utcTime.DateTime, nil
}

func getTimeChainWithZCL(zcl zbus.Client) (time.Time, error) {
gw := stubs.NewSubstrateGatewayStub(zcl)
return gw.GetTime(context.Background())
}

0 comments on commit 6f832d7

Please sign in to comment.