-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f1a71c
commit a749e67
Showing
7 changed files
with
137 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,134 @@ | ||
package ntp | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"math" | ||
"sync" | ||
"time" | ||
|
||
"github.com/beevik/ntp" | ||
"github.com/pactus-project/pactus/util/logger" | ||
) | ||
|
||
type Server struct { | ||
const ( | ||
maxClockOffset = time.Duration(math.MinInt64) | ||
) | ||
|
||
type Checker struct { | ||
lock sync.RWMutex | ||
ctx context.Context | ||
cancel func() | ||
|
||
threshold time.Duration | ||
offset time.Duration | ||
interval time.Duration | ||
logger *logger.SubLogger | ||
serverList []string | ||
} | ||
|
||
func NewNtpServer() *Server { | ||
server := &Server{ | ||
threshold: 1 * time.Second, | ||
serverList: []string{ | ||
"0.pool.ntp.org", | ||
"1.pool.ntp.org", | ||
"2.pool.ntp.org", | ||
"3.pool.ntp.org", | ||
}, | ||
retryTimes int | ||
} | ||
|
||
func NewNtpChecker(interval time.Duration) *Checker { | ||
ctxWithCancel, cancel := context.WithCancel(context.Background()) | ||
server := &Checker{ | ||
ctx: ctxWithCancel, | ||
cancel: cancel, | ||
interval: interval, | ||
threshold: 1 * time.Second, | ||
retryTimes: 3, | ||
} | ||
|
||
server.logger = logger.NewSubLogger("_ntp", server) | ||
|
||
return server | ||
} | ||
|
||
func (s *Server) ClockOffset() time.Duration { | ||
clockOffset := time.Duration(math.MinInt64) | ||
func (c *Checker) clockOffset() time.Duration { | ||
clockOffset := maxClockOffset | ||
|
||
for _, server := range s.serverList { | ||
remoteTime, err := ntp.Time(server) | ||
for i := 0; i < c.retryTimes; i++ { | ||
server := "pool.ntp.org" | ||
response, err := ntp.Query(server) | ||
if err != nil { | ||
s.logger.Debug("ntp error", "server", server, "error", err) | ||
c.logger.Debug( | ||
fmt.Sprintf("ntp error(retry %v/%v times)", i+1, c.retryTimes), | ||
"server", server, "error", err, | ||
) | ||
|
||
continue | ||
} | ||
|
||
clockOffset = time.Since(remoteTime) | ||
if err := response.Validate(); err != nil { | ||
c.logger.Debug( | ||
fmt.Sprintf("ntp error(retry %v/%v times)", i+1, c.retryTimes), | ||
"server", server, "error", err, | ||
) | ||
|
||
continue | ||
} | ||
|
||
clockOffset = response.ClockOffset | ||
|
||
break | ||
} | ||
|
||
return clockOffset | ||
} | ||
|
||
func (s *Server) String() string { | ||
func (c *Checker) checkClockOffset() { | ||
ticker := time.NewTicker(c.interval) | ||
|
||
for { | ||
select { | ||
case <-c.ctx.Done(): | ||
return | ||
|
||
case <-ticker.C: | ||
c.lock.Lock() | ||
c.offset = c.clockOffset() | ||
|
||
if c.offset == maxClockOffset { | ||
c.logger.Error("error on getting clock offset") | ||
} else if c.OutOfSync(c.offset) { | ||
c.logger.Error( | ||
"The node is out of sync with the network time", | ||
"threshold", c.GetThreshold(), | ||
"offset", c.offset, | ||
) | ||
} | ||
c.lock.Unlock() | ||
} | ||
} | ||
} | ||
|
||
func (c *Checker) OutOfSync(offset time.Duration) bool { | ||
return math.Abs(float64(offset)) > float64(c.threshold) | ||
} | ||
|
||
func (c *Checker) String() string { | ||
Check failure on line 108 in util/ntp/ntp.go GitHub Actions / linting
|
||
return "ntp" | ||
} | ||
|
||
func (s *Server) GetThreshold() time.Duration { | ||
return s.threshold | ||
func (c *Checker) Start() { | ||
go c.checkClockOffset() | ||
} | ||
|
||
func (c *Checker) Stop() { | ||
c.cancel() | ||
} | ||
|
||
func (c *Checker) GetThreshold() time.Duration { | ||
return c.threshold | ||
} | ||
|
||
func (s *Server) OutOfSync(offset time.Duration) bool { | ||
return math.Abs(float64(offset)) > float64(s.threshold) | ||
func (c *Checker) GetClockOffset() (time.Duration, error) { | ||
c.lock.RLock() | ||
offset := c.offset | ||
c.lock.RUnlock() | ||
|
||
if offset == maxClockOffset { | ||
return 0, errors.New("error on getting clock offset") | ||
} | ||
|
||
return offset, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters