Skip to content

Commit 6f832d7

Browse files
committed
feature: GetTime from substrate in the ntpCheck
1 parent 10396ec commit 6f832d7

1 file changed

Lines changed: 48 additions & 26 deletions

File tree

pkg/perf/healthcheck/ntp.go

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,34 @@ package healthcheck
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"math"
78
"net/http"
89
"time"
910

1011
"github.com/cenkalti/backoff/v3"
1112
"github.com/pkg/errors"
1213
"github.com/rs/zerolog/log"
14+
"github.com/threefoldtech/zbus"
15+
"github.com/threefoldtech/zos/pkg/perf"
16+
"github.com/threefoldtech/zos/pkg/stubs"
1317
"github.com/threefoldtech/zos/pkg/zinit"
1418
)
1519

1620
const acceptableSkew = 10 * time.Minute
1721

18-
// TimeServer represents a time server with its name and fetching function
19-
type TimeServer struct {
20-
Name string
21-
Func func() (time.Time, error)
22-
}
23-
24-
// List of time servers
25-
var timeServers = []TimeServer{
26-
{
27-
Name: "worldtimeapi",
28-
Func: getWorldTimeAPI,
29-
},
30-
{
31-
Name: "worldclockapi",
32-
Func: getWorldClockAPI,
33-
},
34-
{
35-
Name: "timeapi.io",
36-
Func: getTimeAPI,
37-
},
38-
}
39-
4022
func RunNTPCheck(ctx context.Context) {
23+
operation := func() error {
24+
return ntpCheck(ctx)
25+
}
4126
go func() {
4227
for {
4328
exp := backoff.NewExponentialBackOff()
4429
retryNotify := func(err error, d time.Duration) {
4530
log.Error().Err(err).Msg("failed to run ntp check")
4631
}
4732

48-
if err := backoff.RetryNotify(ntpCheck, backoff.WithContext(exp, ctx), retryNotify); err != nil {
33+
if err := backoff.RetryNotify(operation, backoff.WithContext(exp, ctx), retryNotify); err != nil {
4934
log.Error().Err(err).Send()
5035
continue
5136
}
@@ -62,10 +47,14 @@ func RunNTPCheck(ctx context.Context) {
6247
}()
6348
}
6449

65-
func ntpCheck() error {
50+
func ntpCheck(ctx context.Context) error {
6651
z := zinit.Default()
52+
zcl, err := perf.TryGetZbusClient(ctx)
6753

68-
utcTime, err := getCurrentUTCTime()
54+
if err != nil {
55+
return fmt.Errorf("ntpCheck expects zbus client in the context and found none %w", err)
56+
}
57+
utcTime, err := getCurrentUTCTime(zcl)
6958
if err != nil {
7059
return err
7160
}
@@ -79,7 +68,35 @@ func ntpCheck() error {
7968
return nil
8069
}
8170

82-
func getCurrentUTCTime() (time.Time, error) {
71+
func getCurrentUTCTime(zcl zbus.Client) (time.Time, error) {
72+
73+
// TimeServer represents a time server with its name and fetching function
74+
type TimeServer struct {
75+
Name string
76+
Func func() (time.Time, error)
77+
}
78+
79+
// List of time servers
80+
var timeServers = []TimeServer{
81+
{
82+
Name: "tfchain",
83+
Func: func() (time.Time, error) {
84+
return getTimeChainWithZCL(zcl)
85+
},
86+
},
87+
{
88+
Name: "worldtimeapi",
89+
Func: getWorldTimeAPI,
90+
},
91+
{
92+
Name: "worldclockapi",
93+
Func: getWorldClockAPI,
94+
},
95+
{
96+
Name: "timeapi.io",
97+
Func: getTimeAPI,
98+
},
99+
}
83100
for _, server := range timeServers {
84101
utcTime, err := server.Func()
85102
if err == nil {
@@ -140,3 +157,8 @@ func getTimeAPI() (time.Time, error) {
140157

141158
return utcTime.DateTime, nil
142159
}
160+
161+
func getTimeChainWithZCL(zcl zbus.Client) (time.Time, error) {
162+
gw := stubs.NewSubstrateGatewayStub(zcl)
163+
return gw.GetTime(context.Background())
164+
}

0 commit comments

Comments
 (0)